4

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>

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

<span ng-repeat="item in items | filter:prodClassIsALine | filter:prodClassIsASubLineof"> does not work however.
Thanks for your help bresleveloper

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.