0

Given the solution here,http://jsfiddle.net/brendanowen/uXbn6/8/ in relation to the problem posted here, Is it possible to make a Tree View with Angular?

While the example of Fiddle lets you generate a nested list with liberty to how deep it is. I am more concerned on how to just display it, like the category widget in WordPress.

angular.module("myApp", []).
controller("TreeController", ['$scope', function($scope) {
    $scope.delete = function(data) {
        data.nodes = [];
    };
    $scope.add = function(data) {
        var post = data.nodes.length + 1;
        var newName = data.name + '-' + post;
        data.nodes.push({name: newName,nodes: []});
    };
    $scope.tree = [{name: "Node", nodes: []}];
}]);
ul {
    list-style: circle;
}
li {
    margin-left: 20px;
}
<script src="https://code.angularjs.org/angular-1.0.0rc8.js"></script>

<script type="text/ng-template"  id="tree_item_renderer.html">
    {{data.name}}
    <button ng-click="add(data)">Add node</button>
    <button ng-click="delete(data)" ng-show="data.nodes.length > 0">Delete nodes</button>
    <ul>
        <li ng-repeat="data in data.nodes" ng-include="'tree_item_renderer.html'"></li>
    </ul>
</script>

<ul ng-app="Application" ng-controller="TreeController">
    <li ng-repeat="data in tree" ng-include="'tree_item_renderer.html'"></li>
</ul>
Community
  • 1
  • 1
Mr A
  • 1,345
  • 4
  • 22
  • 51

1 Answers1

0

Displaying and modifying in angular is basically the same since it has 2 way data binding. So in the example you posted just fill the $scope.tree variae with your tree data and view will render it.

Also you may be interested in angular-ui-tree module that provides some outofbox functionality to trees: https://github.com/angular-ui-tree/angular-ui-tree