Laravel 8 Datatables Keeping Selected Page Number

Websolutionstuff | Dec-03-2021 | Categories : Laravel PHP jQuery

In this tutorial we will see laravel 8 datatables keeping selected page number after callback. In datatable page number is not save when you reload page so, we need to keep or get current page in datatable after page reload to save state of datatable. 

So, we need this kind of requirements like refresh datatable without refreshing page number in laravel 6/7/8, laravel 6/7/8 datatable refresh current page, laravel 6/7/8 datatable state save, refresh page without losing active pagination in datatable using jquery, how to refresh datatable without reloading page in laravel 6/7/8.

Enable or disable state saving. Datatables stores state information such as pagination position, display length, filtering and sorting. When this initialisation option is active and the end user reloads the page the table's state will be altered to match what they had previously set up.

So, let's see some examples of datatables state save callback example

Example 1 :

In this example just change value of statesave function like true or false.

$('#example').dataTable( {
  stateSave: true,
  stateDuration:-1
} );

Data storage for the state information in the browser is performed by use of the localStorage or sessionStorage HTML5 APIs.

The stateDuration : This option is also used to indicate to datatables if localStorage or sessionStorage should be used for storing the table's state. When set to -1 sessionStorage will be used, while for 0 or greater localStorage will be used.

 

 

Set state duration to 1 day :

$('#example').dataTable( {
  "stateSave": true,
  "stateDuration": 60 * 60 * 24
});

 

Example 2 :

Enable state saving and override state save/load handlers to use only the table's DOM id

$('#example').dataTable( {
  stateSave: true,
  stateSaveCallback: function(settings,data) {
      localStorage.setItem( 'DataTables_' + settings.sInstance, JSON.stringify(data) )
    },
  stateLoadCallback: function(settings) {
    return JSON.parse( localStorage.getItem( 'DataTables_' + settings.sInstance ) )
    }
} );

 

Example 3 :

stay on the current page after re-drawing if you pass false as a first parameter to the draw() function.

table.row(index).data(data).draw(false)

 

 

Example 4 :

If you are using server side datatable, then you can use ajax.reload() function to reload the datatable and pass the true or false as a parameter for refresh paging.

var table = $("#table").DataTable();
table.ajax.reload(null, false);

 


You might also like :

Recommended Post
Featured Post
How To Install Angular In Ubuntu
How To Install Angular In Ubun...

In this article, we will see how to install angular in ubuntu. Angular is a framework, library, assets, a...

Read More

May-09-2022

Laravel where and orWhere Condition Example
Laravel where and orWhere Cond...

In this article, we will see laravel where and orWhere condition example. we will give you a very simple example of...

Read More

Jan-11-2021

Know About MilesWeb’s WordPress Hosting Plans
Know About MilesWeb’s WordPres...

Want to make your WordPress site online? But for this, you will need to opt-in for a managed WordPress hosting provider....

Read More

Apr-09-2022

Laravel 9 whereIn / whereNotIn / orWhereIn / orWhereNotIn
Laravel 9 whereIn / whereNotIn...

In this article, we will see Laravel 9 whereIn / whereNotIn / orWhereIn / orWhereNotIn Query Example. The whereIn()...

Read More

Oct-17-2022