1

Object is

StudentObj = {name : 'Me', Place : 'Bangalore',uniqueId :'233abc233' }

Html template

<ul>
  <li ng-repeat:'entry in StudentObj |filter:sText' >
         <span>{{entry.name}}</span>
         <span ng-bind='entry.place'></span> 
   </li>
</ul>

And search filter is

<input type='text' ng-model='sText'>

Problem :

if i search '233abc233' in text-filter the row is selecting , But this should not selected.

Only on name and place values , the Row should be selected"

Thanks in Advance

1 Answer 1

2

You could use a filter like this to match only two fields:

<li ng-repeat="entry in StudentObj | filter:{name: sText, Place: sText}">

For more variation of how to use to use filter, please see filter.

Edit: If you want an OR logic (should match if any key is match), you have to roll your own filter, for example,

In controller:

$scope.customFilter = function (searchText) {
  function comparator(a, b) {
    return (''+a).toLowerCase().indexOf((''+b).toLowerCase()) > -1;
  }

  var lookInKeys = ['name', 'Place'];

  return function (item) {
    if (!searchText) {
      return true; // no filter
    }

    for (var i = 0; i < lookInKeys.length; i++) {
      var key = lookInKeys[i];
      if (comparator(item[key], searchText)) {
        return true; // if any key is match, return true
      }
    }

    return false; // none of keys are match
  };
};

and then use it in ng-repeat like this:

<li ng-repeat="entry in StudentObj | filter:customFilter(sText)">

Example plunker: http://plnkr.co/edit/Mlca2gXvXNVAXEsNbpCI?p=preview

Sign up to request clarification or add additional context in comments.

3 Comments

Issue is still exists as the filter will check for both name and place properties and if either one of the property returns false the logic is failing.
So, you want an OR logic? If either name or Place is match, the row should be shown, right?
Yes your correct. if i type "Me" it should search either in name or place , if name is returns true then this logic should return true ie row selected else false

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.