exact filter in angular

前端 未结 6 1361
天命终不由人
天命终不由人 2020-11-29 12:10

In Angular, is there a way to modify the filter such that it only returns exact matches?

Example:

var words = [
    {   title: \"ball\"   },
    {            


        
相关标签:
6条回答
  • 2020-11-29 12:38

    I would create a new filter. Is this what you want?

    HTML

    <div ng-controller="MyCtrl">
         {{words | exactMatch:'all'}} !
    </div>
    

    JavaScript

    var myApp = angular.module('myApp',[]);
    
    myApp.filter('exactMatch', function() {
        return function(words, pattern) {
            var result = [];
            words.forEach(function (word) {
                if (word.title === pattern) {
                    result.push(word);
                }
            });                
            return result;
        }
    });
    
    function MyCtrl($scope) {
        $scope.words = [
            {title: "ball", other: 1},
            {title: "wall", other: 2},
            {title: "all", other: 3},
            {title: "alloy", other: 4},
            {title: "all", other: 5},
        ];
    }
    

    JsFiddle: jsfiddle
    More information about custom filters: filters, creating custom filters and using filters

    If you want use filter in Javascript instead of html you should look here: jsfiddle

    0 讨论(0)
  • 2020-11-29 12:41

    Try this :

    var words = [
        {   title: "ball"   },
        {   title: "wall"   },
        {   title: "all"    },
        {   title: "alloy"  }
    ];
    
    var wordsFiltered = filter('filter')
    ( 
        words, 
        { 
            'title': 'all'
        },
        true
    );
    
    0 讨论(0)
  • 2020-11-29 12:42

    You can use Regex to achieve a simple implementation:

    <input ng-model="query" />
    <tr ng-repeat="word in words | filter: myFilter">
    

    In the controller:

    $scope.myFilter = function (word) {
        if ($scope.query === '') return true;
        var reg = RegExp("^" + $scope.query + "$");
        return reg.test(word.title);
    };
    
    0 讨论(0)
  • 2020-11-29 12:54

    UPDATE

    Starting from AngularJS v.1.1.3 the exact filtering is provided natively:

    Find words that exactly match title: 
    <input ng-model="match.title" />
    <br>
    and exactly match type: 
    <input ng-model="match.type" />
    <hr>
    <table>
      <tr ng-repeat="word in words | filter:match:true">
       <td>{{word.title}}</td> 
      </tr>
    </table>
    

    Plunker


    Your question implies that you would want to match against multiple object properties so here's a filter that does that:

    app.controller('AppController',
        [
          '$scope',
          function($scope) {
            $scope.match = {};
            $scope.words = [
              { title: "ball", type: 'object' },
              { title: "wall", type: 'object' },
              { title: "all", type: 'word' },
              { title: "alloy", type: 'material' }
            ];
            
          }
        ]
      );
      
    app.filter('exact', function(){
      return function(items, match){
        var matching = [], matches, falsely = true;
        
        // Return the items unchanged if all filtering attributes are falsy
        angular.forEach(match, function(value, key){
          falsely = falsely && !value;
        });
        if(falsely){
          return items;
        }
        
        angular.forEach(items, function(item){ // e.g. { title: "ball" }
          matches = true;
          angular.forEach(match, function(value, key){ // e.g. 'all', 'title'
            if(!!value){ // do not compare if value is empty
              matches = matches && (item[key] === value);  
            }
          });
          if(matches){
            matching.push(item);  
          }
        });
        return matching;
      }
    });
    
    <body ng-controller="AppController">
    
      Find words that exactly match title: 
      <input ng-model="match.title" />
      <br>
      and exactly match type: 
      <input ng-model="match.type" />
      <hr>
      <table>
        <tr ng-repeat="word in words | exact:match">
         <td>{{word.title}}</td> 
        </tr>
      </table>  
    </body>
    

    PLUNKER

    0 讨论(0)
  • 2020-11-29 12:56

    I created my own filter.

    <select 
            ng-model="selection.neType" 
            ng-options="option.NE_TYPE_ID as option.NAME for option in networkElementTypes">
                <option value="">Todos</option>
        </select>
    
    <select 
            ng-model="selection.neTypeVendor" 
            ng-options="option.VENDOR_TYPE_ID as option.NAME for option in networkElementTypeVendors | exactMatch: {FK_NE_TYPE_ID: selection.neType}">
                    <option value="">All</option>
                </select>
    
        app.filter('exactMatch', function() {
            return function(elements, pattern) {    
    
                var result = [];        
                var fieldSearch = Object.keys(pattern)[0];
    
                elements.forEach(function (element) {  
    
                    if (element[fieldSearch] == pattern[fieldSearch]) {
                        result.push(element);
                    }
                });          
    
                return result;
            }
        });
    
    0 讨论(0)
  • 2020-11-29 13:05

    angular-filter is a useful library of filters.

    One of their filters is the where filter that does an exact match.

    0 讨论(0)
提交回复
热议问题