jQuery dataTables - 获取过滤的列值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27159614/
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 11:21:48  来源:igfitidea点击:

jQuery dataTables - get filtered column values

jqueryjquery-datatables

提问by foop

I am using a jQuery dataTable and when the user selects a drop down it searches the data table and filters it and redraws the contents based upon the searched data :

我正在使用 jQuery 数据表,当用户选择一个下拉列表时,它会搜索数据表并对其进行过滤并根据搜索到的数据重新绘制内容:

mtTable.columns().each(function() {
    mtTable.column(22).search(searchVal, true, true).draw();
});

Now I am trying to get all of the column values after a search is done, however I cannot find a function to do this. Currently I am using from the api

现在我试图在搜索完成后获取所有列值,但是我找不到执行此操作的函数。目前我正在使用 api

var myTable = $("#tblResults").DataTable();
var resultsArray = myTable.columns(colIndex).data();

According to the documentation this will return all of the data from within the column unfiltered. I cannot find a function to give me an array of the column values for the filtered data only.

根据文档,这将返回未过滤的列中的所有数据。我找不到一个函数来为我提供一个仅用于过滤数据的列值数组。

回答by davidkonrad

You can read all about dataTables advanced selector-modifiershere -> http://datatables.net/reference/type/selector-modifier

您可以selector-modifiers在此处阅读有关高级数据表的所有信息-> http://datatables.net/reference/type/selector-modifier

If you want to get filtered rows only :

如果您只想获得过滤的行:

table.rows( { search:'applied' } ).data().each(function(value, index) {
    console.log(value, index);
});

To target a specific column, and get filtered values only (your specific request) - here all filtered values from column #2 :

要定位特定列,并仅获取过滤值(您的特定请求) - 这里是列 #2 中的所有过滤值:

table.column(2, { search:'applied' } ).data().each(function(value, index) {
    console.log(value, index);
});

See demo with both -> http://jsfiddle.net/q0e1bdcz/

使用两者查看演示 -> http://jsfiddle.net/q0e1bdcz/

To create an array over filtered values for a specific column :

要在特定列的过滤值上创建数组:

var array = [];
table.column(2,  { search:'applied' } ).data().each(function(value, index) {
    array.push(value);
});
console.log(array);

See demo -> http://jsfiddle.net/q0e1bdcz/1/

见演示-> http://jsfiddle.net/q0e1bdcz/1/

回答by Raja

You can also get unique and sorted data if you have more number of entries.

如果您有更多条目,您还可以获得唯一和排序的数据。

// Datatable object
var table = $('#example').DataTable();

// Get Unique and Sorted values.  
table.column(3, { search:'applied' } ).data().unique().sort().each(function(value, index) {
    console.log(value, index);
});

Ref: http://www.jqueryscript.net/demo/DataTables-Jquery-Table-Plugin/examples/api/multi_filter_select.html

参考:http: //www.jqueryscript.net/demo/DataTables-Jquery-Table-Plugin/examples/api/multi_filter_select.html

Hope this will also help.

希望这也会有所帮助。