Problems with stateSaveCallback and stateLoadCallback

Problems with stateSaveCallback and stateLoadCallback

billhailsbillhails Posts: 5Questions: 1Answers: 0

Hi,
I have a somewhat non-standard use of state save and state load. The application is unfortunately on a password protected site, but I've uploaded debug:

http://debug.datatables.net/onudef

  1. I have a form that submits a database query and the result populates the table.
  2. I have a drop-down that pre-populates the form in standard pre-defined ways for standard queries.
  3. The user can modify the form and save it to local storage with a new name, that name now appears in the drop-down.
  4. I want to also persist table state, associated separately with each saved form (some columns are irrelevant to some queries.)

I'm using


var table = jQuery('#rhac-re-results-table').DataTable( { "columnDefs": [ { "orderable": false, "targets": 8 } ], stateSave: true, stateSaveCallback: function(settings, data) { console.log("stateSaveCallback setting current table state to %o", data); currentTableState = data; }, stateLoadCallback: function(settings) { console.log("stateLoadCallback returning current table state %o", currentTableState); return currentTableState; } } );
  • The currentTableState is also set when a dropdown query is selected, and saved to local storage keyed on the report name .
  • The table is re-created for each query.

What I'm seeing in my debug, when i run the query, is that the stateLoadCallback is called and then the StateSaveCallback.
the stateLoadCallback reports that it is returning the correct state (some columns invisible) but then the stateSaveCallback immediately reports it is saving what looks like the default state, as if the return value from the stateLoadCallback is getting ignored.

Anyone seen this before?

Replies

  • billhailsbillhails Posts: 5Questions: 1Answers: 0

    I think I know what the problem is.

    I'm styling with jQuery UI and for that reason, as recommended, I'm initializing ColVis separately, after the table is created, and after the state is loaded. So I just need to figure out how to tell the table to reload its state.

  • billhailsbillhails Posts: 5Questions: 1Answers: 0

    I've got it working, but it's ugly. I had to set a colVisInitialised flag to false before creating the table and make the stateSaveCallback do nothing if that flag is false.

    Then after ColVis is initialised:

    if (currentTableState) {
        currentTableState.abVisCols.forEach( function (val, index) {
            var col = table.column(index);
            col.visible(val);
        });
        colvis.rebuild();
    }
    colVisInitialized = true;
    
This discussion has been closed.