StateSave on active buttons
StateSave on active buttons
trongart
Posts: 222Questions: 51Answers: 0
Is it possible to apply stateSave on buttons that become active so when the user returns to the page, the same buttons stay active? I created this test case with an example button. stateSave works with the SearchBuilder, but not on active buttons for me.
This question has an accepted answers - jump to answer
Answers
Use
stateSaveParams
andstateLoadParams
to manipulate custom data to save. I would look at usingbutton().active()
to get the active state of the button instateSaveParams
and save it. Then instateLoadParams
get the button state.My first thought is to use
button().trigger()
if the button is active instateLoadParams
but the button might not be available or the table might not be initialized enough for the button to work. You could use a global variable to save the active state then ininitComplete
trigger the button if active.Kevin
@kthorngren Thank you for your feedback! As you suggested, I tried saving the active state of the button in stateSaveParams and triggering it in stateLoadParams here, but this does not seem to work. I added a class name to the button, but I am unclear how to save and load its state and to trigger it.
You would probably need to activate the button later, probably in
initComplete
.stateLoadParams
is called early, as it needs to get the state of things, so the button may not exist at that point.Colin
Thank you @colin - I'm just unclear what the correct syntax is to get the button active states into stateSaveParams based on the button classes. How would I activate them in initComplete from the data in stateLoadParams?
Use
this.api().button('.myClass').active();
.this.api()
is available in most, if not all, callbacks to access the API as the variable assigned to the initialization,table
for example, is not ready to be used.Use a global variable to store the active state retrieved from
stateLoadParams
.See this example:
http://live.datatables.net/zibabete/4/edit
Note the change from
table.draw()
todt.draw()
in the button function. This is for the same reason as described above.Kevin
Understood! Thank you so much @kthorngren Just out of curiosity - why the change from
table.draw()
todt.draw()
? This seems to work withtable.draw()
too.Change it back and you will see an undefined error. Its due to the variable
table
not being ready to use untilinitComplete
is done and exited.Kevin
@kthorngren I tried to apply the same approach if I use the deep linking and searchBuilder extension here. For some reason, the filter button state is not saved here as in the original test case.