字符串我如何写一个angularjs过滤器来搜索字符串片段/序列 [英] How can I write an angularjs filter to search for string fragments/sequences of strings

查看:158
本文介绍了字符串我如何写一个angularjs过滤器来搜索字符串片段/序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个angularjs自定义过滤器来检查国家的数组是否包含用户输入的搜索字符串。

该字符串可以由一个字母(例如E),或正字母片段(例如'兰')或整个词(例如'英格兰')的。

在任何情况下,包含一个字母或碎片应该归还所有国家,所以'E'将返回英格兰,爱沙尼亚等。而'兰'将返回'英格兰','爱尔兰等

到目前为止,我的过滤器返回整个国家或单个字母,但我在使用字符串片段难度:


  1. HTML模板:

     <输入NG模型=filter.textTYPE =搜索占位符=过滤器.../>< UL>
       <李NG重复=项数据| listfilter:filter.text>
    < / UL>


  2. angularJS

      angular.module('sgComponents')。过滤器('listfilter',[功能(){
    返回功能(物品,SEARCHTEXT){
        变种过滤= [];    angular.forEach(项目功能(项目){
            如果(item.label === SEARCHTEXT){//全字匹配,例如'英国'
                filtered.push(项目);
            }
            无功字母= item.label.split('');
            _.each(字母,函数(字母){
                如果(信=== SEARCHTEXT){//匹配单个字母,例如'E'
                    的console.log('推');
                    filtered.push(项目);
                }
            });
            // code匹配信片段,例如'兰'
        });
        返回过滤;
    };
    }]);



解决方案

有比这更简单,使用 String.indexOf()功能:

  angular.forEach(项目功能(项目){
    如果(item.label.indexOf(SEARCHTEXT)> = 0)filtered.push(项目);
});

您可能需要打开两个字符串 .toLowerCase()做区分大小写的匹配:

  SEARCHTEXT = searchText.toLowerCase();
angular.forEach(项目功能(项目){
    如果(item.label.toLowerCase()的indexOf(SEARCHTEXT)方式> = 0)filtered.push(项目);
});

I'm trying to write an angularjs custom filter that checks whether an array of countries contains a search string entered by the user.

The string can consist of one letter (e.g. 'E'), or a fragment of n-letters (e.g. 'lan') or an entire word (e.g. 'England').

In every case, all countries containing that one letter or that fragment should be returned, so 'E' would return 'England', 'Estonia' etc. while 'lan' would return 'England', 'Ireland', etc.

So far my filter returns the entire country or single letters but I'm having difficulty with string fragments:

  1. HTML Template:

    <input ng-model="filter.text" type="search" placeholder="Filter..."/>
    
    <ul>
       <li ng-repeat="item in data | listfilter:filter.text">
    </ul>
    

  2. angularJS

    angular.module('sgComponents').filter('listfilter',[ function () {
    return function(items, searchText) {
        var filtered = [];            
    
        angular.forEach(items, function(item) {
            if(item.label === searchText) { // matches whole word, e.g. 'England'
                filtered.push(item);
            }
            var letters = item.label.split('');
            _.each(letters, function(letter) {
                if (letter === searchText) { // matches single letter, e.g. 'E'
                    console.log('pushing');
                    filtered.push(item);
                }
            });
            // code to match letter fragments, e.g. 'lan'
        });
        return filtered;
    };
    }]);
    

解决方案

It is much simpler than that, use the String.indexOf() function:

angular.forEach(items, function(item) {
    if( item.label.indexOf(searchText) >= 0 ) filtered.push(item);
});

You may want to turn both strings .toLowerCase() to do case-insensitive matching:

searchText = searchText.toLowerCase();
angular.forEach(items, function(item) {
    if( item.label.toLowerCase().indexOf(searchText) >= 0 ) filtered.push(item);
});

这篇关于字符串我如何写一个angularjs过滤器来搜索字符串片段/序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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