Created
April 15, 2015 11:19
-
-
Save OscarGalindo/c8a857583c964852ecdb to your computer and use it in GitHub Desktop.
Tree view in angularjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html ng-app="ctz"> | |
<head> | |
<title>CategoryTree</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> | |
<script> | |
angular.module('treeApp', []); | |
angular.module('treeApp').controller('treeCtrl', treeCtrl); | |
treeCtrl.$inject = ['$scope', '$http']; | |
function treeCtrl($scope, $http) { | |
$scope.count = function (data) { | |
if (data.childs) { | |
return '(' + Object.keys(data.childs).length + ')'; | |
} else return ''; | |
}; | |
$http.get('tree.json').success(function (data) { | |
$scope.tree = data | |
}) | |
} | |
</script> | |
</head> | |
<body> | |
<script type="text/ng-template" id="tree.html"> | |
<span ng-click="child.open = !child.open">{{child.label}} {{count(child)}}</span> | |
<ul ng-show="child.open"> | |
<li ng-repeat="child in child.childs" ng-include="'tree.html'"></li> | |
</ul> | |
</script> | |
<ul ng-controller="treeCtrl"> | |
<li ng-repeat="child in tree" ng-include="'tree.html'"></li> | |
</ul> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"0": { | |
"label": "Label 0" | |
}, | |
"1": { | |
"label": "Label 1", | |
"childs": { | |
"0": { | |
"label": "Label 1 - Child 0" | |
}, | |
"1": { | |
"label": "Label 1 - Child 1", | |
"childs": { | |
"0": { | |
"label": "Label 1 - Child 1 - Child 0" | |
} | |
} | |
} | |
} | |
}, | |
"2": { | |
"label": "Label 2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment