rows().data() vs cell().data()

rows().data() vs cell().data()

Karl53Karl53 Posts: 72Questions: 29Answers: 0
edited December 2015 in DataTables 1.10

Is rows().data() returning the values from DataTables internal cache while cell().data() is returning the value of the DOM's TD innerHTML?

I'm trying to understand why rows().date() returns NULLs for the cell's value while cell().data() returns an empty string.

I'm initializing a table with empty rows by calling this within a loop:

table.row.add({"idx" : null, "field1": null, "field2": null});

And I'm making the api calls this way, immediately after initializing the table:

console.log(table.rows().data());

console.log(table.cell(2, 2).data());

The column's defaultContent option for both "field1" and "field2" is an empty string, so I think I understand the value returned by cell().data(). My question really is why the 2 api calls don't return the same value for the same cell?

EDIT

Further, a call to table.rows().draw(); does not sync the values up.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Is rows().data() returning the values from DataTables internal cache while cell().data() is returning the value of the DOM's TD innerHTML?

    No - they both return the internal cache value. Or at least they should!

    Could you link to a test page showing the issue so I can debug it please?

    Allan

  • Karl53Karl53 Posts: 72Questions: 29Answers: 0

    Allan, thanks for the prompt reply. I'll double check what I'm saying and put together a sample project to demonstrate this, assuming I've not made an error, and send it your way.

    This is interesting, because cell().data() is documented this way:

    Note that when used as a setter, this method sets the data to apply to the table, but >does not update the table's internal caches of data until the draw() method is called

    ....

    Moreover, although the internal cache is not updated until the next draw, the change to >the cell's content is visible immediately upon calling this method as a setter, as it writes >to the cell's content using innerHTML.

    So, the two forms of cell() method, reads from and writes to different locations?

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Hi,

    We discussed this by e-mail, but for anyone else who encounters this, there are a couple of issues here:

    1. cell().data() should return the plain data from the data source object/array. Currently if that is null and columns.defaultContent is given, it will return the default content. That is wrong and has been fixed.
    2. The terminology of the documentation is a bit confusing. The commit above also changes that to help try and clear it up - basically the set operation will write both to the data source object and also to the HTML of the document. At the same time it will invalidate the row, which means that the search and sorting caches (the "internal caches" the documentation refers to) are cleared and will be refreshed on the next draw.

    Allan

This discussion has been closed.