jQuery dataTables - get filtered column values
我使用的是jQuery dataTable,当用户选择一个下拉列表时,它将搜索数据表并对其进行过滤,并根据搜索到的数据重新绘制内容:
1 2 3 | mtTable.columns().each(function() { mtTable.column(22).search(searchVal, true, true).draw(); }); |
现在,我尝试在搜索完成后获取所有列值,但是找不到用于执行此操作的函数。 目前我正在使用api
1 2 | var myTable = $("#tblResults").DataTable(); var resultsArray = myTable.columns(colIndex).data(); |
根据文档,这将从未经过滤的列中返回所有数据。 我找不到一个仅提供过滤数据列值数组的函数。
您可以在此处-> http://datatables.net/reference/type/selector-modifier阅读有关dataTables Advanced
如果只想获取过滤的行:
1 2 3 | table.rows( { search:'applied' } ).data().each(function(value, index) { console.log(value, index); }); |
定位到特定列,并仅获取过滤值(您的特定请求)-这里是列#2中的所有过滤值:
1 2 3 | table.column(2, { search:'applied' } ).data().each(function(value, index) { console.log(value, index); }); |
两者都查看演示-> http://jsfiddle.net/q0e1bdcz/
要为特定列的过滤值创建一个数组:
1 2 3 4 5 | var array = []; table.column(2, { search:'applied' } ).data().each(function(value, index) { array.push(value); }); console.log(array); |
参见演示-> http://jsfiddle.net/q0e1bdcz/1/
如果您有更多的条目,则还可以获得唯一且已排序的数据。
1 2 3 4 5 6 7 | // 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
希望这也会有所帮助。