[英]How to apply filter on data from a jquery datatable?
I have a jquery datatable which I am showing on UI.我有一个在 UI 上显示的 jquery 数据表。 I want to apply filter on the datatable data and store the filtered data into a variable (not looking for filtering the data on UI).
我想对数据表数据应用过滤器并将过滤后的数据存储到一个变量中(不寻找过滤 UI 上的数据)。
I tried the below code but it returns the same rows as that of the original datatable.我尝试了下面的代码,但它返回与原始数据表相同的行。
var filteredData = $('#table').DataTable().column(1).filter( function ( value, index ) {
return value == 'test' ? true : false;
} ).data();
$('#table').DataTable().column(1).filter( function ( value, index ) {
console.log("VALUE: " + value);
console.log("INDEX: " + index);
} ).data();
This is showing next, is not correct:这是接下来显示的,不正确:
VALUE: 1 INDEX: 0
值:1 索引:0
So... change to this:所以……改成这样:
var filteredData = $('#table').DataTable().column(1).data().filter( function ( value, index ) {
return value=="P";
}).toArray();
console.log("FILTERED DATA: " + filteredData);
This is showing next:这是接下来显示:
FILTERED DATA: P,P
过滤数据:P,P
As you can see, this only stores the column that you are filtering... If you want to store other column (and in general), I would use this other method ( Reference ):如您所见,这仅存储您正在过滤的列...如果您想存储其他列(以及一般情况下),我将使用其他方法(参考):
var array = [];
$('#table').DataTable().rows().every( function ( rowIdx, tableLoop, rowLoop ) {
var data = this.data();
if (data[1] == "P"){
array.push(data[5])
}
// ... do something with data(), or this.node(), etc
} );
console.log(array);
As you can see, this stores the value of column 5 filtering by column 1.如您所见,这存储了按第 1 列过滤的第 5 列的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.