How can I select all checkboxes from all the pages in a jQuery DataTable

checkboxes will only work on current pagination page in jquery datatables
datatable select all checkbox jsfiddle
get all checked checkboxes jquery datatables
jquery datatable get all rows data
datatables checkbox select all
jquery datatable checkbox click event
select all rows in active page only jquery datatable
how to remember selected checkboxes across paginated pages

I have HTML page which have multiple checkboxes and individually they can be checked. I have button for "Select All" and when I click on this button all the checkboxes should get selected, and when I click again on the same button all the checkboxes should get deselected from all pages.

In my original program there are thousands of records, but at a time 10 records are getting display, but when user click on select it should select all thousands record.

I am using jQuery Datatables plug-in for displaying the data. It provides pagination, searching, sorting etc. so at a time I am displaying only 10 records on my current page. if I click on next or page number which is provided by Bootstrap Datatable another 10 records will be displayed. As mention in the problem I want to select all check-boxes from all the pages.

$(document).ready(function () {
   $('body').on('click', '#selectAll', function () {
      if ($(this).hasClass('allChecked')) {
         $('input[type="checkbox"]', '#example').prop('checked', false);
      } else {
       $('input[type="checkbox"]', '#example').prop('checked', true);
       }
       $(this).toggleClass('allChecked');
     })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>monitoring</title>
        <script src="jquery.js"></script>
         </head>
        <body>
        <table id="example" class="myclass">
        <thead>
        <tr>
         <th>
          <button type="button" id="selectAll" class="main">
          <span class="sub"></span> Select </button></th>
        	<th>Name</th>
        	<th>Company</th>
        	<th>Employee Type</th>
        	<th>Address</th>
        	<th>Country</th>
        </tr>
        </thead>
        <tbody>
        										  
        <tr>
        <td><input type="checkbox"/>
        </td>
        <td>varun</td>
        <td>TCS</td>
        <td>IT</td>
        <td>San Francisco</td>
        <td>US</td>
        </tr>

        <tr>
        <td><input type="checkbox"/>
        </td>
        <td>Rahuk</td>
        <td>TCS</td>
        <td>IT</td>
        <td>San Francisco</td>
        <td>US</td>
        </tr>

        <tr>
        <td><input type="checkbox"/>
        </td>
        <td>johm Doe</td>
        <td>TCS</td>
        <td>IT</td>
        <td>San Francisco</td>
        <td>US</td>
        </tr>

        <tr>
        <td><input type="checkbox"/>
        </td>
        <td>Sam</td>
        <td>TCS</td>
        <td>IT</td>
        <td>San Francisco</td>
        <td>US</td>
        </tr>

        <tr>
        <td><input type="checkbox"/>
        </td>
        <td>Lara</td>
        <td>TCS</td>
        <td>IT</td>
        <td>San Francisco</td>
        <td>US</td>
        </tr>

        <tr>
        <td><input type="checkbox"/>
        </td>
        <td>Jay</td>
        <td>TCS</td>
        <td>IT</td>
        <td>San Francisco</td>
        <td>US</td>
        </tr>

        <tr>
        <td><input type="checkbox"/>
        </td>
        <td>Tom</td>
        <td>TCS</td>
        <td>IT</td>
        <td>San Francisco</td>
        <td>US</td>
        </tr>
        																								
        </tbody>
        </table>
        				
        </body>
        </html>

Try this code instead:

$(document).ready(function () { 
    var oTable = $('#example').dataTable({
        stateSave: true
    });

    var allPages = oTable.fnGetNodes();

    $('body').on('click', '#selectAll', function () {
        if ($(this).hasClass('allChecked')) {
            $('input[type="checkbox"]', allPages).prop('checked', false);
        } else {
            $('input[type="checkbox"]', allPages).prop('checked', true);
        }
        $(this).toggleClass('allChecked');
    })
});

The magic should happen in fnGetNodes():

fnGetNodes(): Get an array of the TR nodes that are used in the table's body

Edit

This alternative solution is mostly for debugging (to see if it works). Hardly optimal code:

$(document).ready(function () { 
    var oTable = $('#example').dataTable({
        stateSave: true
    });

    var allPages = oTable.cells( ).nodes( );

    $('#selectAll').click(function () {
        if ($(this).hasClass('allChecked')) {
            $(allPages).find('input[type="checkbox"]').prop('checked', false);
        } else {
            $(allPages).find('input[type="checkbox"]').prop('checked', true);
        }
        $(this).toggleClass('allChecked');
    })
});    

jQuery DataTables: How to add a checkbox column, will then invert from their previous state, checked to unchecked and vice versa. Normally jQuery datatable will display 10 records at a time. If we select using paging max it will show 100. If user needs to select all records form all pages. Below example I will show you how to select all records from all pages of jQuery datatable and same way if user unchecks select all checkbox it will uncheck checkboxes from all pages.

Use datatable $ instance for selections https://datatables.net/docs/DataTables/1.9.4/#$

$(document).ready(function () { 
    var oTable = $('#example').dataTable({
        stateSave: true
    });

    $("#selectAll").on("change", function(){
        oTable.$("input[type='checkbox']").attr('checked', $(this.checked));  
    });
});

5 Ways To Check or Uncheck Multiple Checkboxes in Web Browsers, How check checkbox is checked or not in jquery Datatable? I have button for "Select All" and when I click on this button all the checkboxes should get selected, and when I click again on the same button all the checkboxes should get deselected from all pages. In my original program there are thousands of records, but at a time 10 records are getting display, but when user click on select it should

Try This,

   if ($(this).hasClass('allChecked')) {
     $('input[type="checkbox"]').prop('checked', 'checked')
   } else {
     $('input[type="checkbox"]').prop('checked', 'false')
   }

how to remember selected checkboxes across paginated pages?, (); var row = table. row("#"+trId); var rowData = row. check link Select all contents from all pages from a DataTable using Jquery DataTables - JSFiddle var table = $('#example').DataTable(); $('#checkall&#039;).click

DEMO

Easiest way is to use following jQuery code:

//EDIT on the second click, you now remove all checked boxes.

$('#selectAll').click(function(e) {
    if($(this).hasClass('checkedAll')) {
      $('input').prop('checked', false);   
      $(this).removeClass('checkedAll');
    } else {
      $('input').prop('checked', true);
      $(this).addClass('checkedAll');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>monitoring</title>
        <script src="jquery.js"></script>
         </head>																			<body>
        <table id="example" class="myclass"/>
        <thead>
        <tr>
         <th>
          <button type="button" id="selectAll" class="main">
          <span class="sub"></span> Select </button></th>
        	<th>Name</th>
        	<th>Company</th>
        	<th>Employee Type</th>
        	<th>Address</th>
        	<th>Country</th>
        </tr>
        </thead>
        <tbody>
        										  
        <tr>
        <td><input type="checkbox"/>
        </td>
        <td>varun</td>
        <td>TCS</td>
        <td>IT</td>
        <td>San Francisco</td>
        <td>US</td>
        </tr>

        <tr>
        <td><input type="checkbox"/>
        </td>
        <td>Rahuk</td>
        <td>TCS</td>
        <td>IT</td>
        <td>San Francisco</td>
        <td>US</td>
        </tr>

        <tr>
        <td><input type="checkbox"/>
        </td>
        <td>johm Doe</td>
        <td>TCS</td>
        <td>IT</td>
        <td>San Francisco</td>
        <td>US</td>
        </tr>

        <tr>
        <td><input type="checkbox"/>
        </td>
        <td>Sam</td>
        <td>TCS</td>
        <td>IT</td>
        <td>San Francisco</td>
        <td>US</td>
        </tr>

        <tr>
        <td><input type="checkbox"/>
        </td>
        <td>Lara</td>
        <td>TCS</td>
        <td>IT</td>
        <td>San Francisco</td>
        <td>US</td>
        </tr>

        <tr>
        <td><input type="checkbox"/>
        </td>
        <td>Jay</td>
        <td>TCS</td>
        <td>IT</td>
        <td>San Francisco</td>
        <td>US</td>
        </tr>

        <tr>
        <td><input type="checkbox"/>
        </td>
        <td>Tom</td>
        <td>TCS</td>
        <td>IT</td>
        <td>San Francisco</td>
        <td>US</td>
        </tr>
        																								
        </tbody>
        </table>
        				
        </body>
        </html>

Check whether checkbox in a jquery datatable row is checked or not , Normally jQuery datatable will display 10 records at a time. If we select using paging max it will show 100. If user needs to select all records  See jQuery DataTables: How to submit all pages form data for more details and demonstration. NOTES. Each checkbox should have a value attribute assigned with unique value. Avoid using id attribute check for multiple elements, this attribute is supposed to be unique.

use dataTables.checkboxes.min.js and use "selectAllPages" : false . It should be work for selection of each page records .

How to select all checkboxes from all pages in jQuery datatable grid?, check link Select all contents from all pages from a DataTable using Jquery DataTables - JSFiddle var table = $('#example').DataTable()  Thanks for your answer. This again check all checkboxes only in page1 of all paginations. I guess the issue is with datatable not with jquery. Because it is not able to find checkboxes in the rest of the pages – Java_User Jul 8 '15 at 6:02

How to select all checkbox across all the pages on the click of a , I am new to DataTables and have a table with a "Select All" checkbox in the when the "Select All" checkbox is checked, but if there are other pages, There are a couple mistakes in the jQuery example I posted, so I have  See Examples page for a full list of additional examples demonstrating how jQuery DataTables Checkboxes extension can be used. Documentation. See Reference page for a list of options and API methods available when working with jQuery DataTables Checkboxes extension. Known Limitations. Column containing checkboxes must have unique data.

DataTables 1.10 Select All checkbox and Hidden rows/pages , We will demonstrate universal solution on how to add a checkbox column to a table, allow multiple row selection and ability to select all rows  Hi all, Is there anyone who have implemented a DataTable with a checkbox column and then when you tick the checkbox on the header it select or deselect all rows in the table?

jQuery DataTables: Row selection using checkboxes and Select , “Select all” control​​ // Handle click on "Select all" control $('#example-select-all'). on('click', function(){ // Get all rows with search applied var rows = table. rows({ 'search': 'applied' }). nodes(); // Check/uncheck checkboxes for all rows in the table $('input[type="checkbox"]', rows). DataTables Checkbox Select all to select your checkboxes. This is a needy feature for playing with large data rows and want to make changes in several rows. With  the below code you can select all the check boxes from the top checkbox.

Comments
  • possible duplicate of Select all check boxes using button
  • The tabletag is not a self-closing tag, please change <table id="example" class="myclass"/> and remove the /
  • You posted same question stackoverflow.com/questions/29841547/… and stackoverflow.com/questions/29823843/…? Use edit links on the same questions
  • Your code should work. Can you share the error you are getting or the issue that you are facing? Why are you posting same question again, plz edit your previous question or tell us the actual issue you are facing.
  • @varun. What do you mean by all pages, isn't your complete table in 1 page? How are you displaying your table in different pages? Please add this info in your question so that ppl here can help you better.
  • sorry for being so dumb but can you help me with this. I am totally new with this. or just give an idea how do I do that
  • @varun I've updated both codes a bit - since the newer version of DataTables apparently use another definition of options.
  • you are genius. finally you did it. the first code finally worked
  • Be aware this won't work if you're using DataTable.select and outputting the x rows selected. The checkboxes will need to be actually triggered before Datatables registers it.
  • This is fine till we are not searching anything. If we use any filter or search, then the select all option got disappeared. Is it possible to have the select all option available after filtering also?
  • This option is a very clean option to use and I used it myself, however it needs to be updated to $(this).is(":checked")