All State Restore Saved States break if a column is added to a table

All State Restore Saved States break if a column is added to a table

desperadodesperado Posts: 159Questions: 33Answers: 4

I suspected this would happen based on some of my observations about how State Restore behaves and with some testing I think I am correct in stating that all of a users "Saved States" stop working if a new column is introduced to a table.

Here is a simple test case to see the issue.
http://live.datatables.net/gimanevi/1/edit?html,js,output

Test scenario
1) Open the test case and save a few states
2) Uncomment out the three sections that say "Uncomment after creating a saved state"
3) Try to switch to one of your saved states

This means any changes to my table Add Column, Remove Column and likely Rename Column would result in a user having to create all of their views again from scratch.
Since I am using this in combination with Search Builder that could be considerable work for every user.

Would there be a way to fix this so it recognizes the column is new and does some default behavior to allow the saved state to work. Then an update would include the new column making the saved state "correct" again.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,177Questions: 1Answers: 2,590

    We'll have a think and report back towards the end of next week,

    Colin

  • sandysandy Posts: 913Questions: 0Answers: 236

    Hi @desperado ,

    Unfortunately, as it is with the standard stateSave, this isn't something that is supported. The states have no way of telling which columns have been added or removed, or where they have been added or removed. Same goes for renaming. Because of this it is not possible to reimplement a state, so it resets back to the beginning.

    Thanks,
    Sandy

  • desperadodesperado Posts: 159Questions: 33Answers: 4

    I expected this answer because of the fact that stateSave uses column index and not column name. That is a big issue for my system I have thousands of users I can't make any column changes if I will break everyone's saved states. I may use stateRestore for our admin data tables because they are internal users and a small group but I think this is a show stopper for my external users. That is so unfortunate, I really liked this feature and spent considerable time getting it installed.

    Perhaps in the future it would be possible to plug in a version of stateSave which uses column names instead of index ?

    Would it be possible to NOT break views if we followed the rule of only adding columns at the end of the table? That would mean the current states are all valid.

    I can't really think of any other ideas to solve this. What a shame :(

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794
    Answer ✓

    You could use stateLoadParams to apply the saved state as you see fit when the column count changes. I updated your example to show this.
    http://live.datatables.net/sinopafu/1/edit

    If the configured column count is greater than the saved the saved order will be applied. To test apply sorting to one of the and save the state. Then follow your above steps. The table order will be reapplied after the additional column is added.

    You can do the same with the paging and search settings. Depending on your config you can probably work out a way to compare the configured columns with the saved columns to determine if one or more columns were removed or inserted. In this thread the OP wanted ColReorder to work if the number of columns changed.

    Kevin

  • desperadodesperado Posts: 159Questions: 33Answers: 4

    @kthorngren Thanks, that's an interesting solution. Another solution I am considering is altering the saved states in the database. Since I am using ajax and storing the data in the database, I have access to all the saved states and could write some db updates or even a little program to alter them with the new column.

    So there are solutions, it just means adding a column to a table is going to require much more effort now if StateRestore is in use. I guess that is reasonable.

    Thanks!

Sign In or Register to comment.