简体   繁体   English

具有多个单词的实时搜索过滤器,使用AND代替OR

[英]Live Search Filter with Multiple Words, Use AND instead of OR

I have a live search autocomplete filter that I'm using to weed out a directory of people. 我有一个实时搜索自动填充过滤器,用于清除人员目录。 Using this search, I want people to be able to search using multiple words, which will further reduce the number of results they get. 通过这种搜索,我希望人们能够使用多个单词进行搜索,这将进一步减少他们获得的结果数量。

Currently, with the code below, if someone searches "technician Atlanta", they will get the directoryprofile divs that contain "technician" and the ones that contain "Atlanta". 当前,使用下面的代码,如果有人搜索“ technical Atlanta”,他们将获得包含“ technical”的目录配置文件div和包含“ Atlanta”的目录配置文件div。 What I want is for it to find a div that contains BOTH of those words...so they can find a technician that is in Atlanta. 我想要的是找到一个包含这两个词的div ...以便他们可以找到位于亚特兰大的技术人员。 Hopefully that makes sense. 希望这是有道理的。

My code is working for including the terms separately, but I want them to find rows containing multiple terms. 我的代码致力于单独包含这些术语,但是我希望他们查找包含多个术语的行。 Any ideas? 有任何想法吗? THANKS in advance!! 提前致谢!!

 $("#filter").keyup(function () { // Split the current value of the filter textbox var data = this.value.split(" "); // Get the table rows var rows = $(".directoryprofile"); if (this.value == "") { rows.show(); return; } // Hide all the rows initially rows.hide(); // Filter the rows; check each term in data rows.filter(function (i, v) { for (var d = 0; d < data.length; ++d) { if ($(this).is(":contains('" + data[d] + "')")) { return true; } } return false; }) // Show the rows that match. .show(); }); 

 rows.filter(function(i, v) { var truth = true; for (var d = 0; d < data.length; ++d) { //remain true so long as all of the filters are found //if even one is not found, it will stay false truth = truth && $(this).is(":contains('" + data[d] + "')"); } return truth; }) //OR you could just flip your logic and return false if any of the //filters are not found rows.filter(function(i, v) { for (var d = 0; d < data.length; ++d) { if (!$(this).is(":contains('" + data[d] + "')")) { return false; } } return true; }) 

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

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