如何在jquery中同时访问一行完整的数据表

how to access a row of whole datatable at the same time in jquery

本文关键字:一行 数据表 jquery 访问      更新时间:2023-09-26

我使用了DataTables jQuery插件。在DataTable中,我在标题中创建了一个复选框,每个列也有一个复选框。

那个DataTable有几个页。我需要的是当我选中header复选框时,所有的复选框都应该在DataTable的所有页面中选中。

我必须同时访问所有页面的行。

I tried this

if($('#selectAll').is(':checked')){
    $(nRow).find(':input[name=group_select_components]').iCheck('check');
}

我可以在点击分页时勾选复选框。但是我想同时访问数据表的一行

试试下面的代码!!

if($('#selectAll').is(':checked')){
    var table = $('#yourTableId').DataTable();
    var cells = table
        .cells( ":checkbox" )
        .nodes();
    $(cells).iCheck('check');
}

if($('#selectAll').is(':checked')){
    var table = $('#yourTableId').DataTable();
    $(':checkbox', table.rows().nodes()).iCheck('check');
}

如果您的表有id或类,则可以这样做。

if($('#selectAll').is(':checked'))
 $("#YourTableId tr td:first-child").each(function(){$(this).find(':input[name=group_select_components]').iCheck('check')});

这里它会在first td中找到复选框,如果你的复选框在另一个td中,你可以通过使用.eq()来给出index。这将只循环特定的tds

或者如果你不想设置索引,你可以简单地遍历行

if($('#selectAll').is(':checked'))
 $("YourTableId tr").each(function(){$(this).find(':input[name=group_select_components]').iCheck('check')})

您可以通过为所有复选框分配相同的类来实现。所以当你点击全选时,所有的框都会被选中。

试试这个代码:

$(document).ready(function() {
    $('#selecctall').click(function(event) {  //click event
        if(this.checked) { // Check if the box is checked
            $('.checkbox').each(function() { //loop through each checkbox
                this.checked = true;  //select all checkboxes with class "checkbox"               
            });
        }else{
            $('.checkbox').each(function() { //loop through each checkbox
                this.checked = false; //deselect all checkboxes with class "checkbox"                       
            });         
        }
    });
});