2013-08-29 7 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

もこれが動作しているが、デフォルトの結果によってのみ入力後に表示されていないと一致する必要はありませんので、あなたは1つの番号を入力する際のフィルタリングを開始します。 http://jsfiddle.net/suCWn/12/ – zajca

+1

私はあなたのフィドルを少し修正しました:[link](http://jsfiddle.net/suCWn/15/) – martinoss

+2

@zajcaコントローラにモデルの値を代入することで回避できます: '$ scope.selectedCityId = '''これは、手動で入力を変更する前にすべての項目を読み込むという効果があります。 –

0

"Words Like Jared"は正規表現を使用してsearchtermが含まれているかどうかを確認します。この方法は、あなたが単語全体

JSfiddle

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); 
    }; 
関連する問題