Nested filters in angular for hierarchical data model

angularjs, javascript

Solution

do u mean this?

<div ng-repeat="item in items  | filter1 | filter2"> 

more info here http://docs.angularjs.org/guide/dev_guide.templates.filters.using_filters

EDIT:

now i got that u didnt wrote filters but scope function, u need something like this

myModule.filter('iif', function () {
    return function (input, trueValue, falseValue) {
        return input ? trueValue : falseValue;
    };
});

my use is class="{{ catalogItem.IS_FINAL | iif : 'IsFinalCatalogItem' : '' }}"

should look like this

myModule.filter('prodClassIsALine', function() {
    return function(item) {
        return item.level_type=='line';
    };
});

p.s. in angular 1.2 they added the standard iif syntax

Problem

I have an hierarchical data model with lines of products and then sublines, and then sublines of sublines etc. What I am trying to do is isolate only the sublines that are directly descendants (sons not grandchildren) of a particular line or subline. Here is my existing data model: ``` items:[ { "_id": "1", "description": "sth1", "name": "smname1", "level_type": "line", "ancestor": "", "descendant": "smname2" } }, { "_id": "2", "description": "sth2", "name": "smname1", "level_type": "subline", "ancestor": "smname1", "descendant": "" } }, ] ``` Also for the example above another thing I'm trying to accomplish is to get the children of all the product lines. What I have tried but is not working so far is: Controller ``` $scope.prodClassIsALine = function(item) { return item.level_type=='line'; }; $scope.prodClassIsASubLineof = function(item) { return item.ancestor==$scope.prodClassIsALine.name; }; ``` Tragic proposal just to show you that I need all children of all lines i.e. all items with ancestor names of items that are lines. Html ``` <div ng-repeat="item in items | filter:prodClassIsALine:prodClassIsASubLineof"> <p>{[{item.name}]}</p> </div> ``` Is this the way we are nesting filters in AngularJS? It seems that filters are iterating over the list that are given as attribute but further than that I can't understand in detail how they work. Please help. Solution In script.js ``` //product is my ng-module //filter to get all product classes that are lines product.filter('prodClassIsALine', function() { return function(input) { var out = []; for (var i = 0; i < input.length; i++) { if (input[i].level_type=='line') { out.push(input[i]) }; }; return out; }; }); //filter to get all children of product classes that are lines product.filter('prodClassLineChild', function() { return function(input) { var out = []; var out2 = []; for (var i = 0; i < input.length; i++) { if (input[i].level_type=='line') { out2.push(input[i]) }; }; for (var i = 0; i < out2.length; i++) { for (var j = 0; j < input.length; j++) { if (input[j].ancestor==out2[i].name) { out.push(input[j]) }; }; }; return out; }; }); ``` Html ``` <div ng-repeat="item in items | prodClassIsALine"> <!-- or <div ng-repeat="item in items | prodClassLineChild"--> <p>{[{item.name}]}</p> </div> ```

Original source