jquery 数据表。如何获得过滤(可见)行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6478414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:56:15  来源:igfitidea点击:

jquery DataTables. How to get filtered (visible) rows

jquerydatatables

提问by Yuri

I have a button that apply filter to jquery datatable

我有一个将过滤器应用于 jquery 数据表的按钮

$("#buttonFilter").button().click(function() {
                if (lboxColor.val() != null) {
                    jqTable.fnFilter($("option:selected", lboxColor).text(), 1);
                }
            });

It shows me for example 47 rows from 60. I tried .fnGetData() and fnGetNodes() but it shows me all rows, but not filtered. How could I get 47 rows?

例如,它显示了 60 行中的 47 行。我尝试了 .fnGetData() 和 fnGetNodes() 但它显示了所有行,但没有过滤。我怎么能得到 47 行?

回答by Hyman

I've been searching for about an hour, for anyone using DataTables 1.10+ if you want to get the filtered (better term: "searched") rows:

我已经搜索了大约一个小时,对于使用 DataTables 1.10+ 的任何人,如果您想获得过滤的(更好的术语:“搜索”)行:

var table = $('.table').DataTable({...});

function selectOnlyFiltered(){
   var filteredRows = table.rows({filter: 'applied'});
}

回答by RayLoveless

For datatables 1.9 and later this solution works:

对于数据表 1.9 及更高版本,此解决方案有效:

myDataTableHandle = $('#example1').dataTable(...);
...
...
var myFilteredRows = myDataTableHandle._('tr', {"filter":"applied"});

and you won't have to include a separate api plugin. :)

并且您不必包含单独的 api 插件。:)

回答by Bedouin

Just in case you want a collection of nodes (DOM elements) just like fngetNodes(), you can use the '$' instead of the '_' like this table.$('tr', {"filter":"applied"});

以防万一您想要像 fngetNodes() 一样的节点(DOM 元素)集合,您可以像这样使用 '$' 而不是 '_' table.$('tr', {"filter":"applied"});

the '_' returns a collection of 'TR' (html elements).

'_' 返回一个 'TR'(html 元素)的集合。

回答by Moebius

For those who are interested, this is a concrete usecase.

对于那些感兴趣的人,这是一个具体的用例。

/**
 * Select all the elements of the datatable which match the current user
 * search (or all if no search).
 */
function dtable_selectAll()
{
    idTable = 'myDataTable';
    var rows = $('#' + idTable).dataTable()
            .$('tr', {"filter":"applied"});
    var oTT = TableTools.fnGetInstance(idTable);
    $(rows).each(function (index, el){
        oTT.fnSelect(el);
    })
}

Hope it helps.

希望能帮助到你。

回答by billynoah

In current versions selector-modifieruses a slightly different set of properties.

在当前版本中,selector-modifier使用一组略有不同的属性。

var table = $('#example').DataTable();
var rows = table.rows({"search" : "applied"});

And you can iterate over the cell data like this:

您可以像这样迭代单元格数据:

table.rows({"search":"applied" }).every( function () {
    var data = this.data();
});

Some helpful links:

一些有用的链接:

回答by David Otto

For those struggling to get datafrom only one columnAND only from visible rows: you can actually combine the "selectors" like this

对于那些努力获得的数据,从只有一列仅可见行:实际上你可以结合“选择”这样的

var total = api
      .column(2, { search: 'applied' }) // <-- only 3rd column && only visible after applied search
      .data()
      .reduce(function (a, b) {
        return floatVal(a) + floatVal(b)
      }, 0)

I also notices several sources stating { page: 'current' } would be the right selector for currently visible rows, but this one actually gives you the rows visible after a possible pagination.

我还注意到几个消息来源指出 { page: 'current' } 将是当前可见行的正确选择器,但这个实际上为您提供了可能分页后可见的行。

回答by Gavin Brock

Just for completeness, and as Yuri mentioned, there are "plugins" that will provide the filtered TR Nodesor data.

只是为了完整性,正如 Yuri 提到的,有“插件”将提供过滤后的TR Nodesdata

To use these plugins, you will need to paste the code from those links into your script file.

要使用这些插件,您需要将这些链接中的代码粘贴到您的脚本文件中。