Table Object Redraw With Empty Sort Array Throws Error
Table Object Redraw With Empty Sort Array Throws Error
According DT documentation, the proper way to not sort a Table is to pass an empty array to the "order" option.
Also, to redraw a table, with the new documentation, it's recommended to not destroy and reinstantiate the table, but rather call clear() and then rows.add(data), and then draw() on the existing table object.
However, if the first draw has a sort, and the redraw of the table requires no sort, DT will throw an error: "TypeError: e[j] is undefined".
Sample:
var dashboardOrdersDefaultSort = [[6,'desc']];
if ( /* some condition */ ) {
dashboardOrdersDefaultSort = [];
}
if (!dashboardOrdersDTO) {
dashboardOrdersDTO = $dashboardOrdersTable.DataTable({
destroy:true,
data:theData,
columns:dashboardOrdersMap,
autoWidth:false,
order:dashboardOrdersDefaultSort,
paging:false,
dom:'ft',
searching: false
});
} else {
dashboardOrdersDTO.clear();
dashboardOrdersDTO.rows.add(theData);
dashboardOrdersDTO.order(dashboardOrdersDefaultSort).draw();
}
Any help is much appreciated, thanks!
Answers
This won't remove any sort applied to the table - it means don't resort the data, but the data will already be sorted! This plug-in can be used to have DataTables show data in the index order.
If that doesn't work for you, please show a test case.
Allan
That works, but the remaining question is how to apply a sort to the table redraw, in the case that the instantiation has order:[ ] and the redraw then wants a sort applied.
Thanks for the quick response sir!
Wups, I got it.
Here is what works, using the plugin solution mentioned above.
Thanks again for the reply.
I still think it might be buggy that:
Really appreciate the help and speedy reply!
For anyone reading this, something insinuated but not explicitly mentioned above is that, in this case, "dashboardOrdersDefaultSort" can change value between initializing and subsequent redraws of the table, as well.
You might want to
.slice()
the array you are passing in. I can't remember off the top of my head, but I think DataTables will simply use the array you pass in, so any changed made to it would still be in effect.Allan