简体   繁体   English

使用 JQuery 使用逗号分隔的多个值过滤 HTML 表

[英]Filter HTML Table using multiple values separated by Comma using JQuery

I have a filter code which is working fine if i give a single value, but i have more than 100+ values in my table, and i have to search for two or three value in the table at same time, so i planned to use comma for that, like in a single search box i will write the values i have to search in a table column in a comma separated fashion.我有一个过滤器代码,如果我给出一个值,它工作正常,但我的表中有超过 100 个值,我必须同时在表中搜索两个或三个值,所以我打算使用逗号,就像在单个搜索框中一样,我将以逗号分隔的方式在表格列中写入我必须搜索的值。

SO currently it is working, but it is not filtering when i give comma, Since am new to this scripting and web development, Can anyone help me.所以目前它正在工作,但是当我给逗号时它没有过滤,因为我是这个脚本和 web 开发的新手,任何人都可以帮助我。

 $('#tblfiles').dataTable({ "search": { "smart": true, "regex": true } });
 <script src="https://code.jquery.com/jquery-3.3.1.js"></script> <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> <script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script> <div id="nam"></div> <br> <table id="tblfiles"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Country</th> <th>StDID</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>pooe</td> <td>country1</td> <td>Sgyt13</td> </tr> <tr> <td>2</td> <td>AAA</td> <td>country2</td> <td>P5372</td> </tr> <tr> <td>3</td> <td>BBB</td> <td>country3</td> <td>P5972</td> </tr> </tbody> </table>

So here i have to fetch rows which contain Name as AAA and BBB, so instead on searching and getting value one by one, in search bar i have to give AAA, BBB.. so that it will fetch both corresponding rows to me.因此,在这里我必须获取包含名称为 AAA 和 BBB 的行,因此在搜索并一一获取值时,我必须在搜索栏中提供 AAA、BBB .. 以便它将对应的行都获取给我。

JSfiddle 的图像

So here it replaces the first % but not the second所以在这里它取代了第一个 % 但不是第二个

Using Regex Search使用正则表达式搜索

The DataTables Search Option supports regex as a search input. DataTables 搜索选项支持正则表达式作为搜索输入。 If you'll disable smart search , and only enable regex search , you can use the regex syntax to lookup multiple values in the table.如果您将禁用smart search并仅启用regex search ,则可以使用 regex 语法在表中查找多个值。

This might work for you as is since you can look up multiple values like this: country1|country2这可能对您有用,因为您可以像这样查找多个值: country1|country2

$('#tblfiles').dataTable({
  'search': {
    'smart': false,
    'regex': true
  }
});

Exact Match完全符合

If you'll search for country1|country2 , the table will find all items that contain either country1 or country2 .如果您搜索country1|country2 ,该表将找到包含country1country2的所有项目。 Using ^ and $ to specify where the match should be, doesn't work with dataTables as expected.使用^$来指定匹配的位置,不能按预期与 dataTables 一起使用。

If you want to make country1|country2 return an exact match, simulating the ^ and $ use , you can wrap the search term with \\b , which in this case, works similar to ^ and $ .如果你想让country1|country2返回一个完全匹配,模拟^$ use ,你可以用\\b包装搜索词,在这种情况下,它的工作方式类似于^$

// exact match
const regex = '\\b(' + searchTerm + ')\\b';

Using commas as delimiters使用逗号作为分隔符

If you want to use commas as delimiters instead of the regex's default of |如果您想使用逗号作为分隔符而不是正则表达式的默认值| , you can use the same function to replace all commas in the search term to pipes. ,您可以使用相同的 function 将搜索词中的所有逗号替换为管道。 This way, your users will use commas to search multiple terms at the same time instead of |这样,您的用户将使用逗号同时搜索多个术语,而不是| :

// comma delimiter
const searchTerm = this.value.toLowerCase().replace(/,/g, '|');

Examples (with Exact Match enabled)示例(启用精确匹配)

  • Search: country1,country2搜索: country1,country2
  • Result: Will return country1 and country2 specifically结果:将具体返回 country1 和 country2

  • Search: country[1-2][0-9]搜索: country[1-2][0-9]
  • Result: Will return all countries between country10 and country 29结果:将返回国家 10 和国家 29 之间的所有国家

  • Search: o.*搜索: o.*
  • Result: Will return all rows containing a string that starts with o.结果:将返回包含以 o 开头的字符串的所有行。 Without adding the .* , the search will look for an exact match for o (surrounded by something that is not a word) and will return nothing.在不添加.*的情况下,搜索将寻找 o 的完全匹配项(被非单词包围)并且不会返回任何内容。

The main difference is if the search regex is strict or loose主要区别在于搜索正则表达式是严格还是松散

 const table = $('#tblfiles'); /* NOT IMPORTANT FOR ANSWER */ /* This is here just to generate data in the table */ const tableBody = table.find('tbody'); for (const x of Array(100).keys()) { tableBody.append(`<tr> <td>${ x }</td> <td>${ chance.name() }</td> <td>country${ x }</td> <td>${ chance.string({ length: 4, alpha: true, numeric: true }) }</td> </tr>`) } /* ANSWER CONTINUE HERE */ $('#tblfiles').dataTable({ 'search': { 'smart': false, 'regex': true } }); // This is used to force the search value to search whole words. // This makes sure that searching for `country1|country2` will // still find an exact match $('.dataTables_filter input').unbind().bind('keyup', function() { const searchTerm = this.value.toLowerCase().replace(/,/g, '|'); const regex = '\\b(' + searchTerm + ')\\b'; table.DataTable().rows().search(regex, true, false).draw(); });
 body { font-size: 12px;important: padding; 2em. }:dataTables_length { display; none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/chance/1.1.7/chance.min.js"></script> <link href="https://cdn.datatables.net/1.10.24/css/dataTables.bootstrap4.min.css" rel="stylesheet" /> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css" rel="stylesheet" /> <script src="https://code.jquery.com/jquery-3.3.1.js"></script> <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> <script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script> <div id="nam"></div> <br> <table id="tblfiles" class="table table-striped table-bordered" style="width:100%"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Country</th> <th>StDID</th> </tr> </thead> <tbody> </tbody> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM