1

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.

1
  • Welcome to StackOverflow. Do you have some examples of code you've tried? Commented Apr 17, 2013 at 18:31

2 Answers 2

4

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); 
     }
 );
Sign up to request clarification or add additional context in comments.

1 Comment

Cdlong, can you explain this code a bit more ? That would be of great help.
0

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>

1 Comment

How about getting all records after clicking to this textbox?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.