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

前端 未结 6 2030
既然无缘
既然无缘 2020-11-29 05:11

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 check

相关标签:
6条回答
  • 2020-11-29 05:55

    Try This,

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

    0 讨论(0)
  • 2020-11-29 06:02

    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));  
        });
    });
    
    0 讨论(0)
  • 2020-11-29 06:02
    var oTable = $('#myTable').DataTable({
        lengthMenu: [[10, 25, 50, -1], [10, 25, 50, 'All']]
        // stateSave: true
    });
    $('#selectAll').on('change', function() {
        oTable.$('.student_id:checkbox').prop('checked', $(this).prop('checked'));
    });
    
    jQuery(document).on('change', '.student_id', function() {
        if (jQuery(this).is(':checked')) {
            if (jQuery('.student_id:checked').length == jQuery('.student_id').length) {
                jQuery('#selectAll').prop('checked', true);
            } else {
                jQuery('#selectAll').prop('checked', false);
            }
        } else {
            jQuery('#selectAll').prop('checked', false);
        }
    });
    
    0 讨论(0)
  • 2020-11-29 06:04

    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');
        })
    });    
    
    0 讨论(0)
  • 2020-11-29 06:11

    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>

    0 讨论(0)
  • 2020-11-29 06:13

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

    0 讨论(0)
提交回复
热议问题