jQuery DataTables Checkboxes: Filter checkboxes
Example for https://www.gyrocode.com/projects/jquery-datatables-checkboxes/
by gyrocode
HTML
<link rel="stylesheet" href="https://cdn.datatables.net/v/dt/dt-1.10.15/se-1.2.2/datatables.min.css">
<script src="https://cdn.datatables.net/v/dt/dt-1.10.15/se-1.2.2/datatables.min.js"></script>
<link rel="stylesheet" href="https://gyrocode.github.io/jquery-datatables-checkboxes/1.2.9/css/dataTables.checkboxes.css">
<script src="https://gyrocode.github.io/jquery-datatables-checkboxes/1.2.9/js/dataTables.checkboxes.min.js"></script>
<h3><a target="_blank" href="https://www.gyrocode.com/projects/jquery-datatables-checkboxes/">jQuery DataTables Checkboxes:</a> <a target="_blank" href="https://www.gyrocode.com/projects/jquery-datatables-checkboxes/examples/api/filter-checkboxes/">Filter checkboxes</a></h3>
<select id="ctrl-show-selected">
<option value="all" selected>Show all</option>
<option value="selected">Show selected</option>
<option value="not-selected">Show not selected</option>
</select>
<hr>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th></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>Extn</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
<hr><a target="_blank" href="https://www.gyrocode.com/projects/jquery-datatables-checkboxes/examples/api/filter-checkboxes/">See full article on Gyrocode.com</a>
JavaScript
$(document).ready(function (){
var table = $('#example').DataTable({
'ajax': 'https://gyrocode.github.io/files/jquery-datatables/arrays_id.json',
'columnDefs': [
{
'targets': 0,
'checkboxes': {
'selectRow': true,
'selectCallback': function(nodes, selected){
// If "Show all" is not selected
if($('#ctrl-show-selected').val() !== 'all'){
// Redraw table to include/exclude selected row
table.draw(false);
}
}
},
}
],
'select': 'multi',
'order': [[1, 'asc']]
});
// Handle change event for "Show selected records" control
$('#ctrl-show-selected').on('change', function(){
var val = $(this).val();
// If all records should be displayed
if(val === 'all'){
$.fn.dataTable.ext.search.pop();
table.draw();
}
// If selected records should be displayed
if(val === 'selected'){
$.fn.dataTable.ext.search.pop();
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex){
return ($(table.row(dataIndex).node()).hasClass('selected')) ? true : false;
}
);
table.draw();
}
// If selected records should not be displayed
if(val === 'not-selected'){
$.fn.dataTable.ext.search.pop();
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex){
return ($(table.row(dataIndex).node()).hasClass('selected')) ? false : true;
}
);
table.draw();
}
});
});