Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have an array with objects looking like that:

record in recordlist {
    date : "02/12/2014"
    time : "00.02.01"
    car : "369"
    pax: [
        {
        name : "Ben"
        chosen : true
        },
        {
        name : "Eric"
        chosen : true
        }
    ]
}

So far, when I list using ng-repeat, I'm able to filter by object (record) property.

Filter:

<input class="form-control" placeholder="Time" ng-model="search.time">

ng-repeat:

<div ng-repeat="record in filteredRecords = (recordlist | filter: search)">

The problem comes when I want to filter the nested array (pax). I've tried this, but so far no luck:

<input ng-model="search.pax.name"> // filter by name property

<input ng-model="search.pax.length"> // filter by array length

Any tips?

share|improve this question

You will probably need to use a custom filter:

Something like this should do it:

<input ng-model="searchName">

<div ng-repeat="record in recordlist | filter: filterByNested">

And in your controller:

$scope.filterByNested = function (record) {
    return record.pax.reduce(function (prev, curr, idx, array) {
        return prev || curr.name == $scope.searchName;
    }, false);
};

Basically you define a custom filter that returns true if the element you are searching is in the nested array.

It would be trivial to change the filter function to work based on length too.

share|improve this answer
    
Considering my example, in your function would be function(record) {return record.pax.reduce... ? – Eric Mitjans Dec 3 '14 at 6:51
1  
i've edited the answer for a better understanding, hope now it makes more sense. – pedromarce Dec 3 '14 at 7:00
    
I'll give it a try later! Thanks!! – Eric Mitjans Dec 3 '14 at 7:14
    
So far is not working, the filter filters out all results (nothing listed) : / – Eric Mitjans Dec 3 '14 at 10:45
1  
Check jsfiddle working jsfiddle.net/tgddnc2p/1 – pedromarce Dec 3 '14 at 11:39

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.