Filtering by Multiple Specific Model Properties in AngularJS (in OR relationship)

前端 未结 13 2547
北恋
北恋 2020-11-22 17:08

Take a look at the example here: http://docs.angularjs.org/api/ng.filter:filter

You can search by any of the phone properties by using

13条回答
  •  难免孤独
    2020-11-22 17:57

    http://plnkr.co/edit/A2IG03FLYnEYMpZ5RxEm?p=preview

    Here is a case sensitive search that also separates your search into words to search in each model as well. Only when it finds a space will it try to split the query into an array and then search each word.

    It returns true if every word is at least in one of the models.

    $scope.songSearch = function (row) {
        var query = angular.lowercase($scope.query);
        if (query.indexOf(" ") > 0) {
            query_array = query.split(" ");
            search_result = false;
            for (x in query_array) {
                query = query_array[x];
                if (angular.lowercase(row.model1).indexOf(query || '') !== -1 || angular.lowercase(row.model2).indexOf(query || '') !== -1 || angular.lowercase(row.model3).indexOf(query || '') !== -1){
                    search_result = true;
                } else {
                    search_result = false;
                    break;
                }
            }
            return search_result;
        } else {
            return (angular.lowercase(row.model1).indexOf(query || '') !== -1 || angular.lowercase(row.model2).indexOf(query || '') !== -1 || angular.lowercase(row.model3).indexOf(query || '') !== -1);
        }
    };
    

提交回复
热议问题