mData breaks fnGetData(); #145

Closed
zba opened this Issue Jan 7, 2013 · 9 comments

Comments

Projects
None yet
2 participants

zba commented Jan 7, 2013

If use mData with already present table, the type of aoData becames Array[0] while internally it is an Object, see below snipplet:

    $(document).ready( function() {
         var oTable = $('#example').dataTable( {
           "aoColumns": [
             { "mData": "engine" },
             { "mData": "browser" },
             { "mData": "platform.inner" },
             { "mData": "platform.details.0" },
             { "mData": "platform.details.1" }
          ]
         } );
         oTable.fnAddData({
           engine: 'eng',
           browser: 'br',
           platform: { 
             inner: 'pi', 
             details: ['d0','d1']
           }
         });
        console.log('---------------');
        console.log(oTable.fnGetData());   //[Array[0],Array[0],...,Object]
        console.log(JSON.stringify(oTable.fnGetData())); // [[],[], .... ,[],{"engine":"eng","browser":"br","platform":{"inner":"pi","details":["d0","d1"]}}]
        console.log(oTable.fnGetData()[0]); 
        console.log('---------------');
      } );

jsBin

browser Google-chrome 25.0.1323.1

@zba zba added a commit to zba/DataTables that referenced this issue Jan 7, 2013

@zba zba Fix #145 0ad4b20
Owner

DataTables commented Feb 1, 2013

This is an absolute cracker - I'm not sure how this can actually be fixed...! Your solution works assuming that you want to use an object only (defined by the first column's mData - which could actually be null and have some default content, which would break this). It is also possible to have an array with object properties and mix the two. Not sure why you would do that, but it is possible. Forcing an object would break this.

Possibly DataTables needs to have a flag that will let the developer pick is empty objects will be arrays or objects. I don't see how it can be automatically detected 100% of the time.

Going to leave open for the moment to have a bit of a think about it. Any comments / suggestions are very welcome!

zba commented Feb 2, 2013

What is the usage case when mData can be null and .fnAddData() uses Array ? Can you show me little sampe ?
btw;

zba commented Feb 2, 2013

My initial though about this was:
It may be good idea to initalise datatables other way than now. I.e. to convert html table to object and feed it as .fnAddData() does, it will fix the functional duplication and make table acting the same, regardless of data source.

Owner

DataTables commented Feb 2, 2013

What is the usage case when mData can be null and .fnAddData() uses Array ?

You could have a rendered column using mRender or sDefaultContent effectively ignoring any data already in the column. Perhaps an edge case, but very much a possibility.

convert html table to object

Possibly that is a good idea, and if I were starting over, I might decide to do it that way, but changing from arrays to objects now would utterly break an awful lot of code! Something as simple as t.fnGetData( tr ).length for example.

So I think an initialisation option which tells DataTables to use arrays or objects is probably the best way forward, but I want to think about it some more. There are already a bonkers number of options and I want to make sure any new ones are worth adding!

zba commented Feb 2, 2013

I though not about convert to arrays, but make initial html table to be datasource, may be with option to be array or not, now, if datatable has been initialised from html table, it going to be almost unusable when you change it content dynamicaly, o'c you can add data to DOM and reinit table, but it is not very good idea. Also I remember that changing a row calls whole table reinit, any reason for that ?

Owner

DataTables commented Feb 2, 2013

I'm not sure I really follow I'm afraid. If you read the data front he DOM, then it is read into an array. DataTables does that at the moment and it has proven to be quite a successful way of doing it. If you read from a DOM data source, then adding and manipulating the table should also be done with arrays - although adding an option to use objects rather than arrays is a possibility.

Also I remember that changing a row calls whole table reinit, any reason for that ?

No it doesn't. fnUpdate (the API method to change the value of a cell) will not require a reinitilaisation of the table.

zba commented Feb 5, 2013

No it doesn't. fnUpdate (the API method to change the value of a cell) will not require a reinitilaisation of the table.

I meant this demo the mRender was called more than 350 times, when I just add one line, this looks strange for me.

Owner

DataTables commented Feb 5, 2013

When you add the extra row, your rendering function is called for the following reasons:

  • Once for type detection on the numbers column. The other columns are all strings, so they don't need checked
  • Five times to get the display data for the row
  • 58 times to get the sort data for the column being sorted (since a resort is required)
  • 290 times (5 * 58) to get the sort data since the filtering has been invalidated

The last one seems a bit odd to me - I'm not 100% sure why it is invalidating the whole filtering set - it should only be calculating the filter for the newly added row. This I will look into.

Owner

DataTables commented May 18, 2013

I'm going to close this issue due to the work done on v1.10. fnGetData is now effectively redundant being replaced by row().data() and rows().data() . fnGetData will proxy the data request through to the new API.

DataTables closed this May 18, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment