-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathdataTables.checkboxes.js
More file actions
1305 lines (1075 loc) · 43.9 KB
/
dataTables.checkboxes.js
File metadata and controls
1305 lines (1075 loc) · 43.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* jQuery DataTables Checkboxes (https://www.gyrocode.com/projects/jquery-datatables-checkboxes/)
* Checkboxes extension for jQuery DataTables
*
* @version 1.2.13
* @author Gyrocode LLC (https://www.gyrocode.com)
* @copyright (c) Gyrocode LLC
* @license MIT
*/
(function( factory ){
/* eslint-disable */
if ( typeof define === 'function' && define.amd ) {
// AMD
define( ['jquery', 'datatables.net'], function ( $ ) {
return factory( $, window, document );
} );
}
else if ( typeof exports === 'object' ) {
// CommonJS
module.exports = function (root, $) {
if ( ! root ) {
root = window;
}
if ( ! $ || ! $.fn.dataTable ) {
$ = require('datatables.net')(root, $).$;
}
return factory( $, root, root.document );
};
}
else {
// Browser
factory( jQuery, window, document );
}
/* eslint-enable */
}(function( $, window, document ) {
'use strict';
var DataTable = $.fn.dataTable;
/**
* Checkboxes is an extension for the jQuery DataTables library that provides
* universal solution for working with checkboxes in a table.
*
* @class
* @param {object} settings DataTables settings object for the host table
* @requires jQuery 1.7+
* @requires DataTables 1.10.8+
*
* @example
* $('#example').DataTable({
* 'columnDefs': [
* {
* 'targets': 0,
* 'checkboxes': true
* }
* ]
* });
*/
var Checkboxes = function ( settings ) {
// Sanity check that we are using DataTables 1.10.8 or newer
if ( ! DataTable.versionCheck || ! DataTable.versionCheck( '1.10.8' ) ) {
throw 'DataTables Checkboxes requires DataTables 1.10.8 or newer';
}
this.s = {
dt: new DataTable.Api( settings ),
columns: [],
data: {},
dataDisabled: {},
ignoreSelect: false
};
// Get settings object
this.s.ctx = this.s.dt.settings()[0];
// Check if checkboxes have already been initialised on this table
if ( this.s.ctx.checkboxes ) {
return;
}
settings.checkboxes = this;
this._constructor();
};
Checkboxes.prototype = {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Constructor
*/
/**
* Initialise the Checkboxes instance
*
* @private
*/
_constructor: function ()
{
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
var hasCheckboxes = false;
var hasCheckboxesSelectRow = false;
for(var i = 0; i < ctx.aoColumns.length; i++){
if (ctx.aoColumns[i].checkboxes){
var $colHeader = $(dt.column(i).header());
//
// INITIALIZATION
//
hasCheckboxes = true;
if(!$.isPlainObject(ctx.aoColumns[i].checkboxes)){
ctx.aoColumns[i].checkboxes = {};
}
ctx.aoColumns[i].checkboxes = $.extend(
{}, Checkboxes.defaults, ctx.aoColumns[i].checkboxes
);
//
// OPTIONS
//
var colOptions = {
'searchable': false,
'orderable': false
};
if(ctx.aoColumns[i].sClass === ''){
colOptions['className'] = 'dt-checkboxes-cell';
} else {
colOptions['className'] = ctx.aoColumns[i].sClass + ' dt-checkboxes-cell';
}
if(ctx.aoColumns[i].sWidthOrig === null){
colOptions['width'] = '1%';
}
if(ctx.aoColumns[i].mRender === null){
colOptions['render'] = function(){
return '<input type="checkbox" class="dt-checkboxes" autocomplete="off">';
};
}
DataTable.ext.internal._fnColumnOptions(ctx, i, colOptions);
// WORKAROUND: Remove "sorting" class
$colHeader.removeClass('sorting');
// WORKAROUND: Detach all event handlers for this column
$colHeader.off('.dt');
// If table has data source other than Ajax
if(ctx.sAjaxSource === null){
// WORKAROUND: Invalidate column data
var cells = dt.cells('tr', i);
cells.invalidate('data');
// WORKAROUND: Add required class to existing cells
$(cells.nodes()).addClass(colOptions['className']);
}
//
// DATA
//
// Initialize object holding data for selected checkboxes
self.s.data[i] = {};
self.s.dataDisabled[i] = {};
// Store column index for easy column selection later
self.s.columns.push(i);
//
// CLASSES
//
// If row selection is enabled for this column
if(ctx.aoColumns[i].checkboxes.selectRow){
// If Select extension is enabled
if(ctx._select){
hasCheckboxesSelectRow = true;
// Otherwise, if Select extension is not enabled
} else {
// Disable row selection for this column
ctx.aoColumns[i].checkboxes.selectRow = false;
}
}
// If "Select all" control is enabled
if(ctx.aoColumns[i].checkboxes.selectAll){
// Save previous HTML content
$colHeader.data('html', $colHeader.html());
// If "Select all" control markup is provided
if(ctx.aoColumns[i].checkboxes.selectAllRender !== null){
var selectAllHtml = '';
// If "selectAllRender" option is a function
if($.isFunction(ctx.aoColumns[i].checkboxes.selectAllRender)){
selectAllHtml = ctx.aoColumns[i].checkboxes.selectAllRender();
// Otherwise, if "selectAllRender" option is a string
} else if(typeof ctx.aoColumns[i].checkboxes.selectAllRender === 'string'){
selectAllHtml = ctx.aoColumns[i].checkboxes.selectAllRender;
}
$colHeader
.html(selectAllHtml)
.addClass('dt-checkboxes-select-all')
.attr('data-col', i);
}
}
// Initial checkbox selection
if(ctx.aoColumns[i].checkboxes.initialSelectionArray){
for (var arrayIndex = 0; arrayIndex < ctx.aoColumns[i].checkboxes.initialSelectionArray.length; ++arrayIndex) {
var cellData = ctx.aoColumns[i].checkboxes.initialSelectionArray[arrayIndex];
ctx.checkboxes.s.data[i][cellData] = 1;
}
}
}
}
// If table has at least one checkbox column
if(hasCheckboxes){
// Load previous state
self.loadState();
//
// EVENT HANDLERS
//
var $table = $(dt.table().node());
var $tableBody = $(dt.table().body());
var $tableContainer = $(dt.table().container());
// If there is at least one column that has row selection enabled
if(hasCheckboxesSelectRow){
$table.addClass('dt-checkboxes-select');
// Handle event before row is selected/deselected
$table.on('user-select.dt.dtCheckboxes', function (e, dt, type, cell , originalEvent){
self.onDataTablesUserSelect(e, dt, type, cell , originalEvent);
});
// Handle row select/deselect event
$table.on('select.dt.dtCheckboxes deselect.dt.dtCheckboxes', function(e, api, type, indexes){
self.onDataTablesSelectDeselect(e, type, indexes);
});
// If displaying of Select extension information is enabled
if(ctx._select.info){
// Disable Select extension information display
dt.select.info(false);
// Update the table information element with selected item summary
//
// NOTE: Needed to display correct count of selected rows
// when using server-side processing mode
$table.on('draw.dt.dtCheckboxes select.dt.dtCheckboxes deselect.dt.dtCheckboxes', function(){
self.showInfoSelected();
});
}
}
// Handle table draw event
$table.on('draw.dt.dtCheckboxes', function(e){
self.onDataTablesDraw(e);
});
// Handle checkbox click event
$tableBody.on('click.dtCheckboxes', 'input.dt-checkboxes', function(e){
self.onClick(e, this);
});
// Handle click on "Select all" control
$tableContainer.on('click.dtCheckboxes', 'thead th.dt-checkboxes-select-all input[type="checkbox"]', function(e){
self.onClickSelectAll(e, this);
});
// Handle click on heading containing "Select all" control
$tableContainer.on('click.dtCheckboxes', 'thead th.dt-checkboxes-select-all', function() {
$('input[type="checkbox"]', this).not(':disabled').trigger('click');
});
// If row selection is disabled
if(!hasCheckboxesSelectRow){
// Handle click on cell containing checkbox
$tableContainer.on('click.dtCheckboxes', 'tbody td.dt-checkboxes-cell', function() {
$('input[type="checkbox"]', this).not(':disabled').trigger('click');
});
}
// Handle click on label node in heading containing "Select all" control
// and in cell containing checkbox
$tableContainer.on('click.dtCheckboxes', 'thead th.dt-checkboxes-select-all label, tbody td.dt-checkboxes-cell label', function(e) {
// Prevent default behavior
e.preventDefault();
});
// Handle click on "Select all" control in floating fixed header
$(document).on('click.dtCheckboxes', '.fixedHeader-floating thead th.dt-checkboxes-select-all input[type="checkbox"]', function(e){
// If FixedHeader is enabled in this instance
if(ctx._fixedHeader){
// If header is floating in this instance
if(ctx._fixedHeader.dom['header'].floating){
self.onClickSelectAll(e, this);
}
}
});
// Handle click on heading containing "Select all" control in floating fixed header
$(document).on('click.dtCheckboxes', '.fixedHeader-floating thead th.dt-checkboxes-select-all', function() {
// If FixedHeader is enabled in this instance
if(ctx._fixedHeader){
// If header is floating in this instance
if(ctx._fixedHeader.dom['header'].floating){
$('input[type="checkbox"]', this).trigger('click');
}
}
});
// Handle table initialization event
$table.on('init.dt.dtCheckboxes', function(){
// Use delay to handle initialization event
// because certain extensions (FixedColumns) are initialized
// only when initialization event is triggered.
setTimeout(function(){
self.onDataTablesInit();
}, 0);
});
// Handle state saving event
$table.on('stateSaveParams.dt.dtCheckboxes', function (e, settings, data) {
self.onDataTablesStateSave(e, settings, data);
});
// Handle table destroy event
$table.one('destroy.dt.dtCheckboxes', function(e, settings){
self.onDataTablesDestroy(e, settings);
});
}
},
// Handles DataTables initialization event
onDataTablesInit: function(){
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
// If server-side processing mode is not enabled
// NOTE: Needed to avoid duplicate call to updateStateCheckboxes() in onDataTablesDraw()
if(!ctx.oFeatures.bServerSide){
// If state saving is enabled
if(ctx.oFeatures.bStateSave){
self.updateState();
}
// Handle Ajax request completion event
// NOTE: Needed to update table state
// if table is reloaded via ajax.reload() API method
$(dt.table().node()).on('xhr.dt.dtCheckboxes', function ( e, settings , json, xhr ) {
self.onDataTablesXhr(e. settings, json, xhr);
});
}
},
// Handles DataTables user initiated select event
onDataTablesUserSelect: function ( e, dt, type, cell /*, originalEvent*/ ){
var self = this;
var cellIdx = cell.index();
var rowIdx = cellIdx.row;
var colIdx = self.getSelectRowColIndex();
var cellData = dt.cell({ row: rowIdx, column: colIdx }).data();
// If checkbox in the cell cannot be checked
if(!self.isCellSelectable(colIdx, cellData)){
// Prevent row selection
e.preventDefault();
}
},
// Handles DataTables row select/deselect event
onDataTablesSelectDeselect: function(e, type, indexes){
var self = this;
var dt = self.s.dt;
if(self.s.ignoreSelect){ return; }
if(type === 'row'){
// Get index of the first column that has checkbox and row selection enabled
var colIdx = self.getSelectRowColIndex();
if(colIdx !== null){
var cells = dt.cells(indexes, colIdx);
self.updateData(cells, colIdx, (e.type === 'select') ? true : false);
self.updateCheckbox(cells, colIdx, (e.type === 'select') ? true : false);
self.updateSelectAll(colIdx);
}
}
},
// Handles DataTables state save event
onDataTablesStateSave: function (e, settings, data) {
var self = this;
var ctx = self.s.ctx;
// For every column where checkboxes are enabled
$.each(self.s.columns, function(index, colIdx){
// If checkbox state saving is enabled
if(ctx.aoColumns[colIdx].checkboxes.stateSave){
// If checkboxes state hasn't been saved before
if(!Object.prototype.hasOwnProperty.call(data, 'checkboxes')){
// Initialize array to save checkboxes state for each column
data.checkboxes = [];
}
// Save checkboxes state
data.checkboxes[colIdx] = self.s.data[colIdx];
}
});
},
// Handles DataTables destroy event
onDataTablesDestroy: function(){
var self = this;
var dt = self.s.dt;
// Get table elements
var $table = $(dt.table().node());
var $tableBody = $(dt.table().body());
var $tableContainer = $(dt.table().container());
// Detach event handlers
$(document).off('click.dtCheckboxes');
$tableContainer.off('.dtCheckboxes');
$tableBody.off('.dtCheckboxes');
$table.off('.dtCheckboxes');
// Clear data
//
// NOTE: Needed only to reduce memory footprint
// in case user saves instance of DataTable object.
self.s.data = {};
self.s.dataDisabled = {};
// Remove added elements
$('.dt-checkboxes-select-all', $table).each(function(index, el){
$(el)
.html($(el).data('html'))
.removeClass('dt-checkboxes-select-all');
});
},
// Handles DataTables draw event
onDataTablesDraw: function(){
var self = this;
var ctx = self.s.ctx;
// If server-side processing is enabled
// or deferred render is enabled
//
// TODO: it's not optimal to update state of checkboxes
// for already created rows in deferred rendering mode
if(ctx.oFeatures.bServerSide || ctx.oFeatures.bDeferRender){
self.updateStateCheckboxes({ page: 'current', search: 'none' });
}
$.each(self.s.columns, function(index, colIdx){
self.updateSelectAll(colIdx);
});
},
// Handles DataTables Ajax request completion event
onDataTablesXhr: function( /* e, settings , json, xhr */ ){
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
// Get table elements
var $table = $(dt.table().node());
// For every column where checkboxes are enabled
$.each(self.s.columns, function(index, colIdx){
// Reset data
self.s.data[colIdx] = {};
self.s.dataDisabled[colIdx] = {};
});
// If state saving is enabled
if(ctx.oFeatures.bStateSave){
// Load previous state
self.loadState();
// Update table state on next redraw
$table.one('draw.dt.dtCheckboxes', function(){
self.updateState();
});
}
},
// Updates array holding data for selected checkboxes
updateData: function(cells, colIdx, isSelected){
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
// If Checkboxes extension is enabled for this column
if(ctx.aoColumns[colIdx].checkboxes){
var cellsData = cells.data();
cellsData.each(function(cellData){
// If checkbox is checked
if(isSelected){
ctx.checkboxes.s.data[colIdx][cellData] = 1;
// Otherwise, if checkbox is not checked
} else {
delete ctx.checkboxes.s.data[colIdx][cellData];
}
});
// If state saving is enabled
if(ctx.oFeatures.bStateSave){
// If checkbox state saving is enabled
if(ctx.aoColumns[colIdx].checkboxes.stateSave){
// Save state
dt.state.save();
}
}
}
},
// Updates row selection
updateSelect: function(selector, isSelected){
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
// If Select extension is enabled
if(ctx._select){
// Disable select event hanlder temporarily
self.s.ignoreSelect = true;
if(isSelected){
dt.rows(selector).select();
} else {
dt.rows(selector).deselect();
}
// Re-enable select event handler
self.s.ignoreSelect = false;
}
},
// Updates state of single checkbox
updateCheckbox: function(cells, colIdx, isSelected){
var self = this;
var ctx = self.s.ctx;
var cellNodes = cells.nodes();
if(cellNodes.length){
$('input.dt-checkboxes', cellNodes).not(':disabled').prop('checked', isSelected);
// If selectCallback is a function
if($.isFunction(ctx.aoColumns[colIdx].checkboxes.selectCallback)){
ctx.aoColumns[colIdx].checkboxes.selectCallback(cellNodes, isSelected);
}
}
},
// Update table state
updateState: function(){
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
self.updateStateCheckboxes({ page: 'all', search: 'none' });
// If FixedColumns extension is enabled
if(ctx._oFixedColumns){
// Use delay to let FixedColumns construct the header
// before we update the "Select all" checkbox
setTimeout(function(){
// For every column where checkboxes are enabled
$.each(self.s.columns, function(index, colIdx){
self.updateSelectAll(colIdx);
});
}, 0);
}
},
// Updates state of multiple checkboxes
updateStateCheckboxes: function(opts){
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
// Enumerate all cells
dt.cells('tr', self.s.columns, opts).every(function(rowIdx, colIdx){
// Get cell data
var cellData = this.data();
// Determine if checkbox in the cell can be selected
var isCellSelectable = self.isCellSelectable(colIdx, cellData);
// If checkbox is checked
if(
Object.prototype.hasOwnProperty.call(ctx.checkboxes.s.data, colIdx)
&& Object.prototype.hasOwnProperty.call(ctx.checkboxes.s.data[colIdx], cellData)
) {
// If row selection is enabled
// and checkbox can be checked
if(ctx.aoColumns[colIdx].checkboxes.selectRow && isCellSelectable){
self.updateSelect(rowIdx, true);
}
self.updateCheckbox(this, colIdx, true);
}
// If checkbox is disabled
if(!isCellSelectable){
$('input.dt-checkboxes', this.node()).prop('disabled', true);
}
});
},
// Handles checkbox click event
onClick: function(e, ctrl){
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
var cellSelector;
// Get cell
var $cell = $(ctrl).closest('td');
// If cell is in a fixed column using FixedColumns extension
if($cell.parents('.DTFC_Cloned').length){
cellSelector = dt.fixedColumns().cellIndex($cell);
} else {
cellSelector = $cell;
}
var cell = dt.cell(cellSelector);
var cellIdx = cell.index();
var colIdx = cellIdx.column;
var rowIdx = cellIdx.row;
// If row selection is not enabled
// NOTE: if row selection is enabled, checkbox selection/deselection
// would be handled by onDataTablesSelectDeselect event handler instead
if(!ctx.aoColumns[colIdx].checkboxes.selectRow){
cell.checkboxes.select(ctrl.checked);
// Prevent click event from propagating to parent
e.stopPropagation();
} else {
// If Select extension is enabled
if(ctx._select){
// If style is set to "os"
if(ctx._select.style === 'os'){
// WORKAROUND:
// See https://github.com/gyrocode/jquery-datatables-checkboxes/issues/128
// Prevent click event from propagating to parent
e.stopPropagation();
// Select/deselect individual row
cell.checkboxes.select(ctrl.checked);
// Otherwise, if style is set to other than "os"
} else {
// WORKAROUND:
// Select extension may keep the row selected
// when checkbox is unchecked with SHIFT key.
//
// We need to update the state of the checkbox AFTER handling
// select/deselect event from Select extension.
//
// Call to setTimeout is needed to let select/deselect event handler
// update the data first.
setTimeout(function(){
// Get cell data
var cellData = cell.data();
// Determine whether data is in the list
var hasData = (
Object.prototype.hasOwnProperty.call(self.s.data, colIdx)
&& Object.prototype.hasOwnProperty.call(self.s.data[colIdx], cellData)
);
// If state of the checkbox needs to be updated
if(hasData !== ctrl.checked){
self.updateCheckbox(cell, colIdx, hasData);
self.updateSelectAll(colIdx);
}
}, 0);
}
}
}
},
// Handles checkbox click event
onClickSelectAll: function(e, ctrl){
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
// Calculate column index
var colIdx = null;
var $th = $(ctrl).closest('th');
// If column is fixed using FixedColumns extension
if($th.parents('.DTFC_Cloned').length){
var cellIdx = dt.fixedColumns().cellIndex($th);
colIdx = cellIdx.column;
} else {
colIdx = dt.column($th).index();
}
// Indicate that state of "Select all" control has been changed
$(ctrl).data('is-changed', true);
dt.column(colIdx, {
page: (
(ctx.aoColumns[colIdx].checkboxes && ctx.aoColumns[colIdx].checkboxes.selectAllPages)
? 'all'
: 'current'
),
search: 'applied'
}).checkboxes.select(ctrl.checked);
// Prevent click event from propagating to parent
e.stopPropagation();
},
// Loads previosly saved sate
loadState: function () {
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
// If state saving is enabled
if(ctx.oFeatures.bStateSave){
// Retrieve stored state
var state = dt.state.loaded();
// For every column where checkboxes are enabled
$.each(self.s.columns, function(index, colIdx){
// If state is loaded and contains data for this column
if(state && state.checkboxes && state.checkboxes.hasOwnProperty(colIdx)){
// If checkbox state saving is enabled
if(ctx.aoColumns[colIdx].checkboxes.stateSave){
// Load previous state
self.s.data[colIdx] = state.checkboxes[colIdx];
}
}
});
}
},
// Updates state of the "Select all" controls
updateSelectAll: function(colIdx){
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
// If Checkboxes extension is enabled for this column
// and "Select all" control is enabled for this column
if(ctx.aoColumns[colIdx].checkboxes && ctx.aoColumns[colIdx].checkboxes.selectAll){
var cells = dt.cells('tr', colIdx, {
page: (
(ctx.aoColumns[colIdx].checkboxes.selectAllPages)
? 'all'
: 'current'
),
search: 'applied'
});
var $tableContainer = dt.table().container();
var $checkboxesSelectAll = $('.dt-checkboxes-select-all[data-col="' + colIdx + '"] input[type="checkbox"]', $tableContainer);
var countChecked = 0;
var countDisabled = 0;
var cellsData = cells.data();
$.each(cellsData, function(index, cellData){
// If checkbox is not disabled
if(self.isCellSelectable(colIdx, cellData)){
if(
Object.prototype.hasOwnProperty.call(self.s.data, colIdx)
&& Object.prototype.hasOwnProperty.call(self.s.data[colIdx], cellData)
) {
countChecked++;
}
// Otherwise, if checkbox is disabled
} else {
countDisabled++;
}
});
// If FixedHeader is enabled in this instance
if(ctx._fixedHeader){
// If header is floating in this instance
if(ctx._fixedHeader.dom['header'].floating){
$checkboxesSelectAll = $('.fixedHeader-floating .dt-checkboxes-select-all[data-col="' + colIdx + '"] input[type="checkbox"]');
}
}
var isSelected;
var isIndeterminate;
// If none of the checkboxes are checked
if (countChecked === 0){
isSelected = false;
isIndeterminate = false;
// If all of the checkboxes are checked
} else if ((countChecked + countDisabled) === cellsData.length){
isSelected = true;
isIndeterminate = false;
// If some of the checkboxes are checked
} else {
isSelected = true;
isIndeterminate = true;
}
var isChanged = $checkboxesSelectAll.data('is-changed');
var isSelectedNow = $checkboxesSelectAll.prop('checked');
var isIndeterminateNow = $checkboxesSelectAll.prop('indeterminate');
// If state of "Select all" control has been changed
if(isChanged || isSelectedNow !== isSelected || isIndeterminateNow !== isIndeterminate){
// Reset "Select all" control state flag
$checkboxesSelectAll.data('is-changed', false);
$checkboxesSelectAll.prop({
// NOTE: If checkbox has indeterminate state,
// "checked" property must be set to false.
'checked': isIndeterminate ? false : isSelected,
'indeterminate': isIndeterminate
});
// If selectAllCallback is a function
if($.isFunction(ctx.aoColumns[colIdx].checkboxes.selectAllCallback)){
ctx.aoColumns[colIdx].checkboxes.selectAllCallback($checkboxesSelectAll.closest('th').get(0), isSelected, isIndeterminate);
}
}
}
},
// Updates the information element of the DataTable showing information about the
// items selected. Based on info() method of Select extension.
showInfoSelected: function(){
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
if ( ! ctx.aanFeatures.i ) {
return;
}
// Get index of the first column that has checkbox and row selection enabled
var colIdx = self.getSelectRowColIndex();
// If there is a column that has checkbox and row selection enabled
if(colIdx !== null){
// Count number of selected rows
var countRows = 0;
for (var cellData in ctx.checkboxes.s.data[colIdx]){
if(
Object.prototype.hasOwnProperty.call(ctx.checkboxes.s.data, colIdx)
&& Object.prototype.hasOwnProperty.call(ctx.checkboxes.s.data[colIdx], cellData)
) {
countRows++;
}
}
var add = function($el, name, num){
$el.append( $('<span class="select-item"/>').append( dt.i18n(
'select.'+name+'s',
{ _: '%d '+name+'s selected', 0: '', 1: '1 '+name+' selected' },
num
) ) );
};
// Internal knowledge of DataTables to loop over all information elements
$.each( ctx.aanFeatures.i, function ( i, el ) {
var $el = $(el);
var $output = $('<span class="select-info"/>');
add($output, 'row', countRows);
var $existing = $el.children('span.select-info');
if($existing.length){
$existing.remove();
}
if($output.text() !== ''){
$el.append($output);
}
});
}
},
// Determines whether checkbox in the cell can be checked
isCellSelectable: function(colIdx, cellData){
var self = this;
var ctx = self.s.ctx;
// If data is in the list of disabled elements
if(
Object.prototype.hasOwnProperty.call(ctx.checkboxes.s.dataDisabled, colIdx)
&& Object.prototype.hasOwnProperty.call(ctx.checkboxes.s.dataDisabled[colIdx], cellData)
) {
return false;
// Otherwise, if checkbox can be selected
} else {
return true;
}
},
// Gets cell index
getCellIndex: function(cell){
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
// If FixedColumns extension is available
if(ctx._oFixedColumns){
return dt.fixedColumns().cellIndex(cell);
} else {
return dt.cell(cell).index();
}
},
// Gets index of the first column that has checkbox and row selection enabled
getSelectRowColIndex: function(){
var self = this;
var ctx = self.s.ctx;
var colIdx = null;
for(var i = 0; i < ctx.aoColumns.length; i++){
// If Checkboxes extension is enabled
// and row selection is enabled for this column
if(ctx.aoColumns[i].checkboxes && ctx.aoColumns[i].checkboxes.selectRow){
colIdx = i;
break;
}
}
return colIdx;
},
// Updates fixed column if FixedColumns extension is enabled
// and given column is inside a fixed column
updateFixedColumn: function(colIdx){
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
// If FixedColumns extension is enabled
if(ctx._oFixedColumns){
var leftCols = ctx._oFixedColumns.s.iLeftColumns;
var rightCols = ctx.aoColumns.length - ctx._oFixedColumns.s.iRightColumns - 1;
if (colIdx < leftCols || colIdx > rightCols){
// Update the data shown in the fixed column
dt.fixedColumns().update();
// Use delay to let FixedColumns construct the header
// before we update the "Select all" checkbox
setTimeout(function(){
// For every column where checkboxes are enabled
$.each(self.s.columns, function(index, colIdx){
self.updateSelectAll(colIdx);
});