jQuery DataTables - 按完全匹配过滤列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8609577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery DataTables - Filter column by exact match
提问by JimmyJammed
Trying to only display exact matches to the search term entered in the search bar.
尝试只显示与在搜索栏中输入的搜索词完全匹配的内容。
For instance, I have a search bar that filters by ID#. I want only records that match the exact # entered to display.
例如,我有一个按 ID# 过滤的搜索栏。我只想显示与输入的确切 # 匹配的记录。
So if 123
is entered, I don't want 12345
, 91239
, etc etc to be displayed. Only 123
.
因此,如果123
输入的,我不想12345
,91239
等等来显示。只有123
。
Saw some info about bRegex
on the FAQ page, but it's not working for me. Any ideas?
bRegex
在常见问题页面上看到了一些信息,但它对我不起作用。有任何想法吗?
采纳答案by JimmyJammed
Ok solved the problem. However, since the column I am using the exact match on sometimes contains multiple ID #s seperated by commas, I wont be able to use an exact match search.
好的解决了问题。但是,由于我使用精确匹配的列有时包含多个以逗号分隔的 ID #,因此我将无法使用精确匹配搜索。
But for those interested, here is the answer:
但对于那些感兴趣的人,这里是答案:
oTable.fnFilter( "^"+TERM+"$", COLUMN , true); //Term, Column #, RegExp Filter
回答by Neeno Xavier
This will give you exact result for a column.
这将为您提供列的确切结果。
table.column(i)
.search("^" + $(this).val() + "$", true, false, true)
.draw();
ie . search( input , regex, smart , caseInsen )
IE 。搜索(输入,正则表达式,智能,caseInsen)
回答by Keith.Abramo
$(document).ready( function() {
$('#example').dataTable( {
"oSearch": {"bSmart": false}
} );
} )
Try using the bSmart option and setting it to false
尝试使用 bSmart 选项并将其设置为 false
From the documentation
从文档
"When "bSmart" DataTables will use it's smart filtering methods (to word match at any point in the data), when false this will not be done."
“当“bSmart”DataTables 将使用它的智能过滤方法(在数据中的任何点进行单词匹配)时,如果为 false,则不会这样做。”
UPDATE
更新
I found this:
我找到了这个:
oSettings.aoPreSearchCols[ iCol ].sSearch = "^\s*"+'1'+"\s*$";
oSettings.aoPreSearchCols[ iCol ].bRegex = false;
oSettings.aoPreSearchCols[ iCol ].bSmart= false;
at this link http://www.datatables.net/forums/discussion/4096/filtering-an-exact-match/p1
在此链接http://www.datatables.net/forums/discussion/4096/filtering-an-exact-match/p1
looks like you can set bSmart
and bRegex
per column as well as specifying a manual regex per column.
看起来您可以设置bSmart
和bRegex
每列以及为每列指定一个手动正则表达式。
回答by Tariq
If you want the exact match from the beginning you can try this code,
如果你想从一开始就完全匹配,你可以试试这个代码,
var table = $('#myTable').DataTable()
$('#filterrow > th:nth-child(2) > input').on( 'keyup change', function () {
table
.column( $(this).parent().index()+':visible' )
.search( "^" + this.value, true, false, true )
.draw();
} );
回答by Wolverine
You can use regular expression for exact matching as following:
您可以使用正则表达式进行精确匹配,如下所示:
var table = $('#dt').DataTable();
$('#column3_search').on('keyup', function () {
// Note: column() accepts zero-based index meaning the index of first column is 0, second column is 1 and so on.
// We use `2` here as we are accessing 3rd column whose index is 2.
table.column(2)
.search("^" + this.value + "$", true, false, true)
.draw();
});
The syntax of the search
function is:
该search
函数的语法是:
search(input, regex, smart_search, case_insensitive)
搜索(输入,正则表达式,智能搜索,不区分大小写)
We disable smart searchin this case because search
function uses regular expression internally when smart searchis set to true. Otherwise, this creates conflict between our regular expression and the one that is used by search
function.
在这种情况下我们禁用智能搜索,因为search
当智能搜索设置为 true时,函数在内部使用正则表达式。否则,这会在我们的正则表达式和search
函数使用的正则表达式之间产生冲突。
For more information, check out the following documentation from DataTable:
有关更多信息,请查看DataTable 中的以下文档:
Hope it's helpful!
希望它有帮助!
回答by anilam
just set the regex and smart false. and you get the exact result.
只需设置正则表达式和智能假。你会得到确切的结果。
$('#yourTableID').DataTable({
search: {
regex: false,
smart: false
}
})
回答by Alejandro Marin
The current versions of Datatables supports using real exact matching on a column basis.
当前版本的数据表支持在列的基础上使用真正的精确匹配。
table.column(i)
.search($(this).val(), false, false, false)
.draw();
The documentation explains each flag.
该文档解释了每个标志。
回答by matino
$(document).ready(function() {
tbl = $('#example').dataTable();
tbl.fnFilter("^" + filter_value + "$");
});
Where filter_value
is the string entered in the filter field.
在filter_value
过滤器字段中输入的字符串在哪里。
回答by Benjie T
table.column(col_num).search(filter_value + "$", true, true, false).draw();
table.column(col_num).search(filter_value + "$", true, true, false).draw();