0

Please refer to this Plunk: http://plnkr.co/edit/5YCCdDSnpHKRU51R9vCL?p=preview

How do I filter based on the field that is displayed? Currently, the filtering is done on the complete user object.

For eg:

$scope.users1 = [
  {name: 'Dave', id: 24},
  {name: 'Tim', id: 22},
  {name: 'Laura', id: 20}
];

<user-info-card users="users" label="name">
</user-info-card>

If I display only the id, the filter works even if I type "Dave". I want it to work only on the fields displayed. Also want it to be generic. It could be a different object but the search term should look at the label field and filter accordingly

3
  • Look on custom filters and try to implement yours Commented Nov 10, 2015 at 15:32
  • Those Fields will always be the same? name and id? Commented Nov 10, 2015 at 15:35
  • Hi Ignacio. The requirement says that they might change. Commented Nov 10, 2015 at 15:49

2 Answers 2

1

Added a function which will sort based on label, hope if suffice.

http://plnkr.co/edit/iCepagIKA1OFDjBnB35I?p=preview

$scope.searchTerm = function(item) {        
  if(item[$scope.label].toString().
    startsWith($scope.searchCondition1)){
          return true;
        }
        else{
          return false;
        }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use strict filter to do this stuff.

test.html

<div>
 <input placeholder="Search for a name..." ng-model="searchTerm" ng-change="onChange()">
  <div ng-repeat="user in users | filter:user:strict">
    ...
  </div>
</div>

During text box key change event, search term is converted as object field value.

script.js

$scope.onChange = function () {
      var user = {};
      user[$scope.label] = $scope.searchTerm;
      $scope.user = user;
     };

Check this link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.