bStateSave and multiple tables on a page

bStateSave and multiple tables on a page

doctorjwdoctorjw Posts: 12Questions: 0Answers: 0
edited April 2014 in DataTables 1.9
I'm using datatables 1.9.4 and have run into an interesting issue with bStateSave. We have a jsp which constructs a table; using bootstrap, we have a table on the page, and the data displayed alters based on the tab/link you click on. In playing with bStateSave in this situation, originally I had a single cookie, as expected, as the cookie name is only based on the page you are on, and the name of the table. I wanted to remember the state of the table based on which "tab" you are on, so I changed the naming of the table to reflect the tab. So instead of a single static name "itemTable", you'd see "itemTable_UNMAPPED_ITEMS", "itemTable_PRODUCTS", etc.

This seemed to work ... at first ... I'd enter a filter on the UNMAPPED_ITEMS tab, and then switch to other tabs, and back to UNMAPPED_ITEMS and the filtering was, indeed, remembered. If I switched to PRODUCTS and then entered a filter, though, my original UNMAPPED_ITEMS tab would lose it's remembered filtering.

In digging, I'm not seeing anything else like this. I did find this post:

http://www.datatables.net/forums/discussion/3997/problem-with-multiple-state-cookies-for-a-path/p1

which isn't quite the same situation, but similar. This pointed me to the use of "fnCookieCallback", so ... I grabbed the sample in the docs which should, I think, be an no-op:

[code]
"fnCookieCallback" : function( sName, oData, sExpires, sPath ) {
// Customise oData or sName or whatever else here
return sName + "="+JSON.stringify(oData)+"; expires=" + sExpires +"; path=" + sPath;
}
[/code]

I really didn't expect anything to change, since this should set the cookie name and data just like the default, but ... it did. Suddenly, the behavior of the state saving worked perfectly.

I also tried setting the cookie prefix via "sCookiePrefix", without the fnCookieCallback, but the cookie naming didn't change.

For now, I'm good with a custom fnCookieCallback, but it's odd that this functions correctly while the default does not. Is there some issue with the default cookie setting where a situation like mine (mild difference in cookie name?) results in the old cookie being cleared?

Thanks,

john

Replies

  • ashiersashiers Posts: 101Questions: 8Answers: 7
    Couldn't help but notice you're on the Java platform. You may be interested in knowing there is a library of classes named JED that were designed to work with DataTables on the Java platform. You might want to check it out. http://jed-datatables.ca/jed/
  • allanallan Posts: 63,576Questions: 1Answers: 10,483 Site admin
    I suspect you might be running into the 4KiB limit of cookies here. Rather than letting to cookie size grow, DataTables will delete the state of saved tables if the size has grown too large. Could you try with the 1.10 beta ( http://next.datatables.net/download ) and let me know how you get on with that? 1.10 uses localStorage which is far more efficient and doesn't suffer from this limitation. The only down side is that it doesn't work in IE6/7 (...).

    Allan
  • doctorjwdoctorjw Posts: 12Questions: 0Answers: 0
    Hmmm... I checked the length of the cookie and it's less than 2k (contents, anyway). It's really just the basic table state, nothing added. If the 4k limit is for the total of all cookies, I could see this (it does seem to retain just 2 cookies at a time, each right around 2k). So maybe datatables isn't quite doing the right thing with sizing the cookies?

    I can test with 1.10 beta, but it will be just a test --- this is supposed to go into production next week. It will take me a bit, as I need to pull in the new bootstrap 3 pagination (and rip out the stuff I originally plugged in). I'll let you know when I get the time to play with it.

    Thanks! --- john
This discussion has been closed.