Is it possible to maintain search between multiple pages

Is it possible to maintain search between multiple pages

edoarslaedoarsla Posts: 7Questions: 1Answers: 0

Hi,

On several pages I have a table with the same id, and stateSave is set to true. However, search is not maintained between these pages.

I see that localStorage key is different between these pages since it includes "location.pathname", but I am not sure if there is a way to override this.

Does anyone know how to do this in 1.10 version.

Thank you!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi,

    You are absolutely correct, the default DataTables state saving methods will use the path to ensure that table state is independently saved. If you want to have the state saved over multiple pages for a common table, the stateSaveCallback and stateLoadCallback options can be used to provide a custom state saving function. For example:

    $('#example').DataTable( {
      stateSave: true,
      stateSaveCallback: function ( settings, state ) {
        localStorage.setItem( 'commonTable', JSON.stringify( data ) );
      },
      stateLoadCallback: function ( settings ) {
        try {
          return JSON.parse( localStorage.getItem( 'commonTable' ) );
        } catch (e) {}
      }
    } );
    

    In this case I've simply used a common localStorage key called commonTable, but you could call that anything you want, and also use any data storage you want (sessionStorage for example).

    Regards,
    Allan

  • edoarslaedoarsla Posts: 7Questions: 1Answers: 0

    Thank you Allan!

This discussion has been closed.