How to add checkboxes with possibility to choose all in datatable jQuery

Viewed 61

I work wit library datatables, and i want to add in table row check box for some actions, i add checkboxes to the table,and one check box in table header for choose all boxes.

Script:

$('#checkAll').click(function () {
    $(':checkbox.checkbox-mark').prop('checked', this.checked);
});

In jsfiddle you can see more.

But problem is, i have pages, and in case if i tap on header checkbox, it's choose only all boxes in curent page, but i want in the all pages, how i can solve this?

1 Answers

like you discussed already you can use service/api to set selectAll values but if you want to it using front end then you can use DataTable().cells().nodes(); to get all the nodes and iterate through them and set the checkbox value to each node.

// update your checkAll logic like this

$('#checkAll').click(function() {
  var allPagesData = $("#test1").DataTable().cells().nodes(); // this will give you all data in datatable.
  $(allPagesData).find('input[type="checkbox"]').prop('checked', this.checked);
});

check out this fiddle

Related