Being new to AngularJS, I find the task of grouping data by tree level and tree id quite challenging. I would appreciate some assistance or guidance on this. What I aim to accomplish is grouping the data in a tree-like structure based on their level and id.
Here is my current HTML code:
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<ul>
<li ng-repeat="item in list">
[[ item.code ]]
</li>
</ul>
</div>
</body>
</html>
And here is my JavaScript code:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.list = [
{
"id": 1,
"code": "TANK",
"tree_id": 1,
"level": 0,
"parent": null
},
{
"id": 2,
"code": "OIL",
"tree_id": 1,
"level": 1,
"parent": 1
},
{
"id": 3,
"code": "CHEM",
"tree_id": 1,
"level": 1,
"parent": 1
},
{
"id": 6,
"code": "PROD",
"tree_id": 1,
"level": 2,
"parent": 3
},
{
"id": 4,
"code": "BULK",
"tree_id": 2,
"level": 0,
"parent": null
},
{
"id": 5,
"code": "LOG",
"tree_id": 2,
"level": 1,
"parent": 4
}
],
});
Upon reviewing the $scope.list, you will notice the tree_id, level, and parent values. My goal is to group the objects by tree id, where the top level (level 0) will be the visible section and the lower levels (level 1 and up) will be collapsible content. The tree id acts as a grouping mechanism, the level determines the hierarchy, and the parent integer value defines the root of the hierarchy. It's important to note that this structure can have multiple branches and be flexible and unlimited in its depth.
The desired visual representation should resemble the following:
- TANK
- CHEM
- OIL
- PROD
- BULK
- LOG