jQuery DataTables: Save the same state for multiple tables -
i want when change state of table (example: number of lines per page), change recorded , applied tables. tried statesave: true
, saved table made change.
$('#currenttab').datatable({ "order": [[0, "desc"]], statesave: true }); $('#twowavetab').datatable({ "order": [[0, "desc"]], statesave: true }); $('#evoltab').datatable({ "order": [[0, "desc"]], statesave: true });
how can save same state table , @ same time other generic tables?
it possible using statesaveparams event , state.save() api method.
there no direct way copy state of 1 table another, need set each state property individually. code below copies page length 1 table others $(this).datatable().page.len(data.length)
.
you need replace example1
, example2
, example3
ids of tables (currenttab
, twowavetab
, evoltab
).
var table1 = $('#example1').datatable({ "order": [[0, "desc"]], statesave: true }); var table2 = $('#example2').datatable({ "order": [[0, "desc"]], statesave: true }); var table3 = $('#example3').datatable({ "order": [[0, "desc"]], statesave: true }); var is_save_in_progress = false; var $tables = $('#example1, #example2, #example3'); $tables .on( 'statesaveparams.dt', function (e, settings, data){ // save dom element var = this; // if state saving not in progress if(!is_save_in_progress){ is_save_in_progress = true; // iterate on tables $tables.each(function(index){ // if table other initial table if(this.id !== that.id){ // set page length, save state , redraw table $(this).datatable() .page.len(data.length) .state.save() .draw(false); } }); // indicate state saving on is_save_in_progress = false; } });
see this jsfiddle demonstration.
Comments
Post a Comment