gpt4 book ai didi

javascript - 在 dataTable 的单列中搜索多个值(可能使用数组?)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:31:50 26 4
gpt4 key购买 nike

我有一个数据表,其中填充了处于草稿、待批准、已批准或已拒绝的项目。项目的状态在一个特定的列中。

我想在此列中搜索多种类型的状态。

例如,我想搜索待定和已批准的项目,然后重绘表格以仅显示那些待定或已批准的项目。

关键是我想通过复选框动态更改此搜索字符串。

搜索使用硬编码值:

$('#theTable').DataTable().search('Pending').draw();

甚至

$('#theTable').DataTable().search('Pending'|'Approved').draw();

但我想根据复选框动态更改搜索字符串('Pending'|'Approved' 部分)。

所以...

if($("#Pending").is(":checked")) {
searchString += 'Pending';
$('#theTable').DataTable().search(searchString).draw();
}

if($("#Approved").is(":checked")) {
searchString += 'Approved';
$('#theTable').DataTable().search(searchString).draw();
}

但这行不通。我试过连接,使用数组,使用 fnFilter 而不是搜索,但似乎没有任何效果。

有什么想法吗??

最佳答案

这是通过使用数组添加搜索项,然后遍历数组并用管道连接它们来解决的。

以下是使用的内容:

 var whatsSelected = [];
$.each($('.statusChk'), function () {
if ($(this).is(":checked")) {
whatsSelected.push('(?=.*' + $(this).val() + ')');
}
});

$('#theTable').DataTable().search(whatsSelected.join('|'), true, false, true).draw();

添加 (?=.* 的字符串,然后关闭正则表达式(?我认为这就是 false 所做的)是完成这项工作所必需的。

关于javascript - 在 dataTable 的单列中搜索多个值(可能使用数组?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38232383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com