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 in angular nested object like this. is there way how to filter it for nested property

<li ng-repeat="shop in shops | filter:search">
search.locations.city_id = 22

I'm showing only parent element but want to filter by both of it, like:

search = 
  category_id: 2
  locations:
    city_id: 368

[
 name: "xxx"
 category_id: 1
 locations: [
   city_id: 368
   region_id: 4
  ,
   city_id: 368
   region_id: 4
  ,
   city_id: 368
   region_id: 4
  ]
,
 name: "xxx"
 category_id: 2
 locations: [
   city_id: 30
   region_id: 4
  ,
   city_id: 22
   region_id: 2
  ]
]
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Yes, you can, if I understood your example properly.

Depending on the size of your collection it may be better to compute the collection you iterate over in ng-repeat so that the filter isn't doing it constantly as the model changes.

http://jsfiddle.net/suCWn/

Basically you do something like this, if I understood you correctly:

$scope.search = function (shop) {

    if ($scope.selectedCityId === undefined || $scope.selectedCityId.length === 0) {
        return true;
    }

    var found = false;
    angular.forEach(shop.locations, function (location) {          
        if (location.city_id === parseInt($scope.selectedCityId)) {
            found = true;
        }
    });

    return found;
};
share|improve this answer
 
that's it thank you :) –  zajca Aug 29 '13 at 8:38
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.