State Restore - looping through states and filter data

State Restore - looping through states and filter data

bg7bg7 Posts: 100Questions: 14Answers: 0

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:

  1. Save the current state to a temporary state.
  2. 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.
  3. Restore the saved temporary state.
  4. Remove the temporary state.
  5. 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

  • kthorngrenkthorngren Posts: 22,413Questions: 26Answers: 5,153

    One option might be to use stateSaveParams to save the desired filtered data set with the state. Then you can loop through with either stateRestore.states() or stateRestore.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:

    Uncaught TypeError: minEl.val is not a function

    @allan will need to fix the error.

    One other thing I wanted to ask is if there's a way to add a confirmation modal to the "Remove All" for states.

    The removeAllStates states this:

    No confirmation modal is shown as default when using this button.

    One option might be to create a custom button and use stateRestore.states().remove() setting the skipModal parameter to true.

    Kevin

Sign In or Register to comment.