hide show custom filter in ng-repeat that uses table

Question

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..


Show source
| javascript   | angularjs   | algorithm   2017-01-01 04:01 1 Answers

Answers ( 1 )

  1. 2017-01-01 05:01

    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

◀ Go back