Unexpected behaviour of custom sort reset
Unexpected behaviour of custom sort reset
Link to test case:
https://live.datatables.net/yecebura/2/edit
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
Our custom sort reset function that we used in 1.x version already started to fail under specific circumstances in our regression test.
I think it is a bug and created an example test case in order to reproduce it easily.
The datatable with the defaultOrder
of only one sort [[0, 'asc']]
needs to be loaded correctly.
Then i changed the the sort order manually on the first column to not being sorted at all. Press the 'Reset' button and the default order is [0, '']
. I manually changed the sort order of other columns afterwards and pressed the reset button. Several combinations leading to several outcomes. Sometimes the original sort is set, sometimes the [0, '']
is set.
I actually thought that it works flawless with two or more sort order configurations, but it turns out that there is a similar flaw. When setting the sort order of the table to none by clicking on the first column and pressing the reset button ends up in just resetting the second column. Where as doing it on any column > 1, it always resets the correct order.
I added the state to the table, because we have it too, but it turned out that it seems not to impact the behaviour.
Is this behaviour intended? Is there any way to prevent this?
Please let me know if anything is unclear.
Replies
Hi,
The issue is that the original array is being mutated - i.e. DataTables doesn't take a copy of the array's contents, it actually uses the array you are passing in, so it reflects the current state.
What you need to do is take a copy of the array - normally that would just be
.slice()
but since it is an array inside an array you need to use a deep copy, which you can do withstructuredClone
orjQuery.extend(true, ...)
.https://live.datatables.net/yecebura/3/edit
Allan
Good point, it is written on many places in the documentation as well. So sorry that i missed that. I also screwed up the ticket category...
I fixed that and everything works as expected. Thank you for your time
It's all good - I reckon that is a non-obvious footgun!
Allan