Skip to content

Commit bab3eee

Browse files
committed
Show correct number of selected rows in table's summary element
1 parent 82f49bc commit bab3eee

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

css/dataTables.checkboxes.css

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
table.dataTable.checkboxes-select tbody tr,
22
table.dataTable thead .checkboxes-select-all {
33
cursor: pointer;
4-
}
4+
}
5+
6+
div.dataTables_wrapper span.select-info,
7+
div.dataTables_wrapper span.select-item {
8+
margin-left: 0.5em;
9+
}
10+
11+
@media screen and (max-width: 640px) {
12+
div.dataTables_wrapper span.select-info,
13+
div.dataTables_wrapper span.select-item {
14+
margin-left: 0;
15+
display: block;
16+
}
17+
}

js/dataTables.checkboxes.js

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
/*! Checkboxes 1.0.2
1+
/*! Checkboxes 1.0.3
22
* Copyright (c) Gyrocode (www.gyrocode.com)
33
* License: MIT License
44
*/
55

66
/**
77
* @summary Checkboxes
88
* @description Checkboxes extension for jQuery DataTables
9-
* @version 1.0.2
9+
* @version 1.0.3
1010
* @file dataTables.checkboxes.js
1111
* @author Gyrocode (http://www.gyrocode.com/projects/jquery-datatables-checkboxes/)
1212
* @contact http://www.gyrocode.com/contacts
@@ -178,6 +178,9 @@ Checkboxes.prototype = {
178178
self.onSelect(e, type, indexes);
179179
});
180180

181+
// Disable Select extension information display
182+
dt.select.info(false);
183+
181184
// Otherwise, if Select extension is not available
182185
} else {
183186
$table_body.on('click', 'td', function(){
@@ -191,6 +194,11 @@ Checkboxes.prototype = {
191194
$row.toggleClass('selected');
192195
});
193196
}
197+
198+
// Update the table information element with selected item summary
199+
$table.on('draw.dt select.dt deselect.dt', function (){
200+
self.showInfoSelected();
201+
});
194202
}
195203

196204
// Handle table draw event
@@ -539,7 +547,59 @@ Checkboxes.prototype = {
539547
}
540548
}
541549
}
550+
},
551+
552+
// Updates the information element of the DataTable showing information about the
553+
// items selected. Based on info() method of Select extension.
554+
showInfoSelected: function(){
555+
var self = this;
556+
var dt = self.s.dt;
557+
var ctx = dt.settings()[0];
558+
559+
if ( ! ctx.aanFeatures.i ) {
560+
return;
561+
}
562+
563+
var $output = $('<span class="select-info"/>');
564+
var add = function(name, num){
565+
$output.append( $('<span class="select-item"/>').append( dt.i18n(
566+
'select.'+name+'s',
567+
{ _: '%d '+name+'s selected', 0: '', 1: '1 '+name+' selected' },
568+
num
569+
) ) );
570+
};
571+
572+
// Find index of the first column that has checkbox and row selection enabled
573+
var colSelectRowIdx = -1;
574+
for(var colIdx = 0; colIdx < ctx.aoColumns.length; colIdx++){
575+
// If Checkboxes extension is enabled
576+
// and row selection is enabled for this column
577+
if(ctx.aoColumns[colIdx].checkboxes && ctx.aoColumns[colIdx].checkboxes.selectRow){
578+
colSelectRowIdx = colIdx;
579+
break;
580+
}
581+
}
582+
583+
// If there is a column that has checkbox and row selection enabled
584+
if(colSelectRowIdx !== -1){
585+
add('row', ctx.checkboxes.s.data[colSelectRowIdx].length);
586+
587+
// Internal knowledge of DataTables to loop over all information elements
588+
$.each( ctx.aanFeatures.i, function ( i, el ) {
589+
var $el = $(el);
590+
591+
var $exisiting = $el.children('span.select-info');
592+
if($exisiting.length){
593+
$exisiting.remove();
594+
}
595+
596+
if($output.text() !== ''){
597+
$el.append($output);
598+
}
599+
});
600+
}
542601
}
602+
543603
};
544604

545605

0 commit comments

Comments
 (0)