Reputation: 643
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
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/
Upvotes: 2