从 DataTable 中获取所有行,包括过滤掉的行

Get all rows from DataTable including those filtered out

我正在使用优秀的 jQuery Datatables 插件。

我有一个简单的 table,其中每一行的第一列是一个带有 class .chk-items.

的复选框

我希望用户能够 select 多行,然后对选中复选框的所有行执行操作。这可能包括选中一个或多个框,使用搜索工具进行过滤,然后选中其他框。这当然意味着一些被选中的行不再可见(即它们已被过滤掉)。

我目前有以下方法来处理选中的行:

var tableData = new Array([]);
var idx = 0;
$('.chk-items').each(function() {
//Add checked items to next tab
    if($(this).is(':checked')) {
        //Get the selected row data into an array
        var rowData = (this).closest('tr').children("td").map(function() {
            return $(this).text();
        }).get();
        tableData[idx] = rowData;
        idx = idx + 1;
    }
})

但是,当其中一个选中的行被搜索过滤掉时,这将不起作用。

任何人都可以建议一种方法来更改此设置以处理搜索过滤掉的选中行吗?

解决方案

使用 DataTables API 方法 $() 对整个 table.

执行 jQuery 选择操作
var table = $('#example').DataTable();

// ... skipped ...

var tableData = [];
table.$('.chk-items').each(function() {
   // Add checked items to next tab
    if($(this).is(':checked')) {
        // Get the selected row data into an array
        var rowData = $(this).closest('tr').children("td").map(function() {
            return $(this).text();
        });

        tableData.push(rowData);
    }
});

演示

有关代码和演示,请参阅 this jsFiddle

链接

有关在 jQuery DataTables 中使用复选框的详细信息,请参阅 jQuery DataTables - How to add a checkbox column