2

I have an array of employees that look like

$scope.Arr = [
           {'name':'John','title':'CEO','Age':'32'}, 
           {'name':'Jill', 'title':'Sales Executive', 'Age':'44'},
           {'name':'Alex','title':'Developer','Age':24}
          ];

I use ng-repeat to render this array in html and am looking to make the filter work such that the same input control's value can be used to filter employees based on both the name as well as title.

<input type='text' data-ng-model='search.name' />

<div>
  <span data-ng-repeat="emp in Arr | filter:search">
  <p> {{emp.name}} </p>
  <p> {{emp.title}} </p>
  </span>
</div>

The above code only lets me filter on basis of employee name

6
  • You wanted to filter items based on what is given in the input box am I right? (not just name attribute) Commented Jun 12, 2015 at 5:55
  • Change it to filter:search.name. Note that it will compare the model value to all properties, not just name and title Commented Jun 12, 2015 at 5:58
  • 1
    @Swayam here is plunker plnkr.co/edit/Fni3pDdOvYA8B7IpRCzD?p=preview. Simple use search.name in filter. Commented Jun 12, 2015 at 6:01
  • @squiroid @phil I am trying to filter emp based on both name and title with the search model. search instead of search.name works just fine Commented Jun 12, 2015 at 6:06
  • Can you please elaborate by some example . Commented Jun 12, 2015 at 6:06

1 Answer 1

2

You can use filter to search based on both name and title.

The default angularjs filter on the expression "emp in Arr | filter: {name: $select.search, title: $select.search}" performs an AND between "name: $select.search" and "title: $select.search"

Write a custom filter to perform OR operation

var app = angular.module('myApp', []);

app.filter('customFilter', function() {
    return function(names, title) {
        var output = [];

        if(angular.isArray(names)) {
            names.forEach(function(item) {
                var nameMatch = false;

                var keys = Object.keys(title);
                for(var i=0; i<keys.length; ++i) {
                    var t = keys[i];
                    var text = title[t].toLowerCase();
                    if(item[t].toString().toLowerCase().indexOf(text) !== -1) {
                        nameMatch = true;
                        break;
                    }
                }

                if(nameMatch) {
                    output.push(item);
                }
             });
          }
          else {
              output = names;
       }

       return output;
   };
});

Basically, this code lets you filter based on both name and title. Hope this helps.

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

1 Comment

I've made a plunker -> plnkr.co/edit/VzqvgYCesmsNRMWfEakk?p=preview .... I hope this is what you require. In the select box, you can search based on "name" and "title". 'demo.js' has the "customFilter" defined

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.