StateSave on active buttons

StateSave on active buttons

trongarttrongart 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

  • kthorngrenkthorngren Posts: 21,334Questions: 26Answers: 4,951

    Use stateSaveParams and stateLoadParams to manipulate custom data to save. I would look at using button().active() to get the active state of the button in stateSaveParams and save it. Then in stateLoadParams get the button state.

    My first thought is to use button().trigger() if the button is active in stateLoadParams 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 in initComplete trigger the button if active.

    Kevin

  • trongarttrongart Posts: 222Questions: 51Answers: 0
    edited December 2021

    @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.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    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

  • trongarttrongart Posts: 222Questions: 51Answers: 0

    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?

  • kthorngrenkthorngren Posts: 21,334Questions: 26Answers: 4,951
    Answer ✓

    nclear what the correct syntax is to get the button active states into stateSaveParams based on the button classes.

    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.

    How would I activate them in initComplete from the data in stateLoadParams?

    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() to dt.draw() in the button function. This is for the same reason as described above.

    Kevin

  • trongarttrongart Posts: 222Questions: 51Answers: 0

    Understood! Thank you so much @kthorngren Just out of curiosity - why the change from table.draw() to dt.draw() ? This seems to work with table.draw() too.

  • kthorngrenkthorngren Posts: 21,334Questions: 26Answers: 4,951

    Change it back and you will see an undefined error. Its due to the variable table not being ready to use until initComplete is done and exited.

    Kevin

  • trongarttrongart Posts: 222Questions: 51Answers: 0
    edited December 2021

    @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.

Sign In or Register to comment.