Can I specify cookie name for state saving?
Can I specify cookie name for state saving?
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?
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?
This discussion has been closed.
Replies
"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
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
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'!
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
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.
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
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?
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
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?
Allan
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