Endrit Demaj
Endrit Demaj

Reputation: 134

filter just 2 out of 3 columns 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>

I have this table. How can i make that the filter works only on descriptionLong or descriptionShort and not in primaryKey. At this time the filter works only on descriptionLong. DATA sceernshot

Upvotes: 0

Views: 79

Answers (2)

Fida
Fida

Reputation: 1358

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

Why not use multiple filters?

Upvotes: 0

Andriy
Andriy

Reputation: 15442

I suppose this is what you want:

<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 try to search by 0 or 1 - nothing displayed, it is not searching by primary key.

Upvotes: 1

Related Questions