I have a hierarchical data model with product lines and then sublines and then sublines sublines, etc. What I'm trying to do is isolate only sublins who 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 getting children from all product lines. What I have tried but not working so far:
controller
$scope.prodClassIsALine = function(item) { return item.level_type=='line'; }; $scope.prodClassIsASubLineof = function(item) { return item.ancestor==$scope.prodClassIsALine.name; };
The tragic offer is just to show you that I need all the children of all lines, i.e. all elements with the names of the ancestors of the elements, which are strings.
Html
<div ng-repeat="item in items | filter:prodClassIsALine:prodClassIsASubLineof"> <p>{[{item.name}]}</p> </div>
Is this the way we embed filters in AngularJS? It seems that the filters are repeated according to the list specified as an attribute, but in addition, I can not understand in detail how they work. Please, help.
Decision
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"> <p>{[{item.name}]}</p> </div>