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

前端 未结 13 2383
北恋
北恋 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:47

    I solved this simply:

    <div ng-repeat="Object in List | filter: (FilterObj.FilterProperty1 ? {'ObjectProperty1': FilterObj.FilterProperty1} : '') | filter:(FilterObj.FilterProperty2 ? {'ObjectProperty2': FilterObj.FilterProperty2} : '')">
    
    0 讨论(0)
  • 2020-11-22 17:48

    If you're open to use third party libraries,'Angular Filters' with a nice collection of filters may be useful:

    https://github.com/a8m/angular-filter#filterby

    collection | filterBy: [prop, nested.prop, etc..]: search
    
    0 讨论(0)
  • 2020-11-22 17:50

    Here is the plunker

    New plunker with cleaner code & where both the query and search list items are case insensitive

    Main idea is create a filter function to achieve this purpose.

    From official doc

    function: A predicate function can be used to write arbitrary filters. The function is called for each element of array. The final result is an array of those elements that the predicate returned true for.

    <input ng-model="query">
    
    <tr ng-repeat="smartphone in smartphones | filter: search "> 
    

    $scope.search = function(item) {
        if (!$scope.query || (item.brand.toLowerCase().indexOf($scope.query) != -1) || (item.model.toLowerCase().indexOf($scope.query.toLowerCase()) != -1) ){
            return true;
        }
        return false;
    };
    

    Update

    Some people might have a concern on performance in real world, which is correct.

    In real world, we probably would do this kinda filter from controller.

    Here is the detail post showing how to do it.

    in short, we add ng-change to input for monitoring new search change

    and then trigger filter function.

    0 讨论(0)
  • 2020-11-22 17:52

    Filter can be a JavaScript object with fields and you can have expression as:

    ng-repeat= 'item in list | filter :{property:value}'
    
    0 讨论(0)
  • 2020-11-22 17:56

    Hope this answer will help,Multiple Value Filter

    And working example in this fiddle

    arrayOfObjectswithKeys | filterMultiple:{key1:['value1','value2','value3',...etc],key2:'value4',key3:[value5,value6,...etc]}
    
    0 讨论(0)
  • 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);
        }
    };
    
    0 讨论(0)
提交回复
热议问题