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

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

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

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

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

我试过了

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

我可以在单击分页时选中复选框。但是我想同时访问一行dataTable..

试试下面的代码!!

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');
}

您可以通过为所有复选框指定相同的 class 来实现。因此,当您单击 select 全部时,所有框都将被选中。

试试这个代码:

$(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"                       
            });         
        }
    });

});

如果您的 table 有 id 或 class,您可以这样做。

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

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

上循环

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

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