Angular nested filters for hierarchical data model

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"> <!-- or <div ng-repeat="item in items | prodClassLineChild"--> <p>{[{item.name}]}</p> </div> 
+4
source share
1 answer

does that really mean?

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

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

EDIT:

now i got u didnt write filters, but function area, u need something like this

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

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

should look like this:

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

ps in angular 1.2 they added standard iif syntax

+4
source

Source: https://habr.com/ru/post/1499274/


All Articles