DataTables Checkbox Select all
- Article
- Comment (6)
Introduction
DataTables Checkbox Select all to select your checkboxes. This is a needy feature for playing with large data rows and want to make changes in several rows. With the below code you can select all the check boxes from the top checkbox.
HTML Table
The blow HTML table is an example for our testing. our Data’s are from serverside. So we just use <thead>
.
<table class="table table-striped" id="example">
<thead><tr>
<th><span class="hide"> - </span><div class="checkbox mass_select_all_wrap">
<input type="checkbox" id="mass_select_all" data-to-table="tasks"><label></label></div></th>
<th>Name</th>
<th>Status</th>
<th>Options</th>
</tr></thead>
<tbody></tbody>
</table>
And the next step.
Disable Sorting
We need to disable the first column sorting feature. Because this will work when you click on checkbox. So Disable the first column sorting with below code.
$('#example').dataTable( {
"columnDefs": [ {
"targets": [0],
"orderable": false,
} ]
} );
And now here is the code to select all the checkbox when table header checkbox checked.
DataTable Checkbox Select All
$('body').on('change', '#mass_select_all', function() {
var rows, checked;
rows = $('#example').find('tbody tr');
checked = $(this).prop('checked');
$.each(rows, function() {
var checkbox = $($(this).find('td').eq(0)).find('input').prop('checked', checked);
});
});
And The above one helps you to create check all checkboxes and it will work properly. If you have doubt or problem with its implementation, you can knock me through the comments and also you can follow me on social sites to know more about me.
Dear Varadharaj V,
Thank you for your code. I am using your code in the basic DataTable plugin.
How to get number of selected checkbox?
Regards
Nikolay
Thank you for reading it.
$.each(rows, function() {}
Here you can use a var and iterate it. it will be the count for the rows selected.
Tried to implement this in WP for a hobby project and used the fixedColumns extension. The problem i have now is that the select all checkbox in WP doesn’t seem to work anymore. Do you have an idea how i could fix this or point me in the right direction were the select all code is from WP?
The WP table is different DataTable is different. It’s wp_list_table class program. Try to read it with example it will help you to fix the issue.
Hi,
Using this code I can select rows of the first page only.
How can I select rows from all the pages at a time?
There might be way around it. But I am not sure about it