Jenny Mok
Jenny Mok

Reputation: 2804

hide show custom filter in ng-repeat that uses table

How can I use custom filter to hide inventory column of color pencil special on date 2-10-2017 in the view?

color pencil special's inventory is depends on color pencil's inventory which in this case it's located somewhere.

$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/

I have stuck for this for hours. This is tough..

Upvotes: 3

Views: 84

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222702

You can use this custom filter,

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

Upvotes: 1

Related Questions