Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jquery-datatables-checkboxes

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jquery-datatables-checkboxes - npm Package Compare versions

Comparing version 1.1.3 to 1.2.0

js/dataTables.checkboxes - Copy.js

894

js/dataTables.checkboxes.js

@@ -1,2 +0,2 @@

/*! Checkboxes 1.1.3
/*! Checkboxes 1.2.0
* Copyright (c) Gyrocode (www.gyrocode.com)

@@ -9,3 +9,3 @@ * License: MIT License

* @description Checkboxes extension for jQuery DataTables
* @version 1.1.3
* @version 1.2.0
* @file dataTables.checkboxes.js

@@ -46,4 +46,4 @@ * @author Gyrocode (http://www.gyrocode.com/projects/jquery-datatables-checkboxes/)

var DataTable = $.fn.dataTable;
/**

@@ -81,4 +81,7 @@ * Checkboxes is an extension for the jQuery DataTables library that provides

// Get settings object
this.s.ctx = this.s.dt.settings()[0];
// Check if checkboxes have already been initialised on this table
if ( this.s.dt.settings()[0].checkboxes ) {
if ( this.s.ctx.checkboxes ) {
return;

@@ -107,3 +110,3 @@ }

var dt = self.s.dt;
var ctx = dt.settings()[0];
var ctx = self.s.ctx;
var hasCheckboxes = false;

@@ -117,2 +120,4 @@ var hasCheckboxesSelectRow = false;

if (ctx.aoColumns[i].checkboxes){
var $colHeader = $(dt.column(i).header());
//

@@ -134,38 +139,34 @@ // INITIALIZATION

//
// WORKAROUNDS:
// OPTIONS
//
// DataTables doesn't support natively ability to modify settings on the fly.
// The following code is a workaround that deals with possible consequences.
DataTable.ext.internal._fnApplyColumnDefs(ctx, [{
'targets': i,
'searchable': false,
'orderable': false,
'width':'1%',
'className': 'dt-body-center',
'render': function (data, type, full, meta){
if(type === 'display'){
data = '<input type="checkbox" class="dt-checkboxes">';
}
return data;
}
}], {}, function (iCol, oDef) {
DataTable.ext.internal._fnColumnOptions( ctx, iCol, oDef );
});
var colOptions = {
'searchable': false,
'orderable': false
};
// Remove "sorting" class
$(dt.column(i).header())
.removeClass('sorting');
if(ctx.aoColumns[i].sClass === ''){
colOptions['className'] = 'dt-body-center';
}
// Detach all event handlers for this column
$(dt.column(i).header()).off('.dt');
if(ctx.aoColumns[i].sWidthOrig === null){
colOptions['width'] = '1%';
}
// Invalidate column data
var cells = dt.cells('tr', i);
cells.invalidate('data');
if(ctx.aoColumns[i].mRender === null){
colOptions['render'] = function(){
return '<input type="checkbox" class="dt-checkboxes">';
};
}
// Add required class to existing cells
$(cells.nodes()).addClass('dt-body-center');
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');
//

@@ -175,4 +176,4 @@ // DATA

// Initialize array holding data for selected checkboxes
self.s.data[i] = [];
// Initialize object holding data for selected checkboxes
self.s.data[i] = {};

@@ -193,10 +194,21 @@ // If state is loaded and contains data for this column

// If row selection is enabled for this column
if(ctx.aoColumns[i].checkboxes.selectRow){
hasCheckboxesSelectRow = true;
$(dt.table().node()).addClass('dt-checkboxes-select');
// 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(ctx.aoColumns[i].checkboxes.selectAll){
$(dt.column(i).header())
// Save previous HTML content
$colHeader.data('html', $colHeader.html());
$colHeader
.html('<input type="checkbox">')

@@ -209,3 +221,2 @@ .addClass('dt-checkboxes-select-all')

// If table has at least one checkbox

@@ -222,37 +233,19 @@ if(hasCheckboxes){

// Handles checkbox click event
$tableBody.on('click', 'input.dt-checkboxes', function(e){
self.onClick(e, this);
});
// Handle row select/deselect event
// If there is at least one column that has row selection enabled
if(hasCheckboxesSelectRow){
// If Select extension is available
if(DataTable.select){
// Handle row selection event
$table.on('select.dt deselect.dt', function(e, api, type, indexes){
self.onSelect(e, type, indexes);
});
$table.addClass('dt-checkboxes-select');
// Disable Select extension information display
dt.select.info(false);
// Handle row select/deselect event
$table.on('select.dt.dtCheckboxes deselect.dt.dtCheckboxes', function(e, api, type, indexes){
self.onSelect(e, type, indexes);
});
// Otherwise, if Select extension is not available
} else {
$tableContainer.on('click', 'tbody td', function(){
var $row = $(this).closest('tr');
var e = {
type: ($row.hasClass('selected') ? 'deselect' : 'select')
};
// Disable Select extension information display
dt.select.info(false);
self.onSelect(e, 'row', [dt.row($row).index()]);
$row.toggleClass('selected');
$table.trigger(e.type);
});
}
// Update the table information element with selected item summary
$table.on('draw.dt select.dt deselect.dt', function (){
//
// 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();

@@ -262,9 +255,43 @@ });

// Handle Ajax request completion event
$table.on('xhr.dt', function ( e, settings, json, xhr ) {
// Retrieve stored state
var state = dt.state.loaded();
$.each(self.s.columns, function(index, colIdx){
// Clear data
self.s.data[colIdx] = {};
// If state is loaded and contains data for this column
if(state && state.checkboxes && state.checkboxes.hasOwnProperty(colIdx)){
// Load previous state
self.s.data[colIdx] = state.checkboxes[colIdx];
}
});
// If state saving is enabled
if(ctx.oFeatures.bStateSave){
// If server-side processing mode is not enabled
// NOTE: Needed to avoid duplicate call to updateCheckboxes() in onDraw()
if(!ctx.oFeatures.bServerSide){
// Update table state on next redraw
$table.one('draw.dt.dtCheckboxes', function(e){
self.updateState();
});
}
}
});
// Handle table draw event
$table.on('draw.dt', function(e, ctx){
self.onDraw(e, ctx);
$table.on('draw.dt.dtCheckboxes', function(e){
self.onDraw(e);
});
// Handles 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', 'thead th.dt-checkboxes-select-all input[type="checkbox"]', function(e){
$tableContainer.on('click.dtCheckboxes', 'thead th.dt-checkboxes-select-all input[type="checkbox"]', function(e){
self.onClickSelectAll(e, this);

@@ -274,3 +301,3 @@ });

// Handle click on heading containing "Select all" control
$tableContainer.on('click', 'thead th.dt-checkboxes-select-all', function(e) {
$tableContainer.on('click.dtCheckboxes', 'thead th.dt-checkboxes-select-all', function(e) {
$('input[type="checkbox"]', this).trigger('click');

@@ -280,3 +307,3 @@ });

// Handle click on "Select all" control in floating fixed header
$(document).on('click', '.fixedHeader-floating thead th.dt-checkboxes-select-all input[type="checkbox"]', function(e){
$(document).on('click.dtCheckboxes', '.fixedHeader-floating thead th.dt-checkboxes-select-all input[type="checkbox"]', function(e){
// If FixedHeader is enabled in this instance

@@ -292,3 +319,3 @@ if(ctx._fixedHeader){

// Handle click on heading containing "Select all" control in floating fixed header
$(document).on('click', '.fixedHeader-floating thead th.dt-checkboxes-select-all', function(e) {
$(document).on('click.dtCheckboxes', '.fixedHeader-floating thead th.dt-checkboxes-select-all', function(e) {
// If FixedHeader is enabled in this instance

@@ -303,10 +330,43 @@ if(ctx._fixedHeader){

// If stave saving is enabled
if(ctx.oFeatures.bStateSave){
// Handle state saving event
dt.on('stateSaveParams.checkboxes', function (e, settings, data){
// Store data associated with this plug-in
data.checkboxes = self.s.data;
// Handle table initialization event
$table.on('init.dt.dtCheckboxes', function(){
// If state saving is enabled
if(ctx.oFeatures.bStateSave){
// If server-side processing mode is not enabled
// NOTE: Needed to avoid duplicate call to updateCheckboxes() in onDraw()
if(!ctx.oFeatures.bServerSide){
self.updateState();
}
// Handle state saving event
$table.on('stateSaveParams.dt.dtCheckboxes', function (e, settings, data){
// Store data associated with this plug-in
data.checkboxes = self.s.data;
});
}
});
// Handle table destroy event
$table.one('destroy.dt.dtCheckboxes', function(e, settings){
// Detach event handlers
$(document).off('click.dtCheckboxes');
$tableContainer.on('.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 = {};
// Remove added elements
$('.dt-checkboxes-select-all', $table).each(function(index, el){
$(el)
.html($(el).data('html'))
.removeClass('dt-checkboxes-select-all');
});
}
});
}

@@ -316,36 +376,23 @@ },

// Updates array holding data for selected checkboxes
updateData: function(type, selector, isSelected){
updateData: function(type, selector, isSelected, allowDups){
var self = this;
var dt = self.s.dt;
var ctx = dt.settings()[0];
var ctx = self.s.ctx;
var nodes = [];
if(type === 'row'){
dt.rows(selector).every(function(rowIdx){
for(var colIdx = 0; colIdx < ctx.aoColumns.length; colIdx++){
// If Checkboxes extension is enabled
// and row selection is enabled for this column
if(ctx.aoColumns[colIdx].checkboxes && ctx.aoColumns[colIdx].checkboxes.selectRow){
nodes.push(dt.cell(rowIdx, colIdx).node());
}
}
});
// By default, duplicate data is not allowed
if(typeof allowDups === 'undefined'){ allowDups = false; }
} else if(type === 'cell'){
nodes = dt.cells(selector).nodes();
var cellSelector;
$.each(nodes, function(){
var cellIdx = self.getCellIndex(this);
var colIdx = cellIdx.column;
var rowIdx = cellIdx.row;
if(type === 'cell'){
cellSelector = selector;
// If Checkboxes extension is enabled
// and row selection is enabled for this column
if(ctx.aoColumns[colIdx].checkboxes && ctx.aoColumns[colIdx].checkboxes.selectRow){
// Get list of columns other than this cell's column
// where Checkboxes extension is enabled
var columns = $.grep(self.s.columns, function(value){ return value != colIdx; });
} else if(type === 'row'){
cellSelector = [];
// Add cells from other columns
$.merge(nodes, dt.cells(rowIdx, columns).nodes());
dt.rows(selector).every(function(rowIdx){
// Get index of the first column that has checkbox and row selection enabled
var colIdx = self.getSelectRowColIndex();
if(colIdx !== null){
cellSelector.push({ row: rowIdx, column: colIdx });
}

@@ -355,28 +402,41 @@ });

if(nodes.length){
dt.cells(nodes).every(function () {
var cellCol = this.index().column;
dt.cells(cellSelector).every(function (cellRow, cellCol) {
// If Checkboxes extension is enabled for this column
if(ctx.aoColumns[cellCol].checkboxes){
// Get cell data
var cellData = this.data();
// If Checkboxes extension is enabled for this column
if(ctx.aoColumns[cellCol].checkboxes){
// Get cell data
var cellData = this.data();
// Determine whether data is in the list
var hasData = ctx.checkboxes.s.data[cellCol].hasOwnProperty(cellData);
// Determine whether data is in the list
var index = $.inArray(cellData, ctx.checkboxes.s.data[cellCol]);
// If checkbox is checked and data is not in the list
if(isSelected){
// If data is available and duplicates are allowed
if(hasData && allowDups){
ctx.checkboxes.s.data[cellCol][cellData]++;
// If checkbox is checked and data is not in the list
if(isSelected && index === -1){
ctx.checkboxes.s.data[cellCol].push(cellData);
// Otherwise, if data is not available or duplicates are not allowed
} else {
ctx.checkboxes.s.data[cellCol][cellData] = 1;
}
// Otherwise, if checkbox is not checked and data is in the list
} else if (!isSelected && index !== -1){
ctx.checkboxes.s.data[cellCol].splice(index, 1);
// Otherwise, if checkbox is not checked and data is in the list
} else if (!isSelected && hasData){
// If only data counter equals to 1 or duplicates are not allowed
if(ctx.checkboxes.s.data[cellCol][cellData] === 1 || !allowDups){
delete ctx.checkboxes.s.data[cellCol][cellData];
// Otherwise, if data counter is greater than 1 and duplicates are allowed
} else {
ctx.checkboxes.s.data[cellCol][cellData]--;
}
}
});
}
});
// If state saving is enabled
if(ctx.oFeatures.bStateSave){
// Save state
dt.state.save();
}
// Save state
dt.state.save();
},

@@ -388,46 +448,52 @@

var dt = self.s.dt;
var ctx = dt.settings()[0];
var ctx = self.s.ctx;
var nodes = [];
if(type === 'row'){
nodes = dt.rows(selector).nodes();
// If Select extension is enabled
if(ctx._select){
// Disable select event hanlder temporarily
self.s.ignoreSelect = true;
} else if(type === 'cell'){
var cellNodes = dt.cells(selector).nodes();
if(isSelected){
dt.rows(selector).select();
} else {
dt.rows(selector).deselect();
}
$.each(cellNodes, function(){
var cellIdx = self.getCellIndex(this);
var colIdx = cellIdx.column;
var rowIdx = cellIdx.row;
// Re-enable select event handler
self.s.ignoreSelect = false;
}
},
// If Checkboxes extension is enabled
// and row selection is enabled for this column
if(ctx.aoColumns[colIdx].checkboxes && ctx.aoColumns[colIdx].checkboxes.selectRow){
nodes.push(dt.$(this).closest('tr').get(0));
// Updates state of single checkbox
updateCheckbox: function(type, selector, isSelected){
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
var cellSelector = [];
if(type === 'row'){
dt.rows(selector).every(function(rowIdx){
// Get index of the first column that has checkbox and row selection enabled
var colIdx = self.getSelectRowColIndex();
if(colIdx !== null){
cellSelector.push({ row: rowIdx, column: colIdx });
}
});
} else if(type === 'cell'){
cellSelector = selector;
}
var nodes = dt.cells(cellSelector).nodes();
if(nodes.length){
// If Select extension is available
if(DataTable.select){
// Disable select event hanlder temporarily
self.s.ignoreSelect = true;
$('input.dt-checkboxes', nodes).prop('checked', isSelected);
if(isSelected){
dt.rows(nodes).select();
} else {
dt.rows(nodes).deselect();
}
// NOTE: For performance reasons assume that cellSelector is always
// an array of objects with two properties: "row" and "column".
var colIdx = cellSelector[0].column;
// Re-enable select event handler
self.s.ignoreSelect = false;
// Otherwise, if Select extension is not available
} else {
if(isSelected){
dt.$(nodes).addClass('selected');
} else {
dt.$(nodes).removeClass('selected');
}
// If selectCallback is a function
if($.isFunction(ctx.aoColumns[colIdx].checkboxes.selectCallback)){
ctx.aoColumns[colIdx].checkboxes.selectCallback(nodes, isSelected);
}

@@ -437,51 +503,49 @@ }

// Updates row selection
updateCheckbox: function(type, selector, isSelected){
// Update table state
updateState: function(){
var self = this;
var dt = self.s.dt;
var ctx = dt.settings()[0];
var ctx = self.s.ctx;
var nodes = [];
if(type === 'row'){
dt.rows(selector).every(function(rowIdx){
for(var colIdx = 0; colIdx < ctx.aoColumns.length; colIdx++){
if(ctx.aoColumns[colIdx].checkboxes && ctx.aoColumns[colIdx].checkboxes.selectRow){
nodes.push(dt.cell(rowIdx, colIdx).node());
}
}
});
self.updateCheckboxes({ page: 'all', search: 'none' });
} else if(type === 'cell'){
nodes = dt.cells(selector).nodes();
$.each(self.s.columns, function(index, colIdx){
self.updateSelectAll(colIdx);
});
},
$.each(nodes, function(){
var cellIdx = self.getCellIndex(this);
var colIdx = cellIdx.column;
var rowIdx = cellIdx.row;
// Updates state of multiple checkboxes
updateCheckboxes: function(opts){
var self = this;
var dt = self.s.dt;
var ctx = self.s.ctx;
// If Checkboxes extension is enabled
// and row selection is enabled for this column
if(ctx.aoColumns[colIdx].checkboxes && ctx.aoColumns[colIdx].checkboxes.selectRow){
// Get list of columns other than this cell's column
// where Checkboxes extension is enabled
// and row selection is enabled
var columns = $.grep(self.s.columns, function(value){
return (value != colIdx) && ctx.aoColumns[value].checkboxes.selectRow;
});
// Enumerate all cells
var dataSeen = {};
dt.cells('tr', self.s.columns, opts).every(function(cellRow, cellCol){
// Get cell data
var cellData = this.data();
// If there are other columns
if(columns.length){
// Add cells from other columns
$.merge(nodes, dt.cells(rowIdx, columns).nodes());
// If data is in the list
if(ctx.checkboxes.s.data[cellCol].hasOwnProperty(cellData)){
// Determine how many times cell with given data was already selected
if(dataSeen.hasOwnProperty(cellData)){
dataSeen[cellData]++;
} else {
dataSeen[cellData] = 1;
}
// If cell needs to be selected
if(dataSeen[cellData] <= ctx.checkboxes.s.data[cellCol][cellData]){
self.updateCheckbox('cell', [{ row: cellRow, column: cellCol }], true);
// If row selection is enabled
if(ctx.aoColumns[cellCol].checkboxes.selectRow){
self.updateSelect('row', cellRow, true);
}
}
});
}
if(nodes.length){
dt.$(nodes).find('input.dt-checkboxes').prop('checked', isSelected);
}
}
});
},
// Handles checkbox click event

@@ -491,21 +555,46 @@ onClick: function(e, ctrl){

var dt = self.s.dt;
var ctx = dt.settings()[0];
var ctx = self.s.ctx;
// Get cell
var $cell = $(ctrl).closest('td');
var cellSelector;
// TODO: Possibly replace with dt.cells($cell).checkbox.select()
// Get cell and column index
var cellIdx = this.getCellIndex($(ctrl).closest('td'));
var colIdx = cellIdx.column;
// Get cell's column index
var cellIdx = self.getCellIndex($cell);
// Get cell
var cell = dt.cell(cellIdx);
// If Checkboxes extension is enabled for this column
if(ctx.aoColumns[cellIdx.column].checkboxes){
self.updateCheckbox('cell', cellIdx, ctrl.checked);
self.updateData('cell', cellIdx, ctrl.checked);
self.updateSelect('cell', cellIdx, ctrl.checked);
self.updateSelectAll();
// If row selection is not enabled
// NOTE: if row selection is enabled, checkbox selection/deselection
// would be handled by onSelect event instead
if(!ctx.aoColumns[colIdx].checkboxes.selectRow){
cell.checkboxes.select(ctrl.checked, true);
// Prevent click event from propagating to parent
e.stopPropagation();
// Otherwise, if row selection is enabled
} 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 = self.s.data[colIdx].hasOwnProperty(cellData);
// If state of the checkbox needs to be updated
if(hasData !== ctrl.checked){
self.updateCheckbox('cell', [cellIdx], hasData);
self.updateSelectAll(colIdx);
}
}, 0);
}

@@ -518,3 +607,3 @@ },

var dt = self.s.dt;
var ctx = dt.settings()[0];
var ctx = self.s.ctx;

@@ -524,5 +613,24 @@ if(self.s.ignoreSelect){ return; }

if(type === 'row'){
// By default, allow duplicate data
var allowDup = true;
// WORKAROUND:
// Select extension may generate multiple select events for the same row
// when selecting rows using SHIFT key and the following styles are used
// 'os', 'multi+shift'.
//
// If user is selecting/deselecting multiple rows using SHIFT key
if((ctx._select.style === 'os' || ctx._select.style === 'multi+shift') && indexes.length > 1){
// Disallow handling of rows with duplicate data
allowDup = false;
}
self.updateData('row', indexes, (e.type === 'select') ? true : false, allowDup);
self.updateCheckbox('row', indexes, (e.type === 'select') ? true : false);
self.updateData('row', indexes, (e.type === 'select') ? true : false);
self.updateSelectAll();
// Get index of the first column that has checkbox and row selection enabled
var colIdx = self.getSelectRowColIndex();
if(colIdx !== null){
self.updateSelectAll(colIdx);
}
}

@@ -535,19 +643,11 @@ },

var dt = self.s.dt;
var ctx = dt.settings()[0];
var ctx = self.s.ctx;
// Calculate column index
var col = null;
var $th = $(ctrl).closest('th');
// Get cell and column index
var cellIdx = this.getCellIndex($(ctrl).closest('th'));
var colIdx = cellIdx.column;
// If column is fixed using FixedColumns extension
if($th.parents('.DTFC_Cloned').length){
var cellIdx = dt.fixedColumns().cellIndex($th);
col = cellIdx.column;
} else {
col = dt.column($th).index();
}
var cells = dt.cells('tr', col, {
dt.column(colIdx, {
page: (
(ctx.aoColumns[col].checkboxes && ctx.aoColumns[col].checkboxes.selectAllPages)
(ctx.aoColumns[colIdx].checkboxes && ctx.aoColumns[colIdx].checkboxes.selectAllPages)
? 'all'

@@ -557,29 +657,5 @@ : 'current'

search: 'applied'
});
}).checkboxes.select(ctrl.checked, true);
self.updateData('cell', cells.nodes(), ctrl.checked);
self.updateCheckbox('cell', cells.nodes(), ctrl.checked);
// If row selection is enabled
if(ctx.aoColumns[col].checkboxes.selectRow){
var rows = dt.rows({
page: (
(ctx.aoColumns[col].checkboxes && ctx.aoColumns[col].checkboxes.selectAllPages)
? 'all'
: 'current'
),
search: 'applied'
});
self.updateSelect('row', rows.nodes(), ctrl.checked);
}
self.updateSelectAll();
// If column is fixed using FixedColumns extension
if($th.parents('.DTFC_Cloned').length){
// Update columns in the cloned table
dt.fixedColumns().update();
}
// Prevent click event from propagating to parent
e.stopPropagation();

@@ -589,91 +665,76 @@ },

// Handles table draw event
onDraw: function(e, ctx){
onDraw: function(e){
var self = this;
var dt = self.s.dt;
ctx = dt.settings()[0];
var ctx = self.s.ctx;
var rows_seen = {};
// Enumerate all cells
dt.cells('tr', self.s.columns, { page: 'current', search: 'applied' }).every(function(){
var cellColIdx = this.index().column;
var cellRowIdx = this.index().row;
// If server-side processing is enabled
if(ctx.oFeatures.bServerSide){
self.updateCheckboxes({ page: 'current', search: 'none' });
}
// Get cell data
var cellData = this.data();
// Determine whether data is in the list
var index = $.inArray(cellData, ctx.checkboxes.s.data[cellColIdx]);
// If data is in the list
if(index !== -1){
// If this row hasn't been processed yet
if(!rows_seen.hasOwnProperty(cellRowIdx)){
self.updateCheckbox('cell', this.node(), true);
self.updateSelect('cell', this.node(), true);
if(ctx.aoColumns[cellColIdx].checkboxes && ctx.aoColumns[cellColIdx].checkboxes.selectRow){
// Mark row as processed
rows_seen[cellRowIdx] = true;
}
}
}
$.each(self.s.columns, function(index, colIdx){
self.updateSelectAll(colIdx);
});
self.updateSelectAll();
},
// Updates state of the "Select all" controls
updateSelectAll: function(){
updateSelectAll: function(colIdx){
var self = this;
var dt = self.s.dt;
var ctx = dt.settings()[0];
var ctx = self.s.ctx;
for(var colIdx = 0; colIdx < ctx.aoColumns.length; colIdx++){
// 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'
});
// 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 $checkboxes = dt.$(cells.nodes()).find('.dt-checkboxes');
var $checkboxesChecked = dt.$(cells.nodes()).find('.dt-checkboxes:checked');
var $checkboxesSelectAll = $('.dt-checkboxes-select-all[data-col="' + colIdx + '"] input[type="checkbox"]', $tableContainer);
var $tableContainer = dt.table().container();
var $checkboxes = $('.dt-checkboxes', cells.nodes());
var $checkboxesChecked = $checkboxes.filter(':checked');
var $checkboxesSelectAll = $('.dt-checkboxes-select-all[data-col="' + colIdx + '"] input[type="checkbox"]', $tableContainer);
// 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"]');
}
// 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"]');
}
}
// If none of the checkboxes are checked
if ($checkboxesChecked.length === 0) {
$checkboxesSelectAll.prop({
'checked': false,
'indeterminate': false
});
var isSelected;
var isIndeterminate;
// If all of the checkboxes are checked
} else if ($checkboxesChecked.length === $checkboxes.length) {
$checkboxesSelectAll.prop({
'checked': true,
'indeterminate': false
});
// If none of the checkboxes are checked
if ($checkboxesChecked.length === 0) {
isSelected = false;
isIndeterminate = false;
// If some of the checkboxes are checked
} else {
$checkboxesSelectAll.prop({
'checked': true,
'indeterminate': true
});
}
// If all of the checkboxes are checked
} else if ($checkboxesChecked.length === $checkboxes.length) {
isSelected = true;
isIndeterminate = false;
// If some of the checkboxes are checked
} else {
isSelected = true;
isIndeterminate = true;
}
$checkboxesSelectAll.prop({
'checked': 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);
}
}

@@ -687,3 +748,3 @@ },

var dt = self.s.dt;
var ctx = dt.settings()[0];
var ctx = self.s.ctx;

@@ -703,17 +764,17 @@ if ( ! ctx.aanFeatures.i ) {

// Find index of the first column that has checkbox and row selection enabled
var colSelectRowIdx = -1;
for(var colIdx = 0; colIdx < ctx.aoColumns.length; colIdx++){
// If Checkboxes extension is enabled
// and row selection is enabled for this column
if(ctx.aoColumns[colIdx].checkboxes && ctx.aoColumns[colIdx].checkboxes.selectRow){
colSelectRowIdx = colIdx;
break;
}
}
// 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(colSelectRowIdx !== -1){
add('row', ctx.checkboxes.s.data[colSelectRowIdx].length);
if(colIdx !== null){
// Count number of selected rows
var countRows = 0;
for (var cellData in ctx.checkboxes.s.data[colIdx]){
if (ctx.checkboxes.s.data[colIdx].hasOwnProperty(cellData)){
countRows += ctx.checkboxes.s.data[colIdx][cellData];
}
}
add('row', countRows);
// Internal knowledge of DataTables to loop over all information elements

@@ -739,6 +800,6 @@ $.each( ctx.aanFeatures.i, function ( i, el ) {

var dt = self.s.dt;
var ctx = dt.settings()[0];
var ctx = self.s.ctx;
// If FixedColumns extension is available
if(DataTable.FixedColumns){
if(ctx._oFixedColumns){
return dt.fixedColumns().cellIndex(cell);

@@ -749,2 +810,39 @@

}
},
// Gets index of the first column that has checkbox and row selection enabled
getSelectRowColIndex: function(){
var self = this;
var dt = self.s.dt;
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){
dt.fixedColumns().update();
}
}
}

@@ -766,3 +864,3 @@ };

* @type {Boolean}
* @default `false`
* @default `false`
*/

