Invalidate/Update sorting on rowCallback
Invalidate/Update sorting on rowCallback
Hello,
I am using a DOM Source for DataTables, the 4th column is a name field with First and Lastname.
There is a checkbox for users that will switch the display to Lastname Firstname.
I have a working method which I will describe now, but I feel that it's awfully inefficient (even though it works well enough, I want it perfect).
So first on document.ready, before I initialize DataTables I do this to assign data attributes with the original order (Firstname Lastname) and reversed order (Lastname Firstname) to each table cell element of the name column:
$('#uebersichttable .listings').each(function(){
$(this).find('.name').data('originalname', $(this).find('.name').text());
$(this).find('.name').data('reversedname', $(this).find('.name').text().split(' ').reverse().join(' '));
});
In the DT init I have this callback function:
$('#uebersichttable').DataTable({
rowCallback: function( row, data, index ) {
if ($('input[name=NameO]').is(':checked')) {
$('td:eq(4)', row).text($('td:eq(4)', row).data('reversedname'));
} else {
$('td:eq(4)', row).text($('td:eq(4)', row).data('originalname'));
}
},
Finally this is my onclick function for the checkbox that toggles the first/last display:
`$('#uebersichttable').DataTable().draw().rows().invalidate().draw();`
As you can see I have to draw, then invalidate, then draw again because otherwise it won't work (the sorting won't get updated correctly to the new display).
As I said, it works fine, but I feel there might be a better way.
I originally tried just using jquery to update the text instead of the rowCallback function, and then afterwards invalidate and redraw only once, but this is unfeasable because I have several other checkboxes that will push filters to the table, and since DT removes the rows entirely when filtered out, updating via jquery and then just invalidating doesn't work because rows that are filtered out won't get updated this way.
Because of this when using the other filters I have to draw, invalidate and draw again there aswell (because otherwise rows that were previously filtered but are visible now would sort incorrectly).
Any other ideas how this could be done more efficiently?
Answers
Disregard my comment, I didn't read your description close enough.
Kevin