Skip to content

Commit d9a9c2e

Browse files
committed
Fixed issue with updating table state when table is reloaded via ajax.reload() API method
1 parent 6171793 commit d9a9c2e

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

js/dataTables.checkboxes.js

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,35 @@ Checkboxes.prototype = {
238238
});
239239
}
240240

241+
// Handle Ajax request completion event
242+
$table.on('xhr.dt', function ( e, settings, json, xhr ) {
243+
// Retrieve stored state
244+
var state = dt.state.loaded();
245+
246+
$.each(self.s.columns, function(index, colIdx){
247+
// Clear data
248+
self.s.data[colIdx] = {};
249+
250+
// If state is loaded and contains data for this column
251+
if(state && state.checkboxes && state.checkboxes.hasOwnProperty(colIdx)){
252+
// Load previous state
253+
self.s.data[colIdx] = state.checkboxes[colIdx];
254+
}
255+
});
256+
257+
// If state saving is enabled
258+
if(ctx.oFeatures.bStateSave){
259+
// If server-side processing mode is not enabled
260+
// NOTE: Needed to avoid duplicate call to updateCheckboxes() in onDraw()
261+
if(!ctx.oFeatures.bServerSide){
262+
// Update table state on next redraw
263+
$table.one('draw.dt.dtCheckboxes', function(e){
264+
self.updateState();
265+
});
266+
}
267+
}
268+
});
269+
241270
// Handle table draw event
242271
$table.on('draw.dt.dtCheckboxes', function(e){
243272
self.onDraw(e);
@@ -289,11 +318,7 @@ Checkboxes.prototype = {
289318
// If server-side processing mode is not enabled
290319
// NOTE: Needed to avoid duplicate call to updateCheckboxes() in onDraw()
291320
if(!ctx.oFeatures.bServerSide){
292-
self.updateCheckboxes({ page: 'all', search: 'none' });
293-
294-
$.each(self.s.columns, function(index, colIdx){
295-
self.updateSelectAll(colIdx);
296-
});
321+
self.updateState();
297322
}
298323

299324
// Handle state saving event
@@ -453,6 +478,19 @@ Checkboxes.prototype = {
453478
}
454479
},
455480

481+
// Update table state
482+
updateState: function(){
483+
var self = this;
484+
var dt = self.s.dt;
485+
var ctx = self.s.ctx;
486+
487+
self.updateCheckboxes({ page: 'all', search: 'none' });
488+
489+
$.each(self.s.columns, function(index, colIdx){
490+
self.updateSelectAll(colIdx);
491+
});
492+
},
493+
456494
// Updates state of multiple checkboxes
457495
updateCheckboxes: function(opts){
458496
var self = this;
@@ -571,7 +609,7 @@ Checkboxes.prototype = {
571609

572610
// If server-side processing is enabled
573611
if(ctx.oFeatures.bServerSide){
574-
self.updateCheckboxes({ page: 'current' });
612+
self.updateCheckboxes({ page: 'current', search: 'none' });
575613
}
576614

577615
$.each(self.s.columns, function(index, colIdx){

0 commit comments

Comments
 (0)