15

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

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

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

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

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

4

2 回答 2

42

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

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

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

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

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

两者都见演示-> http://jsfiddle.net/q0e1bdcz/

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

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

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

于 2014-11-27T09:12:07.003 回答
4

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

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

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

希望这也会有所帮助。

于 2014-12-19T09:30:03.333 回答