jQuery DataTables – Row selection using checkboxes and Select extension
Example for article at
http://www.gyrocode.com/articles/jquery-datatables-row-selection-using-checkboxes-and-select-extension/
HTML
<link rel="stylesheet" href="https://cdn.datatables.net/s/dt/dt-1.10.10,se-1.1.0/datatables.min.css">
<script src="https://cdn.datatables.net/s/dt/dt-1.10.10,se-1.1.0/datatables.min.js"></script>
<h3><a href="http://www.gyrocode.com/articles/jquery-datatables-row-selection-using-checkboxes-and-select-extension/">jQuery DataTables – Row selection using checkboxes and Select extension</a></h3>
<a href="http://www.gyrocode.com/articles/jquery-datatables-row-selection-using-checkboxes-and-select-extension/">See full article on Gyrocode.com</a>
<hr><br>
<form id="frm-example" action="/nosuchpage" method="POST">
<table id="example" class="display select" cellspacing="0" width="100%">
<thead>
<tr>
<th>
<input type="checkbox">
</th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Extn</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
<hr>
<p>Press <b>Submit</b> and check console for URL-encoded form data that would be submitted.</p>
<p><button>Submit</button></p>
<b>Data submitted to the server:</b><br>
<pre id="example-console">
</pre>
</form>
CSS
table.dataTable.select tbody tr,
table.dataTable thead th:first-child {
cursor: pointer;
}
JavaScript
//
// Updates "Select all" control in a data table
//
function updateDataTableSelectAllCtrl(table) {
var $table = table.table().container();
var $chkbox_all = $('tbody input[type="checkbox"]', $table);
var $chkbox_checked = $('tbody input[type="checkbox"]:checked', $table);
var chkbox_select_all = $('thead input[type="checkbox"]', $table).get(0);
// If none of the checkboxes are checked
if ($chkbox_checked.length === 0) {
chkbox_select_all.checked = false;
if ('indeterminate' in chkbox_select_all) {
chkbox_select_all.indeterminate = false;
}
// If all of the checkboxes are checked
} else if ($chkbox_checked.length === $chkbox_all.length) {
chkbox_select_all.checked = true;
if ('indeterminate' in chkbox_select_all) {
chkbox_select_all.indeterminate = false;
}
// If some of the checkboxes are checked
} else {
chkbox_select_all.checked = true;
if ('indeterminate' in chkbox_select_all) {
chkbox_select_all.indeterminate = true;
}
}
}
$(document).ready(function() {
// Initialize the table
var table = $('#example').DataTable({
ajax: 'https://api.myjson.com/bins/1us28',
columnDefs: [
{
targets: 0,
orderable: false,
searchable: false,
className: 'dt-body-center',
render: function(data, type, full, meta) {
return '<input type="checkbox">';
}
}
],
select: {
style: 'multi'
},
order: [[1, 'asc']]
});
// Handle row selection event
$('#example').on('select.dt deselect.dt', function(e, api, type, items) {
if (e.type === 'select') {
$('tr.selected input[type="checkbox"]', api.table().container()).prop('checked', true);
} else {
$('tr:not(.selected) input[type="checkbox"]', api.table().container()).prop('checked', false);
}
// Update state of "Select all" control
...