AngularJS过滤多个字符串 [英] AngularJS filter for multiple strings
问题描述
我通过AngularJS教程的学习和理解的基础知识
I'm working through the AngularJS tutorial, and understand the basics of
不过,开箱实施似乎仅限于项目列表过滤只是在输入的确切单词或短语。
However, the out of the box implementation seems limited to just filter the list of items to the exact word or phrase entered in .
例如:如果查询是桌布,结果列表可以包括与这句话,装饰台布的结果,但不包括对表装饰布,因为过滤器只是一个连续搜索字符串。
Example: if the query is "table cloth", the result list can include a result with this phrase, "Decorative table cloth", but won't include "Decorative cloth for table" because the filter is just a continuous search string.
我知道有添加自定义过滤器的能力,但乍一看好像这些都是主要变换。
I know there's the ability to add custom filters, but at first glance it seems like those are mainly transforms.
有没有什么办法来添加自定义过滤器,这样既有为表装饰布艺和装饰台布的筛选结果显示设置?
Is there any way to add a custom filter so that both "Decorative cloth for table" and "Decorative table cloth" show up in the filtered result set?
推荐答案
只是自己的过滤器卷:
.filter("myFilter", function(){
return function(input, searchText){
var returnArray = [];
var searchTextSplit = searchText.toLowerCase().split(' ');
for(var x = 0; x < input.length; x++){
var count = 0;
for(var y = 0; y < searchTextSplit.length; y++){
if(input[x].toLowerCase().indexOf(searchTextSplit[y]) !== -1){
count++;
}
}
if(count == searchTextSplit.length){
returnArray.push(input[x]);
}
}
return returnArray;
}
});
的jsfiddle : http://jsfiddle.net/Cq3PF/
这个过滤器可以确保所有的搜索词被找到。
This filter makes sure that all search words are found.
这篇关于AngularJS过滤多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!