State saving during initialization

State saving during initialization

DanSwitzer2DanSwitzer2 Posts: 21Questions: 4Answers: 0

I've noticed that when the stateSave: true option is enabled, DataTables is calling the functions to save state during initialization. What I'm seeing is that the load events fire first and then the save events fire.

Here's an example:

https://jsfiddle.net/dswitzer/539q35p8/

I do not want the save events to fire, if there has been no state change. I only want the save events to fire if a user takes an action that changes state, such as:

  • Changing the sort order
  • Reordering columns
  • Changing the visibility of columns

I really don't even want the state to save if the user changes the page.

I'm especially concerned about the save events being called on table initialization, because it can lead to users not seeing changes to the table. If I change the default sort order of a table, I want a user that's never changed the sort order to see the changes to the defaults. There are are some issues in which a bug could be generated, because of change to the table layout.

Is there a good way to prevent the save from actually happening during table initialization?

Looking at the code, it looks like if I change the following:

            if ( oInit.bStateSave )
            {
                features.bStateSave = true;
                _fnCallbackReg( oSettings, 'aoDrawCallback', _fnSaveState, 'state_save' );
                _fnLoadState( oSettings, oInit, loadedInit );
            }

And just swap the save/load, this resolves the issue:

            if ( oInit.bStateSave )
            {
                features.bStateSave = true;
                _fnLoadState( oSettings, oInit, loadedInit );
                _fnCallbackReg( oSettings, 'aoDrawCallback', _fnSaveState, 'state_save' );
            }

However, I'm not sure if this might break something. It also doesn't address the issue of the user change the page.

Any suggestions on only saving during certain events?

This discussion has been closed.