[英]Datatables - search by string, not substring
I've been trying to make search in datatables to search for full string, not substring of that string.我一直在尝试在数据表中搜索以搜索完整字符串,而不是该字符串的 substring。
I have usernames: Ivan Test and Test Usiver If I search for "iv" or "Iv" result I would expect is only "Ivan Test", but I get both users, as second user contains inside of his string "Test UsIVer"我有用户名:Ivan Test 和 Test Usiver 如果我搜索“iv”或“Iv”结果,我希望只有“Ivan Test”,但我得到了两个用户,因为第二个用户在他的字符串“Test UsIVer”中包含
I've tried with this: $('#TablicaRacuni').DataTable().search($(this).val(), false, false, false);
我试过这个:
$('#TablicaRacuni').DataTable().search($(this).val(), false, false, false);
and this $('#TablicaRacuni').DataTable().search("^" + $(this).val() + "$", true, false, true).draw();
还有这个
$('#TablicaRacuni').DataTable().search("^" + $(this).val() + "$", true, false, true).draw();
With second function there is bug as ^ and $ shows up on search input box like this:对于第二个 function 存在错误,因为 ^ 和 $ 显示在搜索输入框中,如下所示:
All random combinations of true and false and can't get this to work exactly how I want.所有真假的随机组合,无法让它完全按照我想要的方式工作。
Stock function to print table: $('#TablicaRacuni').DataTable();
库存 function 打印表:
$('#TablicaRacuni').DataTable();
It doesn't have to be specific column search, I want all columns to be searchable for example If i input "d" I would expect all diesel bills and all users with "d" for first letter.它不必是特定的列搜索,我希望所有列都是可搜索的,例如如果我输入“d”,我希望所有柴油账单和所有用户都以“d”作为首字母。
You can use the search plug-in to intercept search (filter) events, and then apply your own filtering logic.您可以使用搜索插件拦截搜索(过滤)事件,然后应用您自己的过滤逻辑。 The plug-in is already available - you do not need to add it.
该插件已经可用 - 您无需添加它。
Here is an example for "begins with", also assuming that the search is case insensitive:以下是“开头为”的示例,同时假设搜索不区分大小写:
$(document).ready(function() {
$('#example').DataTable( {
// your normal initialization options here
// no need for any special search options
} );
$.fn.dataTable.ext.search.push(
function( settings, searchData, index, rowData, counter ) {
var match = false;
var searchTerm = settings.oPreviousSearch.sSearch.toLowerCase();
searchData.forEach(function (item, index) {
if (item.toLowerCase().startsWith(searchTerm)) {
match = true;
}
} );
return match;
}
);
} );
The search term is assumed to be entered into the standard global search field, provided by DataTables out-of-the-box.假设搜索词输入到标准全局搜索字段中,由 DataTables 开箱即用提供。 We get the user-entered search term from
settings
.我们从
settings
获取用户输入的搜索词。
Each searchData
represents one row of data, as an array of values - so we iterate over each value looking for a match.每个
searchData
代表一行数据,作为一个值数组 - 因此我们遍历每个值以寻找匹配项。
(If you wanted to change this to "exact match", you would alter the if
condition accordingly. The only downside here is that the user experience might be a bit surprising. Each keystroke causes a re-filtering of the data. So, you may find every row disappears, until you have typed in an exact match for a field. In this case, a search "submit" button might give a better user experience.) (如果您想将其更改为“完全匹配”,您将相应地更改
if
条件。这里唯一的缺点是用户体验可能有点令人惊讶。每次击键都会导致重新过滤数据。所以,您可能会发现每一行都消失了,直到您为某个字段输入了完全匹配的内容。在这种情况下,搜索“提交”按钮可能会提供更好的用户体验。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.