Skip to content

Commit baea34e

Browse files
committed
Fix #128: Allow single rows selection/deselection when Select style is set to "os".
1 parent abbdcd0 commit baea34e

File tree

3 files changed

+49
-29
lines changed

3 files changed

+49
-29
lines changed

js/dataTables.checkboxes.js

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*!
1+
/*!
22
* jQuery DataTables Checkboxes (https://www.gyrocode.com/projects/jquery-datatables-checkboxes/)
33
* Checkboxes extension for jQuery DataTables
44
*
@@ -362,7 +362,7 @@
362362
}
363363

364364
// Handle Ajax request completion event
365-
// NOTE: Needed to update table state
365+
// NOTE: Needed to update table state
366366
// if table is reloaded via ajax.reload() API method
367367
$(dt.table().node()).on('xhr.dt.dtCheckboxes', function ( e, settings , json, xhr ) {
368368
self.onDataTablesXhr(e. settings, json, xhr);
@@ -474,7 +474,7 @@
474474

475475
$.each(self.s.columns, function(index, colIdx){
476476
self.updateSelectAll(colIdx);
477-
});
477+
});
478478
},
479479

480480
// Handles DataTables Ajax request completion event
@@ -583,7 +583,7 @@
583583
self.updateStateCheckboxes({ page: 'all', search: 'none' });
584584

585585
// If FixedColumns extension is enabled
586-
if(ctx._oFixedColumns){
586+
if(ctx._oFixedColumns){
587587
// Use delay to let FixedColumns construct the header
588588
// before we update the "Select all" checkbox
589589
setTimeout(function(){
@@ -652,6 +652,7 @@
652652
var cell = dt.cell(cellSelector);
653653
var cellIdx = cell.index();
654654
var colIdx = cellIdx.column;
655+
var rowIdx = cellIdx.row;
655656

656657
// If row selection is not enabled
657658
// NOTE: if row selection is enabled, checkbox selection/deselection
@@ -663,31 +664,50 @@
663664
e.stopPropagation();
664665

665666
} else {
666-
// WORKAROUND:
667-
// Select extension may keep the row selected
668-
// when checkbox is unchecked with SHIFT key.
669-
//
670-
// We need to update the state of the checkbox AFTER handling
671-
// select/deselect event from Select extension.
672-
//
673-
// Call to setTimeout is needed to let select/deselect event handler
674-
// update the data first.
675-
setTimeout(function(){
676-
// Get cell data
677-
var cellData = cell.data();
678667

679-
// Determine whether data is in the list
680-
var hasData = (
681-
Object.prototype.hasOwnProperty.call(self.s.data, colIdx)
682-
&& Object.prototype.hasOwnProperty.call(self.s.data[colIdx], cellData)
683-
);
668+
// If Select extension is enabled
669+
if(ctx._select){
670+
// If style is set to "os"
671+
if(ctx._select.style === 'os'){
684672

685-
// If state of the checkbox needs to be updated
686-
if(hasData !== ctrl.checked){
687-
self.updateCheckbox(cell, colIdx, hasData);
688-
self.updateSelectAll(colIdx);
673+
// WORKAROUND:
674+
// See https://github.com/gyrocode/jquery-datatables-checkboxes/issues/128
675+
676+
// Prevent click event from propagating to parent
677+
e.stopPropagation();
678+
679+
// Select/deselect individual row
680+
cell.checkboxes.select(ctrl.checked);
681+
682+
// Otherwise, if style is set to other than "os"
683+
} else {
684+
// WORKAROUND:
685+
// Select extension may keep the row selected
686+
// when checkbox is unchecked with SHIFT key.
687+
//
688+
// We need to update the state of the checkbox AFTER handling
689+
// select/deselect event from Select extension.
690+
//
691+
// Call to setTimeout is needed to let select/deselect event handler
692+
// update the data first.
693+
setTimeout(function(){
694+
// Get cell data
695+
var cellData = cell.data();
696+
697+
// Determine whether data is in the list
698+
var hasData = (
699+
Object.prototype.hasOwnProperty.call(self.s.data, colIdx)
700+
&& Object.prototype.hasOwnProperty.call(self.s.data[colIdx], cellData)
701+
);
702+
703+
// If state of the checkbox needs to be updated
704+
if(hasData !== ctrl.checked){
705+
self.updateCheckbox(cell, colIdx, hasData);
706+
self.updateSelectAll(colIdx);
707+
}
708+
}, 0);
689709
}
690-
}, 0);
710+
}
691711
}
692712
},
693713

@@ -827,7 +847,7 @@
827847
$checkboxesSelectAll.data('is-changed', false);
828848

829849
$checkboxesSelectAll.prop({
830-
// NOTE: If checkbox has indeterminate state,
850+
// NOTE: If checkbox has indeterminate state,
831851
// "checked" property must be set to false.
832852
'checked': isIndeterminate ? false : isSelected,
833853
'indeterminate': isIndeterminate

0 commit comments

Comments
 (0)