ng-repeat仅按特定属性按对象数组过滤

时间:2015-03-06 19:05:33

标签: angularjs search angularjs-ng-repeat angular-filters

我有一系列对象,我正在重复。我有一个搜索框来过滤结果。每个对象包含各种属性,其中一些属性与搜索过滤器无关。我想将过滤器限制为只有2个属性。在下面的示例中,我想忽略性别'属性,仅按'名称'过滤或者'电话'。



<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example52-production</title>
  

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
  

  
</head>
<body ng-app="">
    <div ng-init="friends = [{name:'John', phone:'555-1276', gender: 'male'},
                           {name:'Mary', phone:'800-BIG-MARY', gender: 'female'},
                           {name:'Mike', phone:'555-4321', gender: 'male'},
                           {name:'Adam', phone:'555-5678', gender: 'male'},
                           {name:'Julie', phone:'555-8765', gender: 'female'},
                           {name:'Juliette', phone:'555-5678', gender: 'female'}]"></div>

  Search: <input ng-model="searchText">
  <table id="searchTextResults">
    <tr><th>Name</th><th>Phone</th></tr>
    <tr ng-repeat="friend in friends | filter:searchText">
      <td>{{friend.name}}</td>
      <td>{{friend.phone}}</td>
    </tr>
  </table>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您需要自定义过滤器: -

<table id="searchTextResults">
    <tr><th>Name</th><th>Phone</th></tr>
    <tr ng-repeat="friend in friends | filter:search">
      <td>{{friend.name}}</td>
      <td>{{friend.phone}}</td>
    </tr>
  </table>

  $scope.search = function (row) {
        return (angular.lowercase(row.name).indexOf($scope.searchText || '') !== -1 || angular.lowercase(row.phone).indexOf($scope.searchText || '') !== -1);
    };

Plunker