stateSave gets ignored "sometimes"

stateSave gets ignored "sometimes"

btreebtree Posts: 99Questions: 14Answers: 11

Hi,

I got a strange behavior with stateSave option and I do not know how to debug it or find the problem or solution. Randomly my stateSave ignores the stateLoadCallback and I get the standard table settings saved.

inti the table:

stateSave: true,
stateSaveParams: function (settings, data) {
        //Clear column search if there is any
        for (var i = 0; i < data.columns.length; i++){
          data.columns[i].search.search = "";
        }
       //Remove Search Value
        data.search.search = ""; 
      },
      stateSaveCallback: function ( settings, data ) {
        //Set html5 storage
        localStorage.setItem( kind+'_table', JSON.stringify( data ) );
        //Save to DB (User Login sets localStorage from DB)
        table_db_save( kind+'_table', JSON.stringify( data ) );
      },
      stateLoadCallback: function ( settings ) {
        //load saved settings from local storage
        try {
          return JSON.parse( localStorage.getItem( kind+'_table' ) );
        } catch (e) {};
      },

Does anyone else have this behavior? It happens in chrome & firefox on mac and also I got one customer complaining it wont save sometimes with windows and edge.

Is it possible to show an error message if the json string is wrong for datatables? Maybe there is a problem with the JSON format when i remove the values?

Live Version: www.btree.at/app
Login: demo_en@btree.at
PW: demo_en

It is in german but on the left side you see "Tabellen", there are multiple Datatables.

Cheers
Hannes

Answers

  • btreebtree Posts: 99Questions: 14Answers: 11

    Ok seems I found a workaround at the moment, needs more testing but seems fine.

    //Setting a function global
    var load_state = false;
    
    stateSaveCallback: function ( settings, data ) {
            if (load_state) {
              //Set html5 storage
              localStorage.setItem( kind+'_table', JSON.stringify( data ) );
              //Save to DB (User Login sets localStorage from DB)
              table_db_save( kind+'_table', JSON.stringify( data ) );
            }
          },
      stateLoadCallback: function ( settings ) {
            //load saved settings from local storage
             var json = JSON.parse( localStorage.getItem( kind+'_table' ) );
             load_state = true;
             return json;
          },
    

    Cheers
    Hannes

This discussion has been closed.