reset on a copied data table
reset on a copied data table

https://datatables.net/reference/api/colReorder.reset()
I have the following use case:
Send all data of the selected data table row to the server using buttons.exportData() in original column sequence.
https://datatables.net/reference/api/buttons.exportData()
colReorder.reset() restores the original column sequence easily, BUT: I don't want this to be done on the original data table that the user is seeing on the screen but on a copy of the data table, so that the user experience isn't being influenced.
I tried copying the DT using $.extend(true, {} ... also $.extend(true, [] ... etc.
Here are some examples
var copiedDT = $.extend(true, [], originalDT);
or
var copiedDT = $.extend(true, {}, originalDT);
or
var copiedDT = $.extend(true, [], originalDT.columns().data().toArray());
or
var copiedDT = originalDT.slice();
Then I did:
copiedDT.colReorder.reset();
In all cases the result was that BOTH data tables were reset. I couldn't dissolve the connection between the two dts.
Is there any way to do this? I use $.extend a lot and it has been working fine, e.g. with options objects, custom buttons and the like.
This question has accepted answers - jump to:
Answers
Interesting one!
If you have arrays of row data, then you would probably need to do
.slice()
on each row (a top level.slice()
isn't enough since that's just references to the inner arrays which remain).If you are able to live to a simple test case showing the issue I might be able to be a bit more specific?
Thanks,
Allan
Thanks for that, Allan! I thought you might have come across this issue earlier and had a simple solution for it. But never mind! This seems to be a tricky one
I have already implemented a good work around which is probably performing better because it doesn't require creating a copy of the data table.
On the client side I create a new array of "original" column indexes. This array has pretty big gaps in terms of values because the "selector" makes sure that quite a few columns aren't send to the server anyway.
Now I had to kind of "apply" the original order to the data sent to the server. I couldn't do this with JS and found out that it is really easy using PHP.
This creates the array of original indexes:

This sends everything to the server:

And here I apply the original order to the exported data with PHP:

"asort" (sort an array in ascending order and maintain index association) changes the order; writing the new arrays $data, $header and $groups makes sure there are no gaps in the array (see above).
The "asort"ed keys were e.g. 3, 4, 0, 27 etc. which reflects the original order.
Nice one! That looks like a good way of handling it.
Allan