I am using a jQuery Datatable and i would like to have a column of checkboxes and the header should contain a select all checkbox . I have not found any details about this on the datatable official site.
Can someone help?
Thanks.
I am using a jQuery Datatable and i would like to have a column of checkboxes and the header should contain a select all checkbox . I have not found any details about this on the datatable official site.
Can someone help?
Thanks.
One way would be to use the aoColumns property of datatables and just render your markup for the "sTitle" property as an html string.
http://www.datatables.net/usage/columns
//On datatable init the options would look something like this
"aoColumns": [{ "sTitle": "<input type='checkbox' id='selectAll'></input>"}]
Then you could just wire up a handler to the header checkbox after the datatable is created to check/uncheck all the checkboxes;
So something like:
$("#selectAll").toggle(function () {
$("checkboxSelector", dataTable.fnGetNodes()).attr("checked", true); }
, function () {
$("checkboxSelector", dataTable.fnGetNodes()).attr("checked", false);
}
);
you can have row like below
<tr>
<th><input type="checkbox" id="selectall"/></th>
<th>Cell phone</th>
<th>Rating</th>
and jquery to wire up
<SCRIPT language="javascript">
$(function(){
// add multiple select / deselect functionality
$("#selectall").click(function () {
$('.case').attr('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
$(".case").click(function(){
if($(".case").length == $(".case:checked").length) {
$("#selectall").attr("checked", "checked");
} else {
$("#selectall").removeAttr("checked");
}
});
});
</SCRIPT>