Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have the following data:

organizations:[{
                name: "foo",
                contacts: [{
                             firstName = "John",
                             lastName = "Doe",
                             email = "[email protected]"
                          },...]
               },...]

Then I have a table where I list all the organizations and I want to know if it is possible to filter the rows in the table according to a firstName filter or a email filter.

For example I have this code:

<input type="text" id="name" ng-model="search.name">

<tr ng-repeat="organization in organizations | filter:search">
    <td>{{organization.name}}</td>
    <td>{{client.contacts[0].firstName}} {{ client.contacts[0].lastName }}</td>
    <td>{{client.contacts[0].email}}</td>
</tr>

It works filtering only by the 'name' field. I tried something like this:

<input type="text" id="firstName" ng-model="search.contacts">

But it searches in all fields of the objects in the contacts array, but I want to specifically search by firstName. How can I do?

share|improve this question
    
did u try <input type="text" id="firstName" ng-model="search.contacts.firstName"> – Ajay Beniwal Jul 19 '13 at 11:32
    
I tried it but doesn't work I think because contacts is an array. – Ciccio Jul 19 '13 at 11:39
up vote 0 down vote accepted

Use a filter function like filter:filterBy.

Example (relies on underscore.js):

scope.filterBy = function(row) {
    return !!_.where(row.contacts, {firstName:scope.search.name}).length;
}
share|improve this answer
2  
One thing to note about filters is that they need to be fast. If for any reason you see that filters are slowing you down try $watch-ing the filter properties then manually filtering your array and in the template ng-repeat over the filtered array – Liviu T. Jul 19 '13 at 12:54
    
Indeed @LiviuT. For a small number of rows, a filter should be fine. – DanS Jul 19 '13 at 12:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.