简体   繁体   English

如何将复选框全选/取消全选添加到 HTML data.table

[英]How to add checkbox Select/Unselect All to HTML data table

I have this datatable:我有这个数据表:

<div class="card-body">
    <table id="example1" class="table table-bordered table-striped">
        <thead>
            <tr>
                <th>
                    <input type="checkbox" onchange="checkAll(this)" name="chk[]" />
                </th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Department</th>
                <th>Admission Year</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td valign="middle" align="center" style="width: 2%;">
                   
                </td>
                <td></td>
                <td>
   
                </td>
                <td></td>
                <td> </td>
                <td></td>
            </tr>
        </tbody>
    </table>
</div>

The above is a datatable that have pagination.上面是一个有分页的数据表。 Then I added this script below to achieve my goal.然后我在下面添加了这个脚本来实现我的目标。

<script>
    $(function () {
        $("#example1").DataTable({
            "responsive": true, "lengthChange": false, "autoWidth": false
        }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
        $('#example2').DataTable({
            "paging": true,
            "lengthChange": false,
            "searching": false,
            "ordering": true,
            "info": true,
            "autoWidth": false,
            "responsive": true,
        });
    });
    function checkAll(ele) {
        var checkboxes = document.getElementsByTagName('input');
        if (ele.checked) {
            for (var i = 0; i < checkboxes.length; i++) {
                if (checkboxes[i].type == 'checkbox') {
                    checkboxes[i].checked = true;
                }
            }
        } else {
            for (var i = 0; i < checkboxes.length; i++) {
                console.log(i)
                if (checkboxes[i].type == 'checkbox') {
                    checkboxes[i].checked = false;
                }
            }
        }
    }
</script>

I want to add checkbox to Select/UnselectAll, and to also check/uncheck each row.我想将复选框添加到 Select/UnselectAll,并选中/取消选中每一行。

The script above is not doing it.上面的脚本没有这样做。

How do I achieve this?我如何实现这一目标?

Thank感谢

 $(function() { $("#example1").DataTable({ "responsive": true, "lengthChange": false, "autoWidth": false }).buttons().container().appendTo('#example1_wrapper.col-md-6:eq(0)'); $('#example2').DataTable({ "paging": true, "lengthChange": false, "searching": false, "ordering": true, "info": true, "autoWidth": false, "responsive": true, }); }); function checkAll(ele) { var checkboxes = document.getElementsByTagName('input'); if (ele.checked) { for (var i = 0; i < checkboxes.length; i++) { if (checkboxes[i].type == 'checkbox') { checkboxes[i].checked = true; } } } else { for (var i = 0; i < checkboxes.length; i++) { console.log(i) if (checkboxes[i].type == 'checkbox') { checkboxes[i].checked = false; } } } } function checkSingle(ele) { var checkbox = document.getElementsByTagName('input'); console.log(ele.checked) if (ele.checked) { if (checkbox.type == 'checkbox') { checkbox.checked = false; } } else { checkbox.checked = true; } }
 <div class="card-body"> <table id="example1" class="table table-bordered table-striped"> <thead> <tr> <th> <input type="checkbox" onchange="checkAll(this)" name="chk[]" /> </th> <th>First Name</th> <th>Last Name</th> <th>Department</th> <th>Admission Year</th> <th>Status</th> </tr> </thead> <tbody> <tr> <td valign="middle" align="center" style="width: 2%;"> <input type="checkbox" onchange="checkSingle(this)" name="chk[]" /> </td> <td>test</td> <td> test </td> <td>test</td> <td>test </td> <td>test</td> </tr> <tr> <td valign="middle" align="center" style="width: 2%;"> <input type="checkbox" onchange="checkSingle(this)" name="chk[]" /> </td> <td>test</td> <td> test </td> <td>test</td> <td>test </td> <td>test</td> </tr> </tbody> </table> </div>

Is this what you were looking for?这是你要找的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM