stateSave in 1.10
stateSave in 1.10
pablop
Posts: 19Questions: 3Answers: 0
Hi
There are duplicate entries in the manual for stateSave
https://next.datatables.net/reference/option/
What happens if there is settings of a table changed while there is a saved state?
For example, adding columns and filters. Will the state be cleared or will it confuse loading the state?
Can saveState handle multiple tables on the same page?
Does saveState also save a "unique" signature of the table?
Maybe serialize the table settings and include the container id if available or let the user set a table unique id.
This can help in the above cases.
If I have 2 pages loaded from the server and the table state was saved at page 2.
Now I'm deleting enough items so the table fits only in 1 page.
Will saveState be smart enough to show me the first page or will it show empty results?
Thanks
There are duplicate entries in the manual for stateSave
https://next.datatables.net/reference/option/
What happens if there is settings of a table changed while there is a saved state?
For example, adding columns and filters. Will the state be cleared or will it confuse loading the state?
Can saveState handle multiple tables on the same page?
Does saveState also save a "unique" signature of the table?
Maybe serialize the table settings and include the container id if available or let the user set a table unique id.
This can help in the above cases.
If I have 2 pages loaded from the server and the table state was saved at page 2.
Now I'm deleting enough items so the table fits only in 1 page.
Will saveState be smart enough to show me the first page or will it show empty results?
Thanks
This discussion has been closed.
Replies
That's odd! Thanks for pointing it out.
> What happens if there is settings of a table changed while there is a saved state?
Not sure I understand I'm afraid - if the sort, for example, is changed, then the saved state will be updated.
> Can saveState handle multiple tables on the same page?
Yes.
> Does saveState also save a "unique" signature of the table?
No - it just uses the id. However, if the column count changes, it throws away a saved state, since all bets are off at that point!
> Will saveState be smart enough to show me the first page or will it show empty results?
It will show the first page, since your paging will reset when you call fnDeleteRow / rows().remove().
Allan
By change to the table settings, I mean changing the table definition.
For example, I upload a new version of my website where I change the order of the columns.
If I have a select filter on the first column and a text field filter on the second and now I'm reloading the page and the order of columns changes, it might confuse loading the state.
That's why I asked if it serializes the table definition to make sure it wasn't changed.
[quote]It will show the first page, since your paging will reset when you call fnDeleteRow / rows().remove().[/quote]
I'm not calling rows.remove().
user1 navigates to page 2 of the users table.
user2 deletes enough users from the database so the results fits a single page.
user1 refreshes the browser.
The state tries to display page 2 but there is only 1 page of results.
What I would expect in this case is that datatables will display the first page.
I would expect that as well but I'd need to test it. Part of the problem there is that DataTables doesn't know that the data no longer exists, so the server needs to tell it. I suspect that it might not actually work at the moment, but I'd need to do some work on it.
> That's why I asked if it serializes the table definition to make sure it wasn't changed.
I understand now. The only check it makes is to see if the column count has changed. Reordering of the columns isn't taken into account, but probably should be. I will look into adding that.
Allan
https://next.datatables.net/reference/option/stateSave
The default of stateDuration is missing from the doc:
https://next.datatables.net/reference/option/stateDuration
Is it possible that stateDuration=0 will mean - use SessionStorage instead of LocalStorage?
Thanks
Allan
Thanks
I've got a local set of bookmarks to ensure features aren't lost.
Allan
Allan
Thanks for bringing this up, I think this is a nice little feature.
Allan
I might be missing this in your commit but do you handle the situation in this comment?
https://datatables.net/forums/discussion/comment/57253#Comment_57253
If a user has a saved state and the table data and/or definition was changed on the server,
he might get stuck in a non existing page.
Thanks
Allan
Another option is defining a version property and use it in the hash or something like md5(tableDefinition).
Will hashing the tableDefinition will be that bad?
Aren't we talking about few bytes?
Another issue I mentioned in the comment is you load a state with a page but the number of records on the database changed and now this page doesn't exist anymore.
Is it possible to handle this situation?
Not really as there isn't a hashing function in Javascript. I'd need to provide one, which I'd rather not! A hash of the column titles would probably be the best way. If you do want that it is possible to implement it using the callbacks and some custom state saving.
> load a state with a page but the number of records on the database changed and now this page doesn't exist anymore.
Darn yes. I'm not doing so well here! Too many threads to keep track of in the forums these days. That's probably not a trivial fix unfortunately, since the data is loaded after the state, but I will look into it.
Allan
What about a simple version field in the table definition?
The version can be saved with the state.
When loading the state, if the version was changed, discard the state.
[quote]Darn yes. I'm not doing so well here! Too many threads to keep track of in the forums these days. That's probably not a trivial fix unfortunately, since the data is loaded after the state, but I will look into it.[/quote]
When receiving the data from the server, we can see that the saved page is larger than the number of required pages. In that case we can reload the table again with page=pages.length or page=0 or just delete the state and reload the table.
The user experience when loading the table twice might not be optimal but it's much better than showing a non-existing page without the user being able to escape it or delete the saved state.
I've created a github issue so it won't get lost again
https://github.com/DataTables/DataTables/issues/307
Yup - you can provide that information with the fnStateSaveParams callback already. You would then use the loading callbacks to disallow state retrieval if it doesn't match.
Thanks for opening the github issue.
Allan