过滤3列angularjs中的2个

时间:2016-10-14 07:32:01

标签: javascript html angularjs

<table> 
  <tr>
      <th>Primary Key</th>
      <th>descriptionShort</th>
      <th>descriptionLong</th>
 </tr>
 <tr ng-repeat="terReason in data | filter :({descriptionLong:searchTerm}||{descriptionShort:searchTerm}  )">
      <td>terReason.primaryKey</td>
      <td>terReason.descriptionShort</td>
      <td>terReason.descriptionLong</td>
</tr>
</table>

我有这张桌子。我怎样才能使过滤器仅在descriptionLong或descriptionShort上工作,而不在primaryKey中工作。 此时过滤器仅适用于descriptionLong。     DATA sceernshot

2 个答案:

答案 0 :(得分:1)

我想这就是你想要的:

<input ng-model="searchTerm">
<table> 
  <tr>
      <th>Primary Key</th>
      <th>descriptionShort</th>
      <th>descriptionLong</th>
   </tr>
   <tr ng-repeat="terReason in data | filter : myFilter">
        <td>{{ terReason.primaryKey }}</td>
        <td>{{ terReason.descriptionShort }}</td>
        <td>{{ terReason.descriptionLong }}</td>
  </tr>
</table>

JS:

$scope.data = [
{
  primaryKey: 0,
  descriptionShort: 'descriptionShort ZERO',
  descriptionLong: 'descriptionLong descriptionLong descriptionLong ZERO'
},
{
  primaryKey: 1,
  descriptionShort: 'descriptionShort ONE',
  descriptionLong: 'descriptionLong descriptionLong descriptionLong ONE'
},
{
  primaryKey: 2,
  descriptionShort: 'descriptionShort TWO',
  descriptionLong: 'descriptionLong descriptionLong descriptionLong TWO'
},
{
  primaryKey: 3,
  descriptionShort: 'descriptionShort THREE',
  descriptionLong: 'descriptionLong descriptionLong descriptionLong THREE'
}  
];

$scope.myFilter = function myFilter (value) {
  return !$scope.searchTerm || Object.keys(value).some(function (key) {
    return key !== 'primaryKey' && value[key].search($scope.searchTerm) !== -1;
  });
}

pluner:http://plnkr.co/edit/VMcs064WWbGraULyXQTU?p=preview 尝试按 0 1 进行搜索 - 没有显示任何内容,但不是按主键搜索。

答案 1 :(得分:0)

 <tr ng-repeat="terReason in data | filter :filter1 | filter :filter2  ">
      <td>terReason.primaryKey</td>
      <td>terReason.descriptionShort</td>
      <td>terReason.descriptionLong</td>
</tr>

为什么不使用多个过滤器?