【问题标题】:angular ng-repeat with multiple filter options具有多个过滤器选项的角度 ng-repeat
【发布时间】:2013-08-13 19:49:24
【问题描述】:

我有这个 JSON 数据:

{
  "doorTypes": [
  {
    "name": "Flat Panel",
    "image": "doors/flatpanel.png",
    "type": "Wood"
  },
  {
    "name": "Raised Panel",
    "image": "doors/raisedpanel.png",
    "type": "Wood"
  },
  {
    "name": "Slab Door",
    "image": "doors/slabdoor.png",
    "type": "Wood"
  }],
  "woods": [
  {
    "name": "Alder",
    "image": "species/alder.png",
    "weights":[
    {
      "name": "Flat Panel",
      "weight": 1.19
    },
    {
      "name": "Raised Panel",
      "weight": 1.76
    },
    {
      "name": "Slab Door",
      "weight": 1.97
    }]
  },
  {
    "name": "Ash",
    "image": "species/ash.png",
    "weights":[
    {
      "name": "Flat Panel",
      "weight": 1.7
    }]
  },
  {
    "name": "Bamboo",
    "image": "species/bamboo.png",
    "weights":[
    {
      "name": "Slab Door",
      "weight": 2.7
    }]
  },
  {
    "name": "Beech",
    "image": "species/beech.png",
    "weights":[
    {
      "name": "Raised Panel",
      "weight": 2.27
    },
    {
      "name": "Slab Door",
      "weight": 2.54
    }]
  }]
}

根据您选择的doorType,我想过滤wood 类型。例如,如果您选择Raised Panel,我只希望显示重量为Raised Panel 的树林。所以在这种情况下,Alder and Beech 会显示,而不是 AshBamboo

现在我严格使用 ng-repeat,它显示所有木材类型。我查看了 ng-filter 的文档,但我不确定如何在 weights 对象具有多个属性的情况下应用它。

我当前的 ng-repeat:

<ul class="touchcarousel-container">
  <li class="touchcarousel-item" ng-repeat="obj in currentObject">
    <div class="img-select" ng-click="setActive(this)" ng-class="{itemSelected : isActive(this)}">
      <div align="center">
        <img ng-src="resources/images/aventos/{{obj.image}}" />
      </div>
      <div class="img-title">{{obj.name}}</div>
    </div>
  </li>
</ul>

如果这样做更有意义,我也愿意更改我的 JSON。

编辑

这是我的解决方案:

<li class="touchcarousel-item" ng-repeat="obj in currentObject" ng-show="woodTypes(obj)">

然后:

$scope.woodTypes = function(obj)
{
    var shown = false;
    for (var i = 0; i < obj.weights.length; i++)
    {
        if (obj.weights[i].name == $scope.cabinetDetails.door.name)
        {
            shown = true;
            break;
        }
    }
    return shown;
}

【问题讨论】:

标签: javascript angularjs angularjs-ng-repeat


【解决方案1】:

尝试编写自定义过滤器,并使用类似这样的某种组合。

<select ng-options="foo.blah for foo in foos" ng-model="selection"></select>
<li ng-repeat="obj in objects|filter:selection|filterFunction">{{obj}}</li>

.filter('filterFunction', function() {

  return function (objects) {

    var filter_objects = [];

    for (var i = 0; i < objects.length; i++) {
      for (var j = 0; j < objects[i].weights.length; j++) {
        if (objects[i].weights[j].name === "Raised Panel") {
          filter_objects.push(objects[i]);
        }
      }
    }

    return filter_objects;

  }
});

【讨论】:

  • 我想出了(我认为是)一个更简单的解决方案,请参阅我上面的编辑。我尝试了与您的上述解决方案非常相似的方法,但我无法使其正常工作。+1 但是为了您的支持
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-21
  • 2014-10-22
  • 2015-09-15
  • 2015-05-26
相关资源
最近更新 更多