@@ -775,3 +873,3 @@ selectRow: false,

* @type {Boolean}
* @default `true`
* @default `true`
*/

@@ -784,5 +882,21 @@ selectAll: true,

* @type {Boolean}
* @default `true`
* @default `true`
*/
selectAllPages: true
selectAllPages: true,
/**
* Checkbox select/deselect callback
*
* @type {Function}
* @default `null`
*/
selectCallback: null,
/**
* "Select all" control select/deselect callback
*
* @type {Function}
* @default `null`
*/
selectAllCallback: null
};

@@ -801,12 +915,31 @@

Api.registerPlural( 'columns().checkboxes.select()', 'column().checkboxes.select()', function ( select ) {
Api.registerPlural( 'columns().checkboxes.select()', 'column().checkboxes.select()', function ( select, allowDups ) {
if(typeof select === 'undefined'){ select = true; }
return this.iterator( 'column', function (ctx, colIdx){
return this.iterator( 'column-rows', function ( ctx, colIdx, i, j, rowsIdx ) {
if(ctx.checkboxes){
var selector = this.cells('tr', colIdx).nodes();
var selector = [];
$.each(rowsIdx, function(index, rowIdx){
selector.push({ row: rowIdx, column: colIdx });
});
ctx.checkboxes.updateData('cell', selector, (select) ? true : false, allowDups);
ctx.checkboxes.updateCheckbox('cell', selector, (select) ? true : false);
ctx.checkboxes.updateData('cell', selector, (select) ? true : false);
ctx.checkboxes.updateSelect('cell', selector, (select) ? true : false);
ctx.checkboxes.updateSelectAll();
// If row selection is enabled
if(ctx.aoColumns[colIdx].checkboxes.selectRow){
ctx.checkboxes.updateSelect('row', rowsIdx, select);
}
// If FixedColumns extension is enabled
if(ctx._oFixedColumns){
// Use timeout to let FixedColumns construct the header
// before we update the "Select all" checkbox
setTimeout(function(){ ctx.checkboxes.updateSelectAll(colIdx); }, 0);
} else {
ctx.checkboxes.updateSelectAll(colIdx);
}
ctx.checkboxes.updateFixedColumn(colIdx);
}

@@ -816,3 +949,3 @@ }, 1 );

Api.registerPlural( 'cells().checkboxes.select()', 'cell().checkboxes.select()', function ( select ) {
Api.registerPlural( 'cells().checkboxes.select()', 'cell().checkboxes.select()', function ( select, allowDups ) {
if(typeof select === 'undefined'){ select = true; }

@@ -822,7 +955,23 @@

if(ctx.checkboxes){
var selector = { row: rowIdx, column: colIdx };
var selector = [{ row: rowIdx, column: colIdx }];
ctx.checkboxes.updateData('cell', selector, (select) ? true : false, allowDups);
ctx.checkboxes.updateCheckbox('cell', selector, (select) ? true : false);
ctx.checkboxes.updateData('cell', selector, (select) ? true : false);
ctx.checkboxes.updateSelect('cell', selector, (select) ? true : false);
ctx.checkboxes.updateSelectAll();
// If row selection is enabled
if(ctx.aoColumns[colIdx].checkboxes.selectRow){
ctx.checkboxes.updateSelect('row', rowIdx, select);
}
// If FixedColumns extension is enabled
if(ctx._oFixedColumns){
// Use timeout to let FixedColumns construct the header
// before we update the "Select all" checkbox
setTimeout(function(){ ctx.checkboxes.updateSelectAll(colIdx); }, 0);
} else {
ctx.checkboxes.updateSelectAll(colIdx);
}
ctx.checkboxes.updateFixedColumn(colIdx);
}

@@ -832,14 +981,35 @@ }, 1 );

Api.registerPlural( 'columns().checkboxes.deselect()', 'column().checkboxes.deselect()', function () {
return this.checkboxes.select(false);
Api.registerPlural( 'columns().checkboxes.deselect()', 'column().checkboxes.deselect()', function ( allowDups ) {
return this.checkboxes.select(false, allowDups);
} );
Api.registerPlural( 'cells().checkboxes.deselect()', 'cell().checkboxes.deselect()', function () {
return this.checkboxes.select(false);
Api.registerPlural( 'cells().checkboxes.deselect()', 'cell().checkboxes.deselect()', function ( allowDups ) {
return this.checkboxes.select(false, allowDups);
} );
Api.registerPlural( 'columns().checkboxes.deselectAll()', 'column().checkboxes.deselectAll()', function () {
return this.iterator( 'column', function (ctx, colIdx){
// If Checkboxes extension is enabled for this column
if(ctx.aoColumns[colIdx].checkboxes){
ctx.checkboxes.s.data[colIdx] = {};
this.column(colIdx).checkboxes.select(false, false);
}
}, 1 );
} );
Api.registerPlural( 'columns().checkboxes.selected()', 'column().checkboxes.selected()', function () {
return this.iterator( 'column', function (ctx, colIdx){
if(ctx.aoColumns[colIdx].checkboxes){
return ctx.checkboxes.s.data[colIdx];
var data = [];
$.each(ctx.checkboxes.s.data[colIdx], function(cellData, countRows){
for(var i = 0; i < countRows; i++){
data.push(cellData);
}
});
return data;
} else {
return [];
}

@@ -856,3 +1026,3 @@ }, 1 );

*/
Checkboxes.version = '1.1.0';
Checkboxes.version = '1.2.0';

@@ -867,3 +1037,3 @@

// events so we can automatically initialise
$(document).on( 'preInit.dt.dtr', function (e, settings, json) {
$(document).on( 'preInit.dt.dtCheckboxes', function (e, settings, json) {
if ( e.namespace !== 'dt' ) {

@@ -870,0 +1040,0 @@ return;

@@ -1,5 +0,5 @@

/*! Checkboxes 1.1.3
/*! Checkboxes 1.2.0
* Copyright (c) Gyrocode (www.gyrocode.com)
* License: MIT License
*/
(function(factory){if(typeof define==="function"&&define.amd){define(["jquery","datatables.net"],function($){return factory($,window,document);});}else{if(typeof exports==="object"){module.exports=function(root,$){if(!root){root=window;}if(!$||!$.fn.dataTable){$=require("datatables.net")(root,$).$;}return factory($,root,root.document);};}else{factory(jQuery,window,document);}}}(function($,window,document,undefined){var DataTable=$.fn.dataTable;var Checkboxes=function(settings){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:{},ignoreSelect:false};if(this.s.dt.settings()[0].checkboxes){return;}settings.checkboxes=this;this._constructor();};Checkboxes.prototype={_constructor:function(){var self=this;var dt=self.s.dt;var ctx=dt.settings()[0];var hasCheckboxes=false;var hasCheckboxesSelectRow=false;var state=dt.state.loaded();for(var i=0;i<ctx.aoColumns.length;i++){if(ctx.aoColumns[i].checkboxes){hasCheckboxes=true;if(!$.isPlainObject(ctx.aoColumns[i].checkboxes)){ctx.aoColumns[i].checkboxes={};}ctx.aoColumns[i].checkboxes=$.extend({},Checkboxes.defaults,ctx.aoColumns[i].checkboxes);DataTable.ext.internal._fnApplyColumnDefs(ctx,[{"targets":i,"searchable":false,"orderable":false,"width":"1%","className":"dt-body-center","render":function(data,type,full,meta){if(type==="display"){data='<input type="checkbox" class="dt-checkboxes">';}return data;}}],{},function(iCol,oDef){DataTable.ext.internal._fnColumnOptions(ctx,iCol,oDef);});$(dt.column(i).header()).removeClass("sorting");$(dt.column(i).header()).off(".dt");var cells=dt.cells("tr",i);cells.invalidate("data");$(cells.nodes()).addClass("dt-body-center");self.s.data[i]=[];if(state&&state.checkboxes&&state.checkboxes.hasOwnProperty(i)){self.s.data[i]=state.checkboxes[i];}self.s.columns.push(i);if(ctx.aoColumns[i].checkboxes.selectRow){hasCheckboxesSelectRow=true;$(dt.table().node()).addClass("dt-checkboxes-select");}if(ctx.aoColumns[i].checkboxes.selectAll){$(dt.column(i).header()).html('<input type="checkbox">').addClass("dt-checkboxes-select-all").attr("data-col",i);}}}if(hasCheckboxes){var $table=$(dt.table().node());var $tableBody=$(dt.table().body());var $tableContainer=$(dt.table().container());$tableBody.on("click","input.dt-checkboxes",function(e){self.onClick(e,this);});if(hasCheckboxesSelectRow){if(DataTable.select){$table.on("select.dt deselect.dt",function(e,api,type,indexes){self.onSelect(e,type,indexes);});dt.select.info(false);}else{$tableContainer.on("click","tbody td",function(){var $row=$(this).closest("tr");var e={type:($row.hasClass("selected")?"deselect":"select")};self.onSelect(e,"row",[dt.row($row).index()]);$row.toggleClass("selected");$table.trigger(e.type);});}$table.on("draw.dt select.dt deselect.dt",function(){self.showInfoSelected();});}$table.on("draw.dt",function(e,ctx){self.onDraw(e,ctx);});$tableContainer.on("click",'thead th.dt-checkboxes-select-all input[type="checkbox"]',function(e){self.onClickSelectAll(e,this);});$tableContainer.on("click","thead th.dt-checkboxes-select-all",function(e){$('input[type="checkbox"]',this).trigger("click");});$(document).on("click",'.fixedHeader-floating thead th.dt-checkboxes-select-all input[type="checkbox"]',function(e){if(ctx._fixedHeader){if(ctx._fixedHeader.dom["header"].floating){self.onClickSelectAll(e,this);}}});$(document).on("click",".fixedHeader-floating thead th.dt-checkboxes-select-all",function(e){if(ctx._fixedHeader){if(ctx._fixedHeader.dom["header"].floating){$('input[type="checkbox"]',this).trigger("click");}}});if(ctx.oFeatures.bStateSave){dt.on("stateSaveParams.checkboxes",function(e,settings,data){data.checkboxes=self.s.data;});}}},updateData:function(type,selector,isSelected){var self=this;var dt=self.s.dt;var ctx=dt.settings()[0];var nodes=[];if(type==="row"){dt.rows(selector).every(function(rowIdx){for(var colIdx=0;colIdx<ctx.aoColumns.length;colIdx++){if(ctx.aoColumns[colIdx].checkboxes&&ctx.aoColumns[colIdx].checkboxes.selectRow){nodes.push(dt.cell(rowIdx,colIdx).node());}}});}else{if(type==="cell"){nodes=dt.cells(selector).nodes();$.each(nodes,function(){var cellIdx=self.getCellIndex(this);var colIdx=cellIdx.column;var rowIdx=cellIdx.row;if(ctx.aoColumns[colIdx].checkboxes&&ctx.aoColumns[colIdx].checkboxes.selectRow){var columns=$.grep(self.s.columns,function(value){return value!=colIdx;});$.merge(nodes,dt.cells(rowIdx,columns).nodes());}});}}if(nodes.length){dt.cells(nodes).every(function(){var cellCol=this.index().column;if(ctx.aoColumns[cellCol].checkboxes){var cellData=this.data();var index=$.inArray(cellData,ctx.checkboxes.s.data[cellCol]);if(isSelected&&index===-1){ctx.checkboxes.s.data[cellCol].push(cellData);}else{if(!isSelected&&index!==-1){ctx.checkboxes.s.data[cellCol].splice(index,1);}}}});}dt.state.save();},updateSelect:function(type,selector,isSelected){var self=this;var dt=self.s.dt;var ctx=dt.settings()[0];var nodes=[];if(type==="row"){nodes=dt.rows(selector).nodes();}else{if(type==="cell"){var cellNodes=dt.cells(selector).nodes();$.each(cellNodes,function(){var cellIdx=self.getCellIndex(this);var colIdx=cellIdx.column;var rowIdx=cellIdx.row;if(ctx.aoColumns[colIdx].checkboxes&&ctx.aoColumns[colIdx].checkboxes.selectRow){nodes.push(dt.$(this).closest("tr").get(0));}});}}if(nodes.length){if(DataTable.select){self.s.ignoreSelect=true;if(isSelected){dt.rows(nodes).select();}else{dt.rows(nodes).deselect();}self.s.ignoreSelect=false;}else{if(isSelected){dt.$(nodes).addClass("selected");}else{dt.$(nodes).removeClass("selected");}}}},updateCheckbox:function(type,selector,isSelected){var self=this;var dt=self.s.dt;var ctx=dt.settings()[0];var nodes=[];if(type==="row"){dt.rows(selector).every(function(rowIdx){for(var colIdx=0;colIdx<ctx.aoColumns.length;colIdx++){if(ctx.aoColumns[colIdx].checkboxes&&ctx.aoColumns[colIdx].checkboxes.selectRow){nodes.push(dt.cell(rowIdx,colIdx).node());}}});}else{if(type==="cell"){nodes=dt.cells(selector).nodes();$.each(nodes,function(){var cellIdx=self.getCellIndex(this);var colIdx=cellIdx.column;var rowIdx=cellIdx.row;if(ctx.aoColumns[colIdx].checkboxes&&ctx.aoColumns[colIdx].checkboxes.selectRow){var columns=$.grep(self.s.columns,function(value){return(value!=colIdx)&&ctx.aoColumns[value].checkboxes.selectRow;});if(columns.length){$.merge(nodes,dt.cells(rowIdx,columns).nodes());}}});}}if(nodes.length){dt.$(nodes).find("input.dt-checkboxes").prop("checked",isSelected);}},onClick:function(e,ctrl){var self=this;var dt=self.s.dt;var ctx=dt.settings()[0];var $cell=$(ctrl).closest("td");var cellIdx=self.getCellIndex($cell);if(ctx.aoColumns[cellIdx.column].checkboxes){self.updateCheckbox("cell",cellIdx,ctrl.checked);self.updateData("cell",cellIdx,ctrl.checked);self.updateSelect("cell",cellIdx,ctrl.checked);self.updateSelectAll();e.stopPropagation();}},onSelect:function(e,type,indexes){var self=this;var dt=self.s.dt;var ctx=dt.settings()[0];if(self.s.ignoreSelect){return;}if(type==="row"){self.updateCheckbox("row",indexes,(e.type==="select")?true:false);self.updateData("row",indexes,(e.type==="select")?true:false);self.updateSelectAll();}},onClickSelectAll:function(e,ctrl){var self=this;var dt=self.s.dt;var ctx=dt.settings()[0];var col=null;var $th=$(ctrl).closest("th");if($th.parents(".DTFC_Cloned").length){var cellIdx=dt.fixedColumns().cellIndex($th);col=cellIdx.column;}else{col=dt.column($th).index();}var cells=dt.cells("tr",col,{page:((ctx.aoColumns[col].checkboxes&&ctx.aoColumns[col].checkboxes.selectAllPages)?"all":"current"),search:"applied"});self.updateData("cell",cells.nodes(),ctrl.checked);self.updateCheckbox("cell",cells.nodes(),ctrl.checked);if(ctx.aoColumns[col].checkboxes.selectRow){var rows=dt.rows({page:((ctx.aoColumns[col].checkboxes&&ctx.aoColumns[col].checkboxes.selectAllPages)?"all":"current"),search:"applied"});self.updateSelect("row",rows.nodes(),ctrl.checked);}self.updateSelectAll();if($th.parents(".DTFC_Cloned").length){dt.fixedColumns().update();}e.stopPropagation();},onDraw:function(e,ctx){var self=this;var dt=self.s.dt;ctx=dt.settings()[0];var rows_seen={};dt.cells("tr",self.s.columns,{page:"current",search:"applied"}).every(function(){var cellColIdx=this.index().column;var cellRowIdx=this.index().row;var cellData=this.data();var index=$.inArray(cellData,ctx.checkboxes.s.data[cellColIdx]);if(index!==-1){if(!rows_seen.hasOwnProperty(cellRowIdx)){self.updateCheckbox("cell",this.node(),true);self.updateSelect("cell",this.node(),true);if(ctx.aoColumns[cellColIdx].checkboxes&&ctx.aoColumns[cellColIdx].checkboxes.selectRow){rows_seen[cellRowIdx]=true;}}}});self.updateSelectAll();},updateSelectAll:function(){var self=this;var dt=self.s.dt;var ctx=dt.settings()[0];for(var colIdx=0;colIdx<ctx.aoColumns.length;colIdx++){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 $checkboxes=dt.$(cells.nodes()).find(".dt-checkboxes");var $checkboxesChecked=dt.$(cells.nodes()).find(".dt-checkboxes:checked");var $checkboxesSelectAll=$('.dt-checkboxes-select-all[data-col="'+colIdx+'"] input[type="checkbox"]',$tableContainer);if(ctx._fixedHeader){if(ctx._fixedHeader.dom["header"].floating){$checkboxesSelectAll=$('.fixedHeader-floating .dt-checkboxes-select-all[data-col="'+colIdx+'"] input[type="checkbox"]');}}if($checkboxesChecked.length===0){$checkboxesSelectAll.prop({"checked":false,"indeterminate":false});}else{if($checkboxesChecked.length===$checkboxes.length){$checkboxesSelectAll.prop({"checked":true,"indeterminate":false});}else{$checkboxesSelectAll.prop({"checked":true,"indeterminate":true});}}}}},showInfoSelected:function(){var self=this;var dt=self.s.dt;var ctx=dt.settings()[0];if(!ctx.aanFeatures.i){return;}var $output=$('<span class="select-info"/>');var add=function(name,num){$output.append($('<span class="select-item"/>').append(dt.i18n("select."+name+"s",{_:"%d "+name+"s selected",0:"",1:"1 "+name+" selected"},num)));};var colSelectRowIdx=-1;for(var colIdx=0;colIdx<ctx.aoColumns.length;colIdx++){if(ctx.aoColumns[colIdx].checkboxes&&ctx.aoColumns[colIdx].checkboxes.selectRow){colSelectRowIdx=colIdx;break;}}if(colSelectRowIdx!==-1){add("row",ctx.checkboxes.s.data[colSelectRowIdx].length);$.each(ctx.aanFeatures.i,function(i,el){var $el=$(el);var $existing=$el.children("span.select-info");if($existing.length){$existing.remove();}if($output.text()!==""){$el.append($output);}});}},getCellIndex:function(cell){var self=this;var dt=self.s.dt;var ctx=dt.settings()[0];if(DataTable.FixedColumns){return dt.fixedColumns().cellIndex(cell);}else{return dt.cell(cell).index();}}};Checkboxes.defaults={selectRow:false,selectAll:true,selectAllPages:true};var Api=$.fn.dataTable.Api;Api.register("checkboxes()",function(){return this;});Api.registerPlural("columns().checkboxes.select()","column().checkboxes.select()",function(select){if(typeof select==="undefined"){select=true;}return this.iterator("column",function(ctx,colIdx){if(ctx.checkboxes){var selector=this.cells("tr",colIdx).nodes();ctx.checkboxes.updateCheckbox("cell",selector,(select)?true:false);ctx.checkboxes.updateData("cell",selector,(select)?true:false);ctx.checkboxes.updateSelect("cell",selector,(select)?true:false);ctx.checkboxes.updateSelectAll();}},1);});Api.registerPlural("cells().checkboxes.select()","cell().checkboxes.select()",function(select){if(typeof select==="undefined"){select=true;}return this.iterator("cell",function(ctx,rowIdx,colIdx){if(ctx.checkboxes){var selector={row:rowIdx,column:colIdx};ctx.checkboxes.updateCheckbox("cell",selector,(select)?true:false);ctx.checkboxes.updateData("cell",selector,(select)?true:false);ctx.checkboxes.updateSelect("cell",selector,(select)?true:false);ctx.checkboxes.updateSelectAll();}},1);});Api.registerPlural("columns().checkboxes.deselect()","column().checkboxes.deselect()",function(){return this.checkboxes.select(false);});Api.registerPlural("cells().checkboxes.deselect()","cell().checkboxes.deselect()",function(){return this.checkboxes.select(false);});Api.registerPlural("columns().checkboxes.selected()","column().checkboxes.selected()",function(){return this.iterator("column",function(ctx,colIdx){if(ctx.aoColumns[colIdx].checkboxes){return ctx.checkboxes.s.data[colIdx];}},1);});Checkboxes.version="1.1.0";$.fn.DataTable.Checkboxes=Checkboxes;$.fn.dataTable.Checkboxes=Checkboxes;$(document).on("preInit.dt.dtr",function(e,settings,json){if(e.namespace!=="dt"){return;}new Checkboxes(settings);});return Checkboxes;}));
(function(factory){if(typeof define==="function"&&define.amd){define(["jquery","datatables.net"],function($){return factory($,window,document);});}else{if(typeof exports==="object"){module.exports=function(root,$){if(!root){root=window;}if(!$||!$.fn.dataTable){$=require("datatables.net")(root,$).$;}return factory($,root,root.document);};}else{factory(jQuery,window,document);}}}(function($,window,document,undefined){var DataTable=$.fn.dataTable;var Checkboxes=function(settings){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:{},ignoreSelect:false};this.s.ctx=this.s.dt.settings()[0];if(this.s.ctx.checkboxes){return;}settings.checkboxes=this;this._constructor();};Checkboxes.prototype={_constructor:function(){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;var hasCheckboxes=false;var hasCheckboxesSelectRow=false;var state=dt.state.loaded();for(var i=0;i<ctx.aoColumns.length;i++){if(ctx.aoColumns[i].checkboxes){var $colHeader=$(dt.column(i).header());hasCheckboxes=true;if(!$.isPlainObject(ctx.aoColumns[i].checkboxes)){ctx.aoColumns[i].checkboxes={};}ctx.aoColumns[i].checkboxes=$.extend({},Checkboxes.defaults,ctx.aoColumns[i].checkboxes);var colOptions={"searchable":false,"orderable":false};if(ctx.aoColumns[i].sClass===""){colOptions["className"]="dt-body-center";}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">';};}DataTable.ext.internal._fnColumnOptions(ctx,i,colOptions);$colHeader.removeClass("sorting");$colHeader.off(".dt");self.s.data[i]={};if(state&&state.checkboxes&&state.checkboxes.hasOwnProperty(i)){self.s.data[i]=state.checkboxes[i];}self.s.columns.push(i);if(ctx.aoColumns[i].checkboxes.selectRow){if(ctx._select){hasCheckboxesSelectRow=true;}else{ctx.aoColumns[i].checkboxes.selectRow=false;}}if(ctx.aoColumns[i].checkboxes.selectAll){$colHeader.data("html",$colHeader.html());$colHeader.html('<input type="checkbox">').addClass("dt-checkboxes-select-all").attr("data-col",i);}}}if(hasCheckboxes){var $table=$(dt.table().node());var $tableBody=$(dt.table().body());var $tableContainer=$(dt.table().container());if(hasCheckboxesSelectRow){$table.addClass("dt-checkboxes-select");$table.on("select.dt.dtCheckboxes deselect.dt.dtCheckboxes",function(e,api,type,indexes){self.onSelect(e,type,indexes);});dt.select.info(false);$table.on("draw.dt.dtCheckboxes select.dt.dtCheckboxes deselect.dt.dtCheckboxes",function(){self.showInfoSelected();});}$table.on("xhr.dt",function(e,settings,json,xhr){var state=dt.state.loaded();$.each(self.s.columns,function(index,colIdx){self.s.data[colIdx]={};if(state&&state.checkboxes&&state.checkboxes.hasOwnProperty(colIdx)){self.s.data[colIdx]=state.checkboxes[colIdx];}});if(ctx.oFeatures.bStateSave){if(!ctx.oFeatures.bServerSide){$table.one("draw.dt.dtCheckboxes",function(e){self.updateState();});}}});$table.on("draw.dt.dtCheckboxes",function(e){self.onDraw(e);});$tableBody.on("click.dtCheckboxes","input.dt-checkboxes",function(e){self.onClick(e,this);});$tableContainer.on("click.dtCheckboxes",'thead th.dt-checkboxes-select-all input[type="checkbox"]',function(e){self.onClickSelectAll(e,this);});$tableContainer.on("click.dtCheckboxes","thead th.dt-checkboxes-select-all",function(e){$('input[type="checkbox"]',this).trigger("click");});$(document).on("click.dtCheckboxes",'.fixedHeader-floating thead th.dt-checkboxes-select-all input[type="checkbox"]',function(e){if(ctx._fixedHeader){if(ctx._fixedHeader.dom["header"].floating){self.onClickSelectAll(e,this);}}});$(document).on("click.dtCheckboxes",".fixedHeader-floating thead th.dt-checkboxes-select-all",function(e){if(ctx._fixedHeader){if(ctx._fixedHeader.dom["header"].floating){$('input[type="checkbox"]',this).trigger("click");}}});$table.on("init.dt.dtCheckboxes",function(){if(ctx.oFeatures.bStateSave){if(!ctx.oFeatures.bServerSide){self.updateState();}$table.on("stateSaveParams.dt.dtCheckboxes",function(e,settings,data){data.checkboxes=self.s.data;});}});$table.one("destroy.dt.dtCheckboxes",function(e,settings){$(document).off("click.dtCheckboxes");$tableContainer.on(".dtCheckboxes");$tableBody.off(".dtCheckboxes");$table.off(".dtCheckboxes");self.s.data={};$(".dt-checkboxes-select-all",$table).each(function(index,el){$(el).html($(el).data("html")).removeClass("dt-checkboxes-select-all");});});}},updateData:function(type,selector,isSelected,allowDups){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;if(typeof allowDups==="undefined"){allowDups=false;}var cellSelector;if(type==="cell"){cellSelector=selector;}else{if(type==="row"){cellSelector=[];dt.rows(selector).every(function(rowIdx){var colIdx=self.getSelectRowColIndex();if(colIdx!==null){cellSelector.push({row:rowIdx,column:colIdx});}});}}dt.cells(cellSelector).every(function(cellRow,cellCol){if(ctx.aoColumns[cellCol].checkboxes){var cellData=this.data();var hasData=ctx.checkboxes.s.data[cellCol].hasOwnProperty(cellData);if(isSelected){if(hasData&&allowDups){ctx.checkboxes.s.data[cellCol][cellData]++;}else{ctx.checkboxes.s.data[cellCol][cellData]=1;}}else{if(!isSelected&&hasData){if(ctx.checkboxes.s.data[cellCol][cellData]===1||!allowDups){delete ctx.checkboxes.s.data[cellCol][cellData];}else{ctx.checkboxes.s.data[cellCol][cellData]--;}}}}});if(ctx.oFeatures.bStateSave){dt.state.save();}},updateSelect:function(type,selector,isSelected){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;if(ctx._select){self.s.ignoreSelect=true;if(isSelected){dt.rows(selector).select();}else{dt.rows(selector).deselect();}self.s.ignoreSelect=false;}},updateCheckbox:function(type,selector,isSelected){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;var cellSelector=[];if(type==="row"){dt.rows(selector).every(function(rowIdx){var colIdx=self.getSelectRowColIndex();if(colIdx!==null){cellSelector.push({row:rowIdx,column:colIdx});}});}else{if(type==="cell"){cellSelector=selector;}}var nodes=dt.cells(cellSelector).nodes();if(nodes.length){$("input.dt-checkboxes",nodes).prop("checked",isSelected);var colIdx=cellSelector[0].column;if($.isFunction(ctx.aoColumns[colIdx].checkboxes.selectCallback)){ctx.aoColumns[colIdx].checkboxes.selectCallback(nodes,isSelected);}}},updateState:function(){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;self.updateCheckboxes({page:"all",search:"none"});$.each(self.s.columns,function(index,colIdx){self.updateSelectAll(colIdx);});},updateCheckboxes:function(opts){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;var dataSeen={};dt.cells("tr",self.s.columns,opts).every(function(cellRow,cellCol){var cellData=this.data();if(ctx.checkboxes.s.data[cellCol].hasOwnProperty(cellData)){if(dataSeen.hasOwnProperty(cellData)){dataSeen[cellData]++;}else{dataSeen[cellData]=1;}if(dataSeen[cellData]<=ctx.checkboxes.s.data[cellCol][cellData]){self.updateCheckbox("cell",[{row:cellRow,column:cellCol}],true);if(ctx.aoColumns[cellCol].checkboxes.selectRow){self.updateSelect("row",cellRow,true);}}}});},onClick:function(e,ctrl){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;var cellSelector;var cellIdx=this.getCellIndex($(ctrl).closest("td"));var colIdx=cellIdx.column;var cell=dt.cell(cellIdx);if(!ctx.aoColumns[colIdx].checkboxes.selectRow){cell.checkboxes.select(ctrl.checked,true);e.stopPropagation();}else{setTimeout(function(){var cellData=cell.data();var hasData=self.s.data[colIdx].hasOwnProperty(cellData);if(hasData!==ctrl.checked){self.updateCheckbox("cell",[cellIdx],hasData);self.updateSelectAll(colIdx);}},0);}},onSelect:function(e,type,indexes){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;if(self.s.ignoreSelect){return;}if(type==="row"){var allowDup=true;if((ctx._select.style==="os"||ctx._select.style==="multi+shift")&&indexes.length>1){allowDup=false;}self.updateData("row",indexes,(e.type==="select")?true:false,allowDup);self.updateCheckbox("row",indexes,(e.type==="select")?true:false);var colIdx=self.getSelectRowColIndex();if(colIdx!==null){self.updateSelectAll(colIdx);}}},onClickSelectAll:function(e,ctrl){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;var cellIdx=this.getCellIndex($(ctrl).closest("th"));var colIdx=cellIdx.column;dt.column(colIdx,{page:((ctx.aoColumns[colIdx].checkboxes&&ctx.aoColumns[colIdx].checkboxes.selectAllPages)?"all":"current"),search:"applied"}).checkboxes.select(ctrl.checked,true);e.stopPropagation();},onDraw:function(e){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;if(ctx.oFeatures.bServerSide){self.updateCheckboxes({page:"current",search:"none"});}$.each(self.s.columns,function(index,colIdx){self.updateSelectAll(colIdx);});},updateSelectAll:function(colIdx){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;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 $checkboxes=$(".dt-checkboxes",cells.nodes());var $checkboxesChecked=$checkboxes.filter(":checked");var $checkboxesSelectAll=$('.dt-checkboxes-select-all[data-col="'+colIdx+'"] input[type="checkbox"]',$tableContainer);if(ctx._fixedHeader){if(ctx._fixedHeader.dom["header"].floating){$checkboxesSelectAll=$('.fixedHeader-floating .dt-checkboxes-select-all[data-col="'+colIdx+'"] input[type="checkbox"]');}}var isSelected;var isIndeterminate;if($checkboxesChecked.length===0){isSelected=false;isIndeterminate=false;}else{if($checkboxesChecked.length===$checkboxes.length){isSelected=true;isIndeterminate=false;}else{isSelected=true;isIndeterminate=true;}}$checkboxesSelectAll.prop({"checked":isSelected,"indeterminate":isIndeterminate});if($.isFunction(ctx.aoColumns[colIdx].checkboxes.selectAllCallback)){ctx.aoColumns[colIdx].checkboxes.selectAllCallback($checkboxesSelectAll.closest("th").get(0),isSelected,isIndeterminate);}}},showInfoSelected:function(){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;if(!ctx.aanFeatures.i){return;}var $output=$('<span class="select-info"/>');var add=function(name,num){$output.append($('<span class="select-item"/>').append(dt.i18n("select."+name+"s",{_:"%d "+name+"s selected",0:"",1:"1 "+name+" selected"},num)));};var colIdx=self.getSelectRowColIndex();if(colIdx!==null){var countRows=0;for(var cellData in ctx.checkboxes.s.data[colIdx]){if(ctx.checkboxes.s.data[colIdx].hasOwnProperty(cellData)){countRows+=ctx.checkboxes.s.data[colIdx][cellData];}}add("row",countRows);$.each(ctx.aanFeatures.i,function(i,el){var $el=$(el);var $existing=$el.children("span.select-info");if($existing.length){$existing.remove();}if($output.text()!==""){$el.append($output);}});}},getCellIndex:function(cell){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;if(ctx._oFixedColumns){return dt.fixedColumns().cellIndex(cell);}else{return dt.cell(cell).index();}},getSelectRowColIndex:function(){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;var colIdx=null;for(var i=0;i<ctx.aoColumns.length;i++){if(ctx.aoColumns[i].checkboxes&&ctx.aoColumns[i].checkboxes.selectRow){colIdx=i;break;}}return colIdx;},updateFixedColumn:function(colIdx){var self=this;var dt=self.s.dt;var ctx=self.s.ctx;if(ctx._oFixedColumns){var leftCols=ctx._oFixedColumns.s.iLeftColumns;var rightCols=ctx.aoColumns.length-ctx._oFixedColumns.s.iRightColumns-1;if(colIdx<leftCols||colIdx>rightCols){dt.fixedColumns().update();}}}};Checkboxes.defaults={selectRow:false,selectAll:true,selectAllPages:true,selectCallback:null,selectAllCallback:null};var Api=$.fn.dataTable.Api;Api.register("checkboxes()",function(){return this;});Api.registerPlural("columns().checkboxes.select()","column().checkboxes.select()",function(select,allowDups){if(typeof select==="undefined"){select=true;}return this.iterator("column-rows",function(ctx,colIdx,i,j,rowsIdx){if(ctx.checkboxes){var selector=[];$.each(rowsIdx,function(index,rowIdx){selector.push({row:rowIdx,column:colIdx});});ctx.checkboxes.updateData("cell",selector,(select)?true:false,allowDups);ctx.checkboxes.updateCheckbox("cell",selector,(select)?true:false);if(ctx.aoColumns[colIdx].checkboxes.selectRow){ctx.checkboxes.updateSelect("row",rowsIdx,select);}if(ctx._oFixedColumns){setTimeout(function(){ctx.checkboxes.updateSelectAll(colIdx);},0);}else{ctx.checkboxes.updateSelectAll(colIdx);}ctx.checkboxes.updateFixedColumn(colIdx);}},1);});Api.registerPlural("cells().checkboxes.select()","cell().checkboxes.select()",function(select,allowDups){if(typeof select==="undefined"){select=true;}return this.iterator("cell",function(ctx,rowIdx,colIdx){if(ctx.checkboxes){var selector=[{row:rowIdx,column:colIdx}];ctx.checkboxes.updateData("cell",selector,(select)?true:false,allowDups);ctx.checkboxes.updateCheckbox("cell",selector,(select)?true:false);if(ctx.aoColumns[colIdx].checkboxes.selectRow){ctx.checkboxes.updateSelect("row",rowIdx,select);}if(ctx._oFixedColumns){setTimeout(function(){ctx.checkboxes.updateSelectAll(colIdx);},0);}else{ctx.checkboxes.updateSelectAll(colIdx);}ctx.checkboxes.updateFixedColumn(colIdx);}},1);});Api.registerPlural("columns().checkboxes.deselect()","column().checkboxes.deselect()",function(allowDups){return this.checkboxes.select(false,allowDups);});Api.registerPlural("cells().checkboxes.deselect()","cell().checkboxes.deselect()",function(allowDups){return this.checkboxes.select(false,allowDups);});Api.registerPlural("columns().checkboxes.deselectAll()","column().checkboxes.deselectAll()",function(){return this.iterator("column",function(ctx,colIdx){if(ctx.aoColumns[colIdx].checkboxes){ctx.checkboxes.s.data[colIdx]={};this.column(colIdx).checkboxes.select(false,false);}},1);});Api.registerPlural("columns().checkboxes.selected()","column().checkboxes.selected()",function(){return this.iterator("column",function(ctx,colIdx){if(ctx.aoColumns[colIdx].checkboxes){var data=[];$.each(ctx.checkboxes.s.data[colIdx],function(cellData,countRows){for(var i=0;i<countRows;i++){data.push(cellData);}});return data;}else{return[];}},1);});Checkboxes.version="1.2.0";$.fn.DataTable.Checkboxes=Checkboxes;$.fn.dataTable.Checkboxes=Checkboxes;$(document).on("preInit.dt.dtCheckboxes",function(e,settings,json){if(e.namespace!=="dt"){return;}new Checkboxes(settings);});return Checkboxes;}));
{
"name": "jquery-datatables-checkboxes",
"version": "1.1.3",
"version": "1.2.0",
"description": "Checkboxes is an extension for the jQuery DataTables library that provides universal solution for working with checkboxes in a table.",

@@ -22,3 +22,3 @@ "main": "./js/dataTables.checkboxes.js",

"jquery": ">=1.7",
"datatables.net": ">=1.10"
"datatables.net": ">=1.10.8"
},

@@ -25,0 +25,0 @@ "repository": {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc