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>