Local storage expiration?

Local storage expiration?

bigtunacanbigtunacan Posts: 3Questions: 1Answers: 0

I am using the ColReorder extension with datatables and if a user goes several days without using the site, when they return to the page the column ordering has returned to it's default ordering.

I see that the column ordering is being saved to local storage in the data tables JSON string such as this.

"ColReorder": [ 0, 3, 1, 2, 4 ]

I also noticed there is a a "time" field

"time": 1463413394826

Is this time field being used to "expire" the entry? If so, how long is the entry valid for before it expires, and is there a way that I can override this to a longer period of time?

This question has an accepted answers - jump to answer

Answers

  • scholarlylabsscholarlylabs Posts: 16Questions: 0Answers: 2

    Assuming this is being saved to local storage via stateSave (https://datatables.net/examples/basic_init/state_save.html), the duration can be set with stateDuration (https://datatables.net/reference/option/stateDuration)

  • bigtunacanbigtunacan Posts: 3Questions: 1Answers: 0

    So we are using the stateSave setting as described in the doc.

    $("#myobj").DataTable({
    stateSave: true
    })

    We are not setting stateDuration though. The doc you linked to I see says state duration defaults to 7200 (2 hours), but this doesn't match up with the behavior I'm seeing. I have state that persist much longer than this, and then at times the state is just no longer persisted in such a way as to appear a bit random.

    In the jquery.dataTables.js file for the version I'm running I see on line 10699

    "iStateDuration": 7200, // This is inside DataTables.defaults

    but then later on line 13015 I see

    "iStateDuration": 0, // This is inside DataTable.models.oSettings

    My understanding of the documentation is that an iStateDuration of 0 means to never expire the state. I'm confused looking at the source code however what the value of iStateDuration really is when I am not setting it.

  • scholarlylabsscholarlylabs Posts: 16Questions: 0Answers: 2

    I can't comment on the defaults, the behavior you've noted, or the application source code (maybe someone else can), however, if you haven't done so you might try defining the stateDuration to see if it impacts the behavior and the "time" field.

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Answer ✓

    Is this time field being used to "expire" the entry?

    Yes - as @scholarlylabs says, stateDuration controls this.

    The value in the model is more or less irrelevant - the value in the defaults is what is set when the table is created. If you want to check the value for a running table, use the private property: $('#myTable').DataTable().settings()[0].iStateDuration on your console.

    Unlike a cookie, the localStorage value won't automatically remove itself after the set amount of time - it will still be present, its just that DataTables internal logic will have it ignore the value.

    If that isn't working for you, please link to a test case showing the issue.

    Allan

  • bigtunacanbigtunacan Posts: 3Questions: 1Answers: 0

    @allan, Thanks. Being able to access the state duration like you described was helpful. I was able to confirm it is using the default two hours and it just appears that the time stamp is being updated on page visits/refresh which I believe is why this is appearing to be so random. Many of our users spend a long time on our page and/or revisit multiple times throughout the day which is updating the timestamp; then when they come back after not having visited for a some period of time the column ordering is lost.

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    it just appears that the time stamp is being updated on page visits/refresh which I believe is why this is appearing to be so random

    Yes. It is two hours from the last state change, rather than two hours from when the table was first created.

    Allan

This discussion has been closed.