row.add(node) – how to set data-sort?
row.add(node) – how to set data-sort?

I'm adding rows to a table by calling row.add()
passing tr
node types as described in this API doc page:
var row = $.parseHTML(_module.templates.importOrdersTableRow({ order: order }), document);
moduleImport.datatable.row.add(row[0]);
That's because I'm using Handlebars templates to render the table rows because there are some complexities in the markup. Before using row.add()
I was rendering a template of the whole table, replace it in the DOM, and reinitialise the DataTable. This worked all fine, but I didn't want to throw away the whole table (there are checkboxes now, too). The problem is that by switching to row.add()
way of doing things, the data-order
on the cell element is ignored, leading to columns ordered by content rather than by data-order
attribute. From this discussion on Stack Overflow, user Gyrocode.com suggests that data-order
is only evaluated at table initialisation.
How can I provide custom sort values to rows added via row.add(rowNode)
?
This question has accepted answers - jump to:
Answers
If you are passing in a
tr
element torow.add()
it the orthogonal data-* attributes will be correctly parsed. At least they should be!There was some discussion about this in this thread were we identified a bug when tryin got add the data in a Javascript object format when originally read from the DOM. But I gave a link there to a test case showing it working correctly when passing in a DOM node, which it sounds like you are doing ( http://live.datatables.net/johenazi/5/edit ).
That being the case, we'd need a link to an example showing the issue to be able to help.
Allan
Thanks. Well, it doesn't work, I can tell you that!
I'll try to build a page that exemplifies the problem for me.
Okay, I think I could reproduce it, check this out: http://live.datatables.net/pirupira/1/edit
No - it won't work in that specific case since there is no data in the table when it is initialised. It does work if there is a row in the table when the table is created: http://live.datatables.net/pirupira/2/edit .
The reason for that is that the orhtogonal data detection will only run on initialisation. It will detect orthogonal data when new rows are added, but it will only configure the table automatically for data-* orthogonal data on init. Without data in the table it can't know that there will be orthogonal data.
The only option when initialising with an empty table is to use the
columns.data
option to tell DataTables where to get the data from: http://live.datatables.net/kejesewe/1/edit .Allan
Thank, that did it for me!