Can I specify cookie name for state saving?

Can I specify cookie name for state saving?

twistahtwistah Posts: 5Questions: 0Answers: 0
edited April 2009 in General
In my application the same datatable is used for different URIs. (yeah I know...) Is it possible to force DataTables to use a unique name for the cookie so that filtering/sorting state will work across different URIs that in fact displays the same table?

Right now, it seems like DataTables uses the URI as the cookie name, but could I, for instance, force it to use the dom ID for the table?

Replies

  • allanallan Posts: 63,407Questions: 1Answers: 10,452 Site admin
    DataTables does indeed use the URL (mainly for the path location - IE has issues as it is meant to be done...), but it also contains the DOM ID in it. It shold be in the format:

    "SpryMedia_DataTables_"+ID+"_"+path

    If no id is used for the table, then DataTables will use a unique number (unique to that page).

    So perhaps the easiest way is to alter your ID using some server-side code. Alternatively, you could modify the DataTables initialiser where it sets "oSettings.sInstance".

    Hope this helps,
    Allan
  • twistahtwistah Posts: 5Questions: 0Answers: 0
    Thanks Allan!

    Your suggestion works great!

    I modified the "_fnCreateCookie" and "_fnReadCookie" functions with the following modifications:

    In "_fnCreateCookie" commented out
    [code]
    sName += '_'+window.location.pathname.replace(/[\/:]/g,"");
    [/code]

    and in "_fnReadCookie" I changed

    [code]
    var sNameEQ = sName +'_'+ window.location.pathname.replace(/[\/:]/g,"") + "=";
    [/code]

    to

    [code]
    var sNameEQ = sName + "=";
    [/code]

    Now it seems to work as intended. Any pitfalls with this solution you can see? As I understand it, tables with the same DOM ID will share the same cookie state across the application, even on different URIs. But that is really not a concern for me.

    Thanks again for great help!

    :)

    /P
  • allanallan Posts: 63,407Questions: 1Answers: 10,452 Site admin
    Hi P
  • switteswitte Posts: 1Questions: 0Answers: 0
    Hi,

    I'm using datatables in a WebSphere Portal 5.0 environment. (Hopelessly old, i know) and also had problems with saving the state of a table. The solution given by Twistah (removing the path name) solved our problem. The url's in a WebSphere Portal 5.0 are really weird and change all the time.

    Perhaps it should be a configurable option to use the path name in the cookie name.

    I don't know yet how the state saving behaves in a 'modern' (Websphere) Portal environment.

    Anyway, thanks for the great 'DataTables'!
  • allanallan Posts: 63,407Questions: 1Answers: 10,452 Site admin
    Hi switte,

    The cookie name isn't particularly configurable through the normal initialisation object, but you could go into the source and change it to whatever you want. The cookie name that DataTables uses is made up of: SpryMedia_DataTables_"+oSettings.sInstance . The first part of course is a literal string, where I've tried to avoid any name clashes with anything else, and the second part is either the DOM ID of the table, if it has one, or the table initialisation number if it does not.

    I'm not overly happy with the way DataTables handles cookies at the moment - there are a couple of issues here and there (like the path you mentioned), and I plan to look at this at some point soon. Of course, if the URLs keep changing - then that's not going to help...

    Regards,
    Allan
  • wacawaca Posts: 12Questions: 0Answers: 0
    Hiya--

    Just ran into a different snag with the cookie saving state -- a bit obscure but it happens! -- running a demo of an app that uses DataTables (if we actually get any $$$ from it I will be able to donate! ;) ), and when exiting one user and bringing up another there are a different # of rows in the DataTable -- at this point the saved state causes a problem:

    One saved state was on page two showing 15 entries per page, and the subsequent user only had 11 entries to show -- so it had a blank grid with the text "showing 16-11 of 11" on the left (or something similar)

    In a nutshell: the saved state didn't auto-correct itself

    I imagine this problem could happen in many other circumstances (now that I think about it) such as saving state with 16 rows and then coming back at a later time when there are fewer rows and having the same problem outlined above. I would expect it to check that it couldn't fulfill the saved state properly and simply go back to the earliest possible page# (or just page1) and correct the "showing 1-11 of 11" text to fit.

    So in a way -- now that I typed this all out -- it's kind of a bug report as well as a request for the cookie name, which is what I was really wanting to ask:


    A simple thing I would request for the cookie issue would be an option for the data table such as "sCookieSuffix" or something where you could add a little extra text to the cookie you do above and that way the app could save state for different users (or whatever) by adding their own text to the cookie name. Things like "uid1234tablenameXYZ" -- whatever.

    Just a thought. :)


    Thanks again for creating such an awesome piece of work. Cheers.
  • allanallan Posts: 63,407Questions: 1Answers: 10,452 Site admin
    Hi waca,

    Nice one - thanks for the information. I fully agree on both accounts:

    1. Rolling back to the start when the trying to display more records than there are - fix for this will be in the soon to be released 1.6.2

    2. Cookie prefix name - I'll add an option for this to 1.7 which shouldn't (hopefully!) be too far away. Until then, what you can do to name the cookie in a unique manner is use the ID of the table. DataTables uses this in the cookie name, so this could be used to make it unique for each user.

    Good luck with the demo.

    Regards,
    Allan
  • tapsboytapsboy Posts: 11Questions: 0Answers: 0
    I have a similar problem. Is there currently an option for sCookieSuffix or something equivalent.

    On my server-side PHP framework, the following URLs all present the same page
    http://localhost/users
    http://localhost/users/
    http://localhost/users/index

    but in each case, it creates a new cookie with names like:
    my_DataTable_users-grid_users
    my_DataTable_users-grid_
    my_DataTable_users-grid_index

    Is there a way that I can invoke the same cookie without making any code changes to the library?
  • allanallan Posts: 63,407Questions: 1Answers: 10,452 Site admin
    Yup I see the problem. That's annoying :-(. The reason for using the path as part of the cookie is a daft IE bug - it should really be needed - but there we have it...

    The basic answer I'm sorry to say is no - there currently is no way of specifying the full cookie name. This is a bit of code that needs to be overhauled a good bit I think...

    Sorry I don't have better news.

    Allan
  • tapsboytapsboy Posts: 11Questions: 0Answers: 0
    Thanks for the reply. I also observed another thing.

    When I use fnCookieCallback to modify the cookieName like this:

    [code]
    'fnCookieCallback': function(sName, oData, sExpires, sPath){

    sName = 'my_DataTables_' + sGridName;

    return sName + "="+JSON.stringify(oData)+"; expires=" + sExpires +"; path=" + sPath;
    }
    [/code]

    it does create the cookie with the intended name, but when the page refreshes and cookie data is restored, it doesn't seem to use the modified name, but instead looks for the one with the standard format.

    Is there a way to use fnStateLoadCallback to force it to use the cookie name I gave it earlier?
  • allanallan Posts: 63,407Questions: 1Answers: 10,452 Site admin
    Yeah that's what I was getting at earlier - there isn't a way to set the cookie name and have DataTables read the same cookie. You can certainly set the name as you point out, but there is currently no way of overriding the read. A bit of an oversight that, a situation I have never expected to be customised. So I'm afraid the point still stands, without modifying the core there currently isn't a way to set the whole cookie name.

    Allan
  • allanallan Posts: 63,407Questions: 1Answers: 10,452 Site admin
    Hmmm - having said that I was just looking at the code. I put a change in for the 1.9 series which means that this actually is now possible... You can use fnStateLoadCallback to define a function that will read your cookie and then use the second parameter that is passed in to pass back the value from the cookie (parsed as a JSON object - also make sure to pass it by "reference" (i.e. $.extend the object, rather than just assigning it).

    Its a bit nasty since you will need to read the cookie yourself, but I retract what I previously said! It is actually possible now :-)

    Allan
This discussion has been closed.