jquery-datatables-checkboxes icon indicating copy to clipboard operation
jquery-datatables-checkboxes copied to clipboard

Sorting table after initial load

Open NoxxieNl opened this issue 7 years ago • 2 comments

Hiya,

First of all great piece of software you wrote here.

How ever I stumbled upon a issue I cannot resolve.

When I create my data table en select a couple of rows en use the following code:

table.column(0).checkboxes.selected();

and output the ID's to console I get the id's orderd on the generation of the data table.

If I sort the the datatable by another column and then again to the same function as above I still get the same order of id's when the data table was created.

What I expected was that the ordering of the id's with

table.column(0).checkboxes.selected();

Also changed upon reodering the table, is this a bug or am I missing something here?

Edit:

Funny thing is with the old one: https://www.gyrocode.com/articles/jquery-datatables-checkboxes/

the ordering on column is respected :-)

NoxxieNl avatar May 18 '17 09:05 NoxxieNl

It is by design, data is stored differently and doesn't depend on the sorting order. However you raise a good point, because DataTables own API methods return data based on current order, see this example.

As a temporary workaround, you can use the code below:

// Sort checkboxes data based on current sorting order
var rows_selected_sorted = [];
$.each(table.column(0).data(), function(index, value){
   if($.inArray(value, rows_selected) !== -1){
      rows_selected_sorted.push(value);
   }
});

See this example for code and demonstration.

Please note that the code above will work only in client-side processing mode and WILL NOT work in server-side processing mode. In server-side processing mode, it will be impossible to know ordering of data that is not present on the screen and the code would be more complex.

I will see whether I can add something like the workaround above to the plug-in code as a permanent solution.

mpryvkin avatar May 18 '17 11:05 mpryvkin

Your work arround works like a charm, thanks for the quick fix!

NoxxieNl avatar May 18 '17 12:05 NoxxieNl