rahul tyagi
rahul tyagi

Reputation: 643

Jquery DataTable keeps going back to first page when changing the checked status of checkboxes in other pages with javascript

I have the following code

    $('input[name="select-all"]').click(function() {
     //channel-data is initialised as DataTable
     $('#channel-table').find('input[name="batch-select"]:checked').each(function () {
        $(this).prop("checked",true);
    });
});

whenever I click the select all button in any page other than 1st The datatable keeps reverting back to the first page. I think it happens because of redraw due to change in checkboxes property.Any work around this?

Upvotes: 2

Views: 2522

Answers (1)

davidkonrad
davidkonrad

Reputation: 85538

Yes, probably - but have not seen any reason for test your hypothesis :) When dealing with dataTables, always go through the API! There is a neat every() method you can use to cycle through all rows and update a checkbox as checked. Here is an example, have tried to reproduce your setup with a "select all" button and checkboxes :

$('input[name="select-all"]').click(function() {
  table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
     var $tr = this.nodes().to$()
     $tr.find('input[name="batch-select"]').prop('checked', true)
   })     
})

demo -> http://jsfiddle.net/u9Lq56v1/

  • does not change page
  • does actually update checkboxes also on hidden pages (that would the code in OP fail as well)

Upvotes: 2

Related Questions