SearchPanes: stateSaveCallback with incomplete data
SearchPanes: stateSaveCallback with incomplete data
Here is my test case, a simple table with 4 searchPanes:
http://live.datatables.net/fopuyupo/9/edit?js,console,output
If you click on "run",
- stateSaveCallback
is called without the searchPanes
property by DataTables
- stateSaveCallback
is called with data.searchPanes.panes.length
4 by SearchPanes
=> fine!
If you click on the "searchPanes" button, stateSaveCallback
is called with data.searchPanes.panes.length
1 - only the last searchPane is contained.
Same goes for selecting or deselecting an item of any of the search panes.
So when saving the state to session or database, only the state of the last search pane is stored...
I'm now going to look into the code for the cause...
This question has an accepted answers - jump to answer
Answers
Ok, I'm getting closer to the bug. Hopefully my explanation is understandable:
On init
_buildPane()
is called for each search pane calling_setListeners()
for each search pane.Inside
_setListeners()
the eventstateSaveParams.dtsp
is registered for each search pane.For the first search pane,
data.searchPanes
anddata.searchPanes.panes
isundefined
and gets initialized, the search pane is added to thedata.searchPanes.panes
array....
After the last search pane, the
stateSaveParams.dtsp
event is registered withthis
as the last search pane and the data array contains all search panes.Now DataTables triggers the
stateSave
event and all search panes are contained in the data array.After that, something strange happens. I guess
stateSave
is triggered again by DataTables. So thestateSaveParams.dtsp
event is triggered again, but this time,data.searchPanes
anddata.searchPanes.panes
is undefined,this
still points to the last search pane, so the only search pane added to the array is the last one.The error occurs since the line
279
of the nightly build was changed from$(this.s.dt).off('.dtsp');
to
this.s.dt.off('.dtsp');
The first one contains the desired data whereas the second one is an empty array.
Hope that helps...
Hi @pgerundt ,
Thanks for pointing this out and doing all of the debugging to work out what is going wrong. I'm going to take a look too locally, make the change you've suggested and see if there is anything else in there causing issues. I've raised an issue internally (DD-2063 for my reference) and will report back here when there is an update.
Thanks,
Sandy
Hi @pgerundt ,
Thanks again for pointing this out. You were close, but the actual fix was a bit different to what you described above.
Each searchpane is destroyed and then rebuilt when the buttons popover is displayed. They all have listeners on the main table. When an earlier pane was destroyed it removes the listeners for that event on the main table, then sets it again for that pane specifically. The next pane was then being destroyed and would remove that listener - hence why only the last one was being saved. The fix I have made provides a solution to this.
This will be available in the next SearchPanes release which we hope will be in the next few weeks. Until then you can access the fix from the nightly builds.
Thanks,
Sandy