State Restore - looping through states and filter data
State Restore - looping through states and filter data
I'm using State Restore and want to loop through the saved states and collect data from the rows that appear when each state is loaded. The approach I'm currently using works but is complicated. It is roughly as follows:
- Save the current state to a temporary state.
- Loop through the existing saved states, load one at a time and listen for the draw to complete as each state is applied, then grab the resulting rows and get the data I need and save that data off to a list.
- Restore the saved temporary state.
- Remove the temporary state.
- Switch to another screen to use the collected data (in this case to generate charts based on the user's saved states all at once).
It all happens quickly enough that you can't tell as a user which is good. That said, it is complicated and I was hoping I could simply loop through the saved states and ask for the filtered data for each state without affecting the current table. Is it possible and I've missed a simple way to do this? If I could call something like filter() on the state that would massively simplify things but from what I can tell that's not currently possible.
https://datatables.net/reference/api/filter()
One other thing I wanted to ask is if there's a way to add a confirmation modal to the "Remove All" for states. By default there's a confirmation modal when you try to remove a single state which seems reasonable but there's no confirmation modal if you remove all of the states at once which seems odd given it's potentially far more destructive. A confirmation modal by default would be great. I couldn't figure out a simple way to add one in manually.
https://datatables.net/extensions/staterestore/examples/initialisation/removeAll.html
Thanks.
Ben
Answers
One option might be to use
stateSaveParamsto save the desired filtered data set with the state. Then you can loop through with eitherstateRestore.states()orstateRestore.activeStates()and grab the saved filtered data without needing to load the states. Similar to this example. The example doesn't run properly and throws this error:@allan will need to fix the error.
The
removeAllStatesstates this:One option might be to create a custom button and use
stateRestore.states().remove()setting theskipModalparameter totrue.Kevin