2013-08-29 4 views
7

저는 이와 같이 각도가 중첩 된 객체가 있습니다. 가 난 단지 부모 요소를 보여주는거야 중첩 특성Angularjs 중첩 된 객체를 필터링합니다.

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

을 위해 그것을 필터링하지만 같은 그것의 모두 필터링 할 방법을 방법이 :

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 
    ] 
] 

답변

8

예, 당신이 할 수있는, 나는 경우 너의 모범을 제대로 이해했다.

컬렉션의 크기에 따라 반복되는 컬렉션을 ng-repeat에 계산하여 모델이 변경 될 때 필터가 끊임없이 작업을 수행하지 않는 것이 좋습니다. 내가 제대로 이해하면

http://jsfiddle.net/suCWn/

은 기본적으로 당신은 같은 것을 할 :

$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; 
}; 
23

또한 업데이트이 (버전 1.2.13+)

<li ng-repeat="shop in shops | filter: { locations: [{ city_id: search.locations.city_id }] }"> 
+1

JSfiddle가 잘 작동되는 전체 단어를 일치하지 않아도 필터링을 시작하지만, 기본적으로 결과 만 입력 후 표시되지 않습니다. http://jsfiddle.net/suCWn/12/ – zajca

+1

약간 수정 해 보았습니다. [link] (http://jsfiddle.net/suCWn/15/) – martinoss

+2

@zajca 컨트롤러에 모델 값을 지정하여 문제를 해결할 수 있습니다. :'$ scope.selectedCityId = '''. 입력을 수동으로 변경하기 전에 모든 항목을로드하는 효과가 있습니다. –

0

처럼 필터링 할 수 있습니다 "Words Like Jared"는 정규식을 사용하여 searchterm이 포함되어 있는지 확인합니다. 이 방법은 1 개 번호를 입력 할 때

angular.forEach(shop.locations, function (location) {   
     if (checknum(location.city_id)) { 
      found = true; 
     } 
    }); 

    function checknum(num){ 
     var regx = new RegExp($scope.selectedCityId); 
     return regx.test(num); 
    }; 
관련 문제