如何使用fnFilter进行精确匹配?

9

我正在使用datatables中的fnFilter函数,在尝试过滤“inv”时,所有以“inv”开头的内容都被过滤掉了,包括“invc”和“invk”。如何解决这个问题并只获取精确匹配的结果?

代码:

$("#user-lock-status-filter select").change(function() {
        oUserTable.fnFilter($(this).val(), 12);
    });

fnFilter 是做什么用的? - Johan
这是用于在列中过滤数据的内容。 - Davis
1个回答

16

更改此处

oUserTable.fnFilter($(this).val(), 12);

oUserTable.fnFilter("^"+$(this).val()+"$", 12, false, false); 
//disabling smart search/regex and apply your own search

示例

文档

fnFilter的参数

1.{string}: String to filter the table on
2.{int|null}: Column to limit filtering to
3.{bool} [default=false]: Treat as regular expression or not
4.{bool} [default=true]: Perform smart filtering or not
5.{bool} [default=true]: Show the input global filter in it's input box(es)
6.{bool} [default=true]: Do case-insensitive matching (true) or not (false)

它不起作用,我仍然在“inv”搜索中得到“invc”行。 - Davis
@Damon,这是将用于过滤表格的正则表达式。^表示字符串的开头,$表示结尾。因此,^searched$将仅匹配包含完全“searched”的行。 - Gus
1
谢谢!我认为我的主要困惑是,你必须将“视为正则表达式”设置为false,才能将其视为正则表达式... - Damon
2
所以我认为解决方案有点不对...要么禁用智能过滤和正则表达式,使用常规搜索字符串,要么编写正则表达式并将“视为正则表达式”设置为true。 - tObi
1
ouserTable是什么?我认为它是一个datatable变量。但是我已经使用了$('.dataTable').dataTable();来创建一个jquery datatable。 - Avinash Raj
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接