stateSave in 1.10

stateSave in 1.10

pabloppablop Posts: 19Questions: 3Answers: 0
edited February 2014 in DataTables 1.10
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

Replies

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    > There are duplicate entries in the manual for stateSave

    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
  • pabloppablop Posts: 19Questions: 3Answers: 0
    [quote]Not sure I understand I'm afraid - if the sort, for example, is changed, then the saved state will be updated.[/quote]

    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.
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    > 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
  • pabloppablop Posts: 19Questions: 3Answers: 0
    This page mentions cookies instead of LocalStorage
    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
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    stateDuration used to indicate sessionStorage is a very nice idea. I think it should probably be -1, and 0 can mean infinite (it doesn't at the moment - I need to fix that). It does unfortunately mean that a small update is needed for the public API, so let me have a little think before I implement. I'll correct the docs at the same time.

    Allan
  • pabloppablop Posts: 19Questions: 3Answers: 0
    I didn't see changes in github related to the suggested changes. Do you have it in a list of requested features so it won't get forgotten?

    Thanks
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    I've not committed this feature yet, as it still has a dependency on me reducing the overall size of DataTables. Basically DataTables 1.10.0 must be 75K or less - that is my goal. With its current feature set it is 75.5K - thus I need to reduce it in size, before anything else can be added.

    I've got a local set of bookmarks to ensure features aren't lost.

    Allan
  • pabloppablop Posts: 19Questions: 3Answers: 0
    This feature didn't make it to 1.10.0?
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    1.10.0 hasn't yet been released, and no it hasn't been included yet. I will take a look at it again tomorrow, I did want to get this in.

    Allan
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    Committed here: https://github.com/DataTables/DataTablesSrc/commit/6b847666f0a :-).

    Thanks for bringing this up, I think this is a nice little feature.

    Allan
  • pabloppablop Posts: 19Questions: 3Answers: 0
    Thank you for implementing this.

    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
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    The only check DataTables makes is on the number of columns. If they are different then the old state is thrown away. If they are the same the old state is applied. Beyond using the id I'm not sure that much else could be done without hashing a whole lot of information.

    Allan
  • pabloppablop Posts: 19Questions: 3Answers: 0
    Changing the id is a good idea but it might complicate other stuff that might depend on it in the app.
    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?
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    edited April 2014
    > Aren't we talking about few bytes?

    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
  • pabloppablop Posts: 19Questions: 3Answers: 0
    [quote]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.[/quote]

    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
  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    > What about a simple version field in the table definition?

    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
This discussion has been closed.