Reload From Cookies

Reload From Cookies

matthttammatthttam Posts: 40Questions: 2Answers: 0
edited November 2012 in DataTables 1.9
I just realized today that fnStateLoadCallback is no more. This explains the bug that my program is experiencing when reloading cookies.

I'm using code snippets from this example in my production environment:
http://datatables.net/forums/discussion/6071/solved-individual-column-filters-not-being-restored-with-bstatesave/p1
Specifically, the last post before it was closed. I did some searching and came across this: http://datatables.net/blog/localStorage_for_state_saving

So, I changed all my fnStateLoadCallback to fnStateLoad and now it hates me...

As long as a filter has been changed, restore filters works as expected. however, if you change filters and refresh. Then DONT change filters and refresh it loses the filters. This is probably something simple. If my code were identical to the one linked prior (as far s the reloaded cookie stuff) what would I change to make it work with 1.9?

Thanks for the help again :-)

Bil

Replies

  • matthttammatthttam Posts: 40Questions: 2Answers: 0
    edited November 2012
    OK, I fixed the code the way I needed to but I still have that bug. I think it may be because I don't fully understand how it WAS working back in 1.8. Here is my code:
    [code]
    // ... My global variable I am working on to make customizing the tables easier ... //
    var varLookup = {'#WO':
    {
    'obj':'wTable',
    'dataPath':'dt/scripts/WO_Processing.php',
    'uID':'1000',
    'tabID':'tabs-1',
    'redrawTimer': '4', //In minutes
    'deferTimer': '350', //In Milliseconds
    'asInitVals': new Array(),
    'sPreviousSearch':{},
    'isReloadedFromCookies':true
    },
    '#Assignments':
    {
    'obj':'oTable',
    'dataPath':'dt/scripts/Assignment_Processing.php',
    'uID':'1001',
    'tabID':'tabs-2',
    'asInitVals': new Array(),
    'sPreviousSearch':{},
    'isReloadedFromCookies':true
    },
    '#computer':
    {
    'obj':'cTable',
    'dataPath':'dt/scripts/Computer_Processing.php',
    'uID':'1002',
    'tabID':'tabs-3',
    'asInitVals': new Array(),
    'sPreviousSearch':{},
    'isReloadedFromCookies':true
    },
    '#student':
    {
    'obj':'sTable',
    'dataPath':'dt/scripts/Student_Processing.php',
    'uID':'1003',
    'tabID':'tabs-4',
    'asInitVals': new Array(),
    'sPreviousSearch':{},
    'isReloadedFromCookies':true
    }
    };

    // ... Snippet from datatables initialization ... //
    "fnDrawCallback": function(oSettings) {
    build_fnDrawCallback(oSettings, this);
    },
    "fnStateLoaded": function(oSettings) {
    build_fnStateLoadCallback(oSettings, this);
    },
    // ... Functions seen above ... //
    function build_fnStateLoadCallback(oSettings, that){
    varLookup[that.selector].isReloadedFromCookies = true;
    return true;
    }

    function build_fnDrawCallback(oSettings, that){
    if (varLookup[that.selector].isReloadedFromCookies == true) {
    varLookup[that.selector].isReloadedFromCookies = false;
    restoreFilters(oSettings, that);
    }
    return true;
    }
    /* This should go through each th in the tfoot section and determine if the child is a select or input
    * field and set them accordingly. Ignore the special case of PS which is a path selector.
    */
    function restoreFilters(oSettings, that) {
    $(that.selector+' tfoot th').each(function(i) {
    index = that.fnVisibleToColumnIndex(i); theFilter = $(this).children('select, input');
    if (oSettings.aoPreSearchCols[index].sSearch.length > 0) {
    if (theFilter.attr('ID') == 'PS'){
    theFilter.pathSelector("value",oSettings.aoPreSearchCols[index].sSearch);
    }else if(theFilter.is('select')){
    theFilter.find("option:contains('" + oSettings.aoPreSearchCols[index].sSearch + "')").prop("selected", "selected");
    }else if(theFilter.is('input')){
    theFilter.val(oSettings.aoPreSearchCols[index].sSearch);
    theFilter.removeClass('search_init');
    }
    }
    });
    };
    [/code]

    So, the symptom is that as long as I make a change to a filter the reload will reload the cookies for that table. If however I fail to make a change to a table's filters then upon refresh the table has no filters. aoPreSearch will be filled with sSearch of empty strings.

    Thanks again for helping so much. And once again FREAKING LOVE datatables.
  • matthttammatthttam Posts: 40Questions: 2Answers: 0
    edited November 2012
    If I beat my head against the code long enough it seems I always figure it out :-/... sorry about posting and answering my own question but perhaps it will help somebody else somewhere.

    I think what I need to do here is to save aoPreSearchCols into localstorage. Then on fnStateLoad set oSettings.aoPreSearchCols accordingly. Then on fnStateLoaded it will actually do the stuff it is supposed to do with it.
    [code]
    // ... don't forget your b State Save ... //
    "bStateSave": true,
    // ... //
    "fnStateSave": function (oSettings, oData){
    localStorage.setItem('DataTables_aoPreSearchCols_'+this.selector, JSON.stringify(oSettings.aoPreSearchCols))
    },
    "fnStateLoad": function (oSettings){
    aoPre = JSON.parse( localStorage.getItem('DataTables_aoPreSearchCols_'+that.selector));
    if(aoPre != null){
    oSettings.aoPreSearchCols = aoPre;
    }
    },
    "fnStateLoaded": function(oSettings) {
    build_fnStateLoadCallback(oSettings, this);
    },
    [/code]

    This seems to be working. I am going to break those commands out into functions like usual to make the loop easy to understand... but this seemed to fix it. Note I am using this.selector as the unique part of localStorage. this.selector is the tableID as long as you are initializing datatables like $('#tableID').dataTables({}).

    Thanks though. I think explaining my problem to the internet tends to help me figure out the answer. Trust me, no one at the office wants to listen :-)

    Bil
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Good to hear you've got a handle on this now - sorry for the delay in replying to your post.

    You are quite right that state saving changed significantly in 1.9 (it was darn ugly in 1.8-...). This blog post states some of the motivations for changing the API: http://datatables.net/blog/localStorage_for_state_saving .

    localStorage is a good way to go I'd say. Indeed v1.10 of DataTables is going to drop the use of cookies for state storage (it can still be done via the API, but it just is no longer the default), with localStorage being used in preference.

    Allan
This discussion has been closed.