AngularJS filter for multiple strings

后端 未结 6 927
时光取名叫无心
时光取名叫无心 2020-12-14 23:32

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

6条回答
  •  情歌与酒
    2020-12-15 00:18

    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.

提交回复
热议问题