Problem with fnSaveState and fnLoadState

Problem with fnSaveState and fnLoadState

smithk86smithk86 Posts: 3Questions: 0Answers: 0
edited April 2012 in DataTables 1.9
I am trying to implement fnSaveState and fnLoadState. My implementation is a near-exact replica of the examples in the documentation. When the functions are in place, the cookie-based state saves and loads perfectly; the table draws correctly. Once I enable fnSaveState and fnLoadState, the state saves and loads correctly from the server but the table fails to draw correctly. When the state returned is false (the first time DataTables is called), the table draws correctly. Once a state object is returned and the DataTables attempts to implement the state, two column headers get drawn (one scrolls left and right with the table and the other is fixed) and the JavaScript console throws a "o.aoColumns[iVis] is undefined" error (line 3137 of jquery.dataTables.js; I debug with Firebug). I have examined oLoadedState and the state appears to be loaded correctly. Something is tripping up DataTables when it tried to draw the headers.

I do use ColVis with some columns invisible by default. Thinking maybe ColVis was causing the problem, I tried disabling ColVis and setting all the columns to visible default to no avail.

I've been banging my head on this for 24 hours...any thoughts? I am using v1.9.1

Replies

  • allanallan Posts: 61,775Questions: 1Answers: 10,112 Site admin
    Can you link me to your page please?

    Allan
  • smithk86smithk86 Posts: 3Questions: 0Answers: 0
    I was afraid I would get that question...

    The table resides on a secure intranet page which contains data that my ISO probably wouldn't want me posting to the world. Would emailing you some images suffice?
  • allanallan Posts: 61,775Questions: 1Answers: 10,112 Site admin
    To be honest in this case I doubt it. It sounds like a bit of tinkering is going to be required. YOu can send it only to me if that is acceptable? (click on my name above and then 'send message'). One option option that might help is the debugger ( http://debug.datatables.net ) - again you might want to send that to me direct since it includes the data in the table, but I suspect for this one, I'll need to see the table.

    Allan
  • smithk86smithk86 Posts: 3Questions: 0Answers: 0
    Well, I solved my own problem. Here is what was going on:

    Created a fnStateLoaded callback which was just "function(o,oData){console.log(JSON.stringify(oData));}". I examined the returned JSON string with and without my custom fnStateLoad and fnStateSave callbacks. What did I find? The state returned by my server had quotes around all of the int datatypes - they were no longer integers.

    Because of how my server-side code retreives and sends the state back, it made it easier in fnSaveState to do [quote]data: { 'state': oData }[/quote] and encapsulate the oData object into its own variable on the webserver. Because of this, I needed to do [quote]data: { 'state': JSON.stringify( oData ) }[/quote] in fnStateSave, and a [quote]json_decode($oData)[/quote] once PHP retrieved the data. This way, the server receives a JSON string and the server turns it back into an object and stores that object in the session variable.

    When fnLoadState is called, the server gets the PHP object, runs json_encode and prints the json string for jQuery to interpret.

    WIthout comparing the two JSON strings side by side...it looked like they were the same...

    The joy of coding! Thanks again for your help and an awesome tool for anyone dealing with gobs of data!
  • allanallan Posts: 61,775Questions: 1Answers: 10,112 Site admin
    Oh man - well found, and thanks for letting us know what the problem was!

    At some point I really want to do a debug version of DataTables which will just go completely overboard with the amount of trace and debug data to catch exactly this sort of thing. One day...

    Great to hear you've got it going now :-)

    Allan
This discussion has been closed.