过滤嵌套 ng-repeat 中的复杂对象



我想过滤嵌套ng-repeat中的对象。

.HTML:

<div ng-controller="MyController">
<input type="text" ng-model="selectedCityId" />
<ul>
    <li ng-repeat="shop in shops">
      <p ng-repeat = "locations in shop.locations | filter:search" style="display: block">
          City id: {{ locations.city_id }}
          <span style="padding-left: 20px; display: block;" ng-repeat="detail in locations.details | filter:item">Pin code: {{detail.pin}}</span>
      </p>    
    </li>
</ul>

控制器:

var myApp = angular.module('myApp', []);
myApp.controller('MyController', function ($scope) {
    $scope.search = function (location) {
        if ($scope.selectedCityId === undefined || $scope.selectedCityId.length === 0) {
            return true;
        }
           if (location.city_id === parseInt($scope.selectedCityId)) {
               return true;
            }
    };
    $scope.item = function (detail) {
        if ($scope.selectedCityId === undefined || $scope.selectedCityId.length === 0) {
            return true;
        }
        if (detail.pin == parseInt($scope.selectedCityId)) {
            return true;
        }
    };
    $scope.shops =
    [
       {
          "category_id":2,
          "locations":[
             {
                "city_id":368,
                "details": [{
                    "pin": 627718,
                  "state": 'MH'
                }]
             }
          ]
       },
       {
          "name":"xxx",
          "category_id":1,
          "locations":[
             {
                "city_id":400,
                "region_id":4,
                "details": [{
                    "pin": 627009,
                  "state": 'MH'
                },{
                    "pin": 129818,
                    "state": 'QA'
                }]
             },
          ]
       },
    ];
});

这是小提琴:

http://jsfiddle.net/suCWn/210/

我想在 ng-repeat 中使用多个过滤器。

示例:每当用户在输入框中输入 ID 时。该列表应根据城市 ID 或密码进行过滤。如果用户输入"129818",它应该显示129818的PIN码结果及其父城市ID同样,如果用户输入 400,列表应筛选并显示包含 400 的 cityID 结果及其子 PIN 码。

编辑:

更新代码 http://codepen.io/chiragshah_mb/pen/aZorMe?editors=1010]

首先,不得使用匹配的详细信息筛选位置。在search过滤器中使用类似以下内容:

$scope.search = function (location) {
    var id = parseInt($scope.selectedCityId);
    return isNaN(id) || location.city_id === id || 
           location.details.some(function(d) { return d.pin === id });
};

要显示按 cityID 过滤的详细信息,您必须找到父location并检查它是否已过滤。

$scope.item = function (detail) {
    var id = parseInt($scope.selectedCityId);
    return isNaN(id) || detail.pin === id || locationMatches(detail, id);
};
function locationMatches(detail, id) {
    var location = locationByDetail(detail);
    return location && location.city_id === id;
}
function locationByDetail(detail) {
    var shops = $scope.shops;
    for(var iS = 0, eS = shops.length; iS != eS; iS++) {
      for(var iL = 0, eL = shops[iS].locations.length; iL != eL; iL++) {
        if (shops[iS].locations[iL].details.indexOf(detail) >= 0) {
          return shops[iS].locations[iL];
        }
      }
    }
}

编辑 另一个更灵活的解决方案是从ngRepeats中删除所有过滤器,并以您在ngChange上调用的搜索文本的方法进行过滤。以下是此方法的基本结构。

myApp.controller('MyController', function($scope, $http) { 
  var defaultMenu = [];
  $scope.currentMenu = [];
  $scope.searchText = '';
  $http.get(/*...*/).then(function (menu) { defaultMenu = menu; } );
  $scope.onSearch = function() {
    if (!$scope.searchText) {
      $scope.currentMenu = defaultMenu  ;
    }
    else {
      // do your special filter logic here...
    }
  };
});

和模板:

<input type="text" ng-model="searchText" ng-change="onSearch()" />
<ul>
    <li ng-repeat="category in currentMenu">
      ...   
    </li>
</ul>

我已经更新了你的过滤器。问题在于您的search过滤器中,您只检查city_id,您应该做的是:

  1. 检查键入的 ID 是否city_id
  2. 检查键入的 ID 是否是给定位置的子详细信息的pid

item过滤器类似:

  1. 检查键入的 ID 是否是正在筛选的详细信息的pid
  2. 检查键入的 ID 是否是传入详细信息的父位置的city_id

这是一个工作 jsFiddle。我希望这有所帮助。

通过简单地修改JSON以包含子项的city_id,这样您就不需要遍历它来获取父级的city_id,解决方案非常简单:

var myApp = angular.module('myApp', []);
myApp.controller('MyController', function ($scope) {
    $scope.search = function (location) {
        if (!$scope.selectedCityId)
            return true;
        //if user's input is contained within a city's id
        if (location.city_id.toString().indexOf($scope.selectedCityId) > -1)
            return true;
        for (var i = 0; i < location.details.length; i++)
            //if user's input is contained within a city's pin
            if (location.details[i].pin.toString().indexOf($scope.selectedCityId) > -1)
                return true;
    };
    $scope.item = function (detail) {
        if (!$scope.selectedCityId)
            return true;
        //if user's input is contained within a city's id
        if (detail.city_id.toString().indexOf($scope.selectedCityId) > -1)
            return true;
        //if user's input is contained within a city's pin
        if (detail.pin.toString().indexOf($scope.selectedCityId) > -1)
            return true;
    };

修改后的 JSON

$scope.shops=[{"category_id":2,"locations":[{"city_id":368,"details":[{"city_id":368,"pin":627718,"state":'MH'}]}]},{"name":"xxx","category_id":1,"locations":[{"city_id":400,"region_id":4,"details":[{"city_id":400,"pin":627009,"state":'MH'},{"city_id":400,"pin":129818,"state":'QA'}]},]},];});

如果无法直接修改JSON,则可以在此 $scope.shops = ...json... 语句之后直接在此控制器中像这样修改它:

for(var i=0; i<$scope.shops.length; i++)
    for(var j=0, cat=$scope.shops[i]; j<cat.locations.length; j++)
        for(var k=0, loc=cat.locations[j]; k<loc.details.length; k++)
            loc.details[k].city_id=loc.city_id;

工作小提琴:http://jsfiddle.net/87e314a0/

我试图使解决方案更容易理解:

索引.html :

<div ng-controller="MyController">
    <input type="text" ng-model="search.city_id" />
    <ul>
        <li ng-repeat="shop in shops">
          <p ng-repeat = "locations in shop.locations | filter:search.city_id" style="display: block">
              City id: {{ locations.city_id }}
              <span style="padding-left: 20px; display: block;" ng-repeat="detail in locations.details | filter:item">Pin code: {{detail.pin}}</span>
          </p>    
        </li>
    </ul>
</div>

应用程序.js :

var myApp = angular.module('myApp', []);
myApp.controller('MyController', function ($scope) {
    $scope.shops =
[
   {
      "category_id":2,
      "locations":[
         {
            "city_id":368,
            "details": [{
                "pin": 627718,
              "state": 'MH'
            }]
         }
      ]
   },
   {
      "name":"xxx",
      "category_id":1,
      "locations":[
         {
            "city_id":400,
            "region_id":4,
            "details": [{
                "pin": 627009,
              "state": 'MH'
            },{
                "pin": 129818,
                            "state": 'QA'
            }]
         },
      ]
   },
];

});

这是小提琴:我的解决方案

最新更新