隐藏在使用表的ng-repeat中显示自定义过滤器

时间:2017-01-01 03:00:16

标签: javascript angularjs algorithm

如何使用自定义过滤器在视图中隐藏日期2-10-2017特殊彩色铅笔的库存列?

彩色铅笔特别的库存取决于彩色铅笔的库存,在这种情况下,它位于某处。

$scope.stationary = [{
    "name": "Pen",
    "data": [{
      "date": "1-10-2017",
      "inventory": 25
    }, {
      "date": "2-10-2017",
      "inventory": 21
    }]
  }, {
    "name": "Color Pencil",
    "data": [{
      "date": "1-10-2017",
      "inventory": 3
    }, {
      "date": "2-10-2017",
      "inventory": 0
    }]
  }, {
    "name": "Color Pencil Special",
    "data": [{
      "date": "1-10-2017",
      "inventory": 2
    }, {
      "date": "2-10-2017",
      "inventory": 1 // hide this in the view since inventory of color pencil is zero
    }]
  }]

http://jsfiddle.net/2krrda9k/

我坚持了好几个小时。这很难......

1 个答案:

答案 0 :(得分:1)

您可以使用此自定义过滤器

myApp.filter('match', function() {
  return function(items) {
    debugger;
    var filtered = [];
    var condition = false;
    angular.forEach(items, function(item) {
      angular.forEach(item.data, function(vals) {
        if (vals.inventory == 0 && condition == false) {
          condition = true;
        } else if (vals.inventory != 0 && condition === false) {
          if (filtered.indexOf(item) == '-1') {
            filtered.push(item);
          }

        }
      });
    });
    return filtered;
  };
});

<强> DEMO