AngularJS filter for multiple strings

后端 未结 6 937
时光取名叫无心
时光取名叫无心 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:10

    You can do a multiple word search on a object as follows:

    .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++){
                    angular.forEach(input[x], function(item){
                        if(item.toLowerCase().indexOf(searchTextSplit[y]) !== -1){
                            count++;
                        }
                    });
    
                }
                if(count == searchTextSplit.length){
                     returnArray.push(input[x]);   
                }
            }
            return returnArray;
        }
    });    
    

    Working demo in js fiddle

提交回复
热议问题