bStateSave cookie: when does this reset?

bStateSave cookie: when does this reset?

aluferrarialuferrari Posts: 22Questions: 1Answers: 0
edited January 2012 in General
Hi,

We have a problem regarding bStateSave cookie. We have used bStateSave=true. The feature is working fine, but it works a little too much.
It remembers the state even after the user logs out of the application. So the next time when user again visits the page, the settings are still there.

Q1. When does the cookie gets deleted/reset?
Q2. Is there a way to manually delete/reset the cookie?

Thanks a lot in advance.
- Aalap

Replies

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    > Q1. When does the cookie gets deleted/reset?

    When the cookie expires. That is controlled by iCookieDuration .

    > Q2. Is there a way to manually delete/reset the cookie?

    Not really - you could work out the name of the cookie and delete it I suppose. Or your could stop the saved state from loading, but if you want a session only cookie, then you would either need to modify the DataTables core, or look at writing your own method to do the state storage. This blog post might interest you: http://datatables.net/blog/localStorage_for_state_saving

    Allan
  • aluferrarialuferrari Posts: 22Questions: 1Answers: 0
    Thanks Allan. Is there a way to set the state saving for session duration only?
    We are looking for a way to keep the state saved till active session only or may be till the window is closed.

    Please help.
    Thank you.
    - Aalap
  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    Currently no - there is no way of doing this. You would either need to define your own state saving method, or modify the DataTables core.

    Allan
  • JohnnyKJohnnyK Posts: 2Questions: 0Answers: 0
    It would be nice to see the year on some of these posts.

    Cheers!
  • JohnnyKJohnnyK Posts: 2Questions: 0Answers: 0
    Ah.. I see... I think the year only appears if it's not in the current year.. Got it.
  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    Yup - that's it :-)
  • bennybenbenbennybenben Posts: 1Questions: 0Answers: 0
    edited May 2012
    Awesome. Building off allan's example for using local storage it's pretty easy to have table view settings persist and add a reset button to clear everything out and go back to defaults.

    [code]
    function save_dt_view (oSettings, oData) {
    localStorage.setItem( 'DataTables_'+window.location.pathname, JSON.stringify(oData) );
    }
    function load_dt_view (oSettings) {
    return JSON.parse( localStorage.getItem('DataTables_'+window.location.pathname) );
    }
    function reset_dt_view() {
    localStorage.removeItem('DataTables_'+window.location.pathname);
    }
    [/code]

    so my tables are loaded with
    [code]
    ...
    "bStateSave": true,
    "fnStateSave": function(oSettings, oData) { save_dt_view(oSettings, oData); },
    "fnStateLoad": function(oSettings) { return load_dt_view(oSettings); },
    ...
    [/code]

    and then I just hook the reset function to a button on the page just below the table which deletes the settings and refreshes the page. all done.
  • aluferrarialuferrari Posts: 22Questions: 1Answers: 0
    Thanks bennybenben !
  • kshippkshipp Posts: 6Questions: 0Answers: 0
    Thanks bennybenben - this worked for me too! For some reason, the bStateSave stopped working for me and I couldn't find any resolutions on here, until yours. :)
  • TokinokiTokinoki Posts: 1Questions: 0Answers: 0
    edited October 2012
    Use sessionStorage instead of localStorage and your good ;)
  • mruivomruivo Posts: 7Questions: 0Answers: 0
    Thank you! I was resetting the view by removing the cookie(s), but I like this method much, much better.
  • Popp1nFreshPopp1nFresh Posts: 1Questions: 0Answers: 0
    edited December 2012
    Allen's code and Ben's idea of a reset button to what I am using currently. I added a portion of the Session ID to the end of the string within localStorage.get/setItem(...) method. Note: The exact code below would only work in ASP.NET but may be applicable elsewhere.

    [code]
    "fnStateSave": function ( oSettings, oData ) {
    localStorage.setItem( 'DataTables'
    + window.location.pathname
    + '<%=Session.SessionID.Substring(1,3)%>',
    JSON.stringify( oData ) );
    },

    "fnStateLoad": function ( oSettings ) {
    return JSON.parse( localStorage.getItem( 'DataTables'
    + window.location.pathname
    + '<%=Session.SessionID.Substring(1,3)%>' ) );
    }
    [/code]

    The window.location.pathname makes sure that multiple tables on differing pages use only their settings and the appended chunk of Session ID makes sure the storage is only read for the current session, unless of course the user manages to get the exact same chunk as before.

    Another note: I have not yet looked into the implications of creating a new, local, file every time the user starts a new session. Does the browser delete it on its own or will this cause files to build up overtime?

    EDIT: After a quick test in Chrome the local storage files pile up as the session changes. So do this at your own risk!
This discussion has been closed.