Filter by multiple columns with ng-repeat

前端 未结 8 1746
小鲜肉
小鲜肉 2020-11-30 12:16

I\'m wondering if there\'s an easy way in Angular to filter a table using ng-repeat on specific columns using or logic, rather than and

8条回答
  •  盖世英雄少女心
    2020-11-30 12:48

    It's not hard to create a custom filter which allows you to have as many arguments as you want. Below is an example of a filter with one and two arguments, but you can add as many as you need.

    Example JS:

    var app = angular.module('myApp',[]);
    app.filter('myTableFilter', function(){
      // Just add arguments to your HTML separated by :
      // And add them as parameters here, for example:
      // return function(dataArray, searchTerm, argumentTwo, argumentThree) {
      return function(dataArray, searchTerm) {
          // If no array is given, exit.
          if (!dataArray) {
              return;
          }
          // If no search term exists, return the array unfiltered.
          else if (!searchTerm) {
              return dataArray;
          }
          // Otherwise, continue.
          else {
               // Convert filter text to lower case.
               var term = searchTerm.toLowerCase();
               // Return the array and filter it by looking for any occurrences of the search term in each items id or name. 
               return dataArray.filter(function(item){
                  var termInId = item.id.toLowerCase().indexOf(term) > -1;
                  var termInName = item.name.toLowerCase().indexOf(term) > -1;
                  return termInId || termInName;
               });
          } 
      }    
    });
    

    Then in your HTML:

    
    

    Or if you want to use multiple arguments:

    
    

提交回复
热议问题