Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 Singh 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
add comment

1 Answer

up vote 1 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
1  
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
add comment

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.