Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery datatable checkall

HTML

<div class="box">
<table id="Datatable">
    <thead>
       <tr>
         <th><input name="checkall" type="checkbox" class="checkall" value="ON" /></th>
         <th>field</th>
         <th>field</th>
         <th>field</th>
         <th>field</th>
         <th>field</th>
      </tr>
   </thead>
   <tbody>
      <tr>
        <th class="checkers"><input type="checkbox" name="selected[]"/></th>
        <td>value</td>                                          
        <td>value</td>  
        <td>value</td>  
        <td>value</td>  
        <td>value</td>  
      </tr>
   </tbody>
</table>
</div>

I'm trying to make the checkall checkbox selects all the checkboxes using this code:

$('.checkall').click(function () {
        var checkall =$(this).parents('.box:eq(0)').find(':checkbox').attr('checked', this.checked);
        $.uniform.update(checkall);
    });

As datatable shows the first 10,20,30 ... etc rows and removes the others from the DOM to do the pagination, this jQuery code only selects the rows in the current page. So is there anyway that I can select all checkboxes?

like image 462
trrrrrrm Avatar asked Jun 21 '11 11:06

trrrrrrm


2 Answers

My solution works too:

$('.checkall').click(function(e) {

          var chk = $(this).prop('checked');

          $('input', oTable.fnGetNodes()).prop('checked',chk);
        });

And if you want check only filtered (if you use dom for filtering in datatables), then you can use this, to check only filtered

$('.checkall').click(function(e) {

          var chk = $(this).prop('checked');

          $('input', oTable.$('tr', {"filter": "applied"} )).prop('checked',chk);
        });
like image 66
carlituxman Avatar answered Dec 01 '22 03:12

carlituxman


i found the solution

$('.checkall', oTable.fnGetNodes()).click(function () {
like image 39
trrrrrrm Avatar answered Dec 01 '22 02:12

trrrrrrm