多个字符串的AngularJS过滤器 [英] AngularJS filter for multiple strings

查看:28
本文介绍了多个字符串的AngularJS过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 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?

推荐答案

请参阅 surfbuds 在下面回答,因为它更好

Please see surfbuds answer below as it is superior

只需使用您自己的过滤器:

Just roll with your own filter:

.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