Checkboxes is an extension for the jQuery DataTables library that provides universal solution for working with checkboxes in a table.
Features
- Select/deselect all checkboxes with “select all” control
- Select/deselect rows by clicking on a checkbox with Select extension
- Works with various data sources including Ajax-sourced data with server-side processing
- Supports alternative styling
- Supports Select, FixedHeader, FixedColumns, and RowGroup extensions
- Supports deferred rendering
- Supports localization
- Extensive API for fine-grained control
Demonstration
Example below shows a table powered by jQuery DataTables with Select and Checkbox extensions using client-side processing mode where data is received from the server via Ajax request.
$(document).ready(function (){
var table = $('#example').DataTable({
'ajax': '/lab/jquery-datatables-checkboxes/ids-arrays.txt',
'columnDefs': [
{
'targets': 0,
'checkboxes': {
'selectRow': true
}
}
],
'select': {
'style': 'multi'
},
'order': [[1, 'asc']]
});
// Handle form submission event
$('#frm-example').on('submit', function(e){
var form = this;
var rows_selected = table.column(0).checkboxes.selected();
// Iterate over all selected checkboxes
$.each(rows_selected, function(index, rowId){
// Create a hidden element
$(form).append(
$('<input>')
.attr('type', 'hidden')
.attr('name', 'id[]')
.val(rowId)
);
});
});
});
External resources used in this example:
Installation
Include two extra files in addition to jQuery and jQuery DataTables plugins.
<link type="text/css" href="//gyrocode.github.io/jquery-datatables-checkboxes/1.2.12/css/dataTables.checkboxes.css" rel="stylesheet" />
<script type="text/javascript" src="//gyrocode.github.io/jquery-datatables-checkboxes/1.2.12/js/dataTables.checkboxes.min.js"></script>
Usage
In your jQuery DataTables initialization code add checkboxes
option for the column where you want the checkboxes to appear using either columns or columnDefs options.
If you’re using Select extension, you may want to enable multiple row selection using select.style option.
If checkboxes are located in the first column, you may also want to sort by column other than first which is performed by default with order option.
For example:
var table = $('#example').DataTable({
'columnDefs': [
{
'targets': 0,
'checkboxes': {
'selectRow': true
}
}
],
'select': {
'style': 'multi'
},
'order': [[1, 'asc']]
});
Examples
See Examples page for a full list of additional examples demonstrating how jQuery DataTables Checkboxes extension can be used.
Documentation
See Reference page for a list of options and API methods available when working with jQuery DataTables Checkboxes extension.
Known Limitations
-
Column containing checkboxes must have unique data. Using columns.data option set to
null
for the column containing checkboxes will result in unexpected behavior. - Selecting rows on all pages using columns.checkboxes.selectAllPages option in server-side processing mode is currently not possible. In this mode only rows on the current page are selected, when “select all” control is clicked.
Copyright
Michael Ryvkin,
License
MIT License, opensource.org/licenses/MIT