AngularJS Tree Menu Example for Github
Basic example.
HTML
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="myController">
<div style="margin:10px 0 30px 0; padding:10px; background-color:#EEEEEE; border-radius:5px; font:12px Tahoma;">
<span><b>Selected Node</b> : {{mytree.currentNode.roleName}}</span>
</div>
<hr>
<div>
<input ng-model='treeSearchQuery' type="text" placeholder="Search in tree..."/>
</div>
<!--
[TREE attribute]
angular-treeview: the treeview directive
tree-id : each tree's unique id.
tree-model : the tree model on $scope.
node-id : each node's id
node-label : each node's label
node-children: each node's children
-->
<div
data-angular-treeview="true"
data-tree-id="mytree"
data-tree-model="roleList"
data-node-id="roleId"
data-node-label="roleName"
data-node-children="children"
data-search-query='treeSearchQuery'>
</div>
</div>
</div>
CSS
div[data-angular-treeview] {
/* prevent user selection */
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
/* default */
font-family: Tahoma;
font-size:13px;
color: #555;
text-decoration: none;
}
div[data-tree-model] ul {
margin: 0;
padding: 0;
list-style: none;
border: none;
overflow: hidden;
}
div[data-tree-model] li {
position: relative;
padding: 0 0 0 20px;
line-height: 20px;
}
div[data-tree-model] li .expanded {
padding: 1px 10px;
background-image: url("http://cfile23.uf.tistory.com/image/205B973A50C13F4B19D9BD");
background-repeat: no-repeat;
}
div[data-tree-model] li .collapsed {
padding: 1px 10px;
background-image: url("http://cfile23.uf.tistory.com/image/1459193A50C13F4B1B05FB");
background-repeat: no-repeat;
}
div[data-tree-model] li .normal {
padding: 1px 10px;
background-image: url("http://cfile23.uf.tistory.com/image/165B663A50C13F4B196CCA");
background-repeat: no-repeat;
}
div[data-tree-model] li i, div[data-tree-model] li span {
cursor: pointer;
}
div[data-tree-model] li .selected {
background-color: #aaddff;
font-weight: bold;
padding: 1px 5px;
}
JavaScript
(function(){
//angular module
var myApp = angular.module('myApp', ['angularTreeview']);
//test controller
myApp.controller('myController', function($scope){
//test tree model 2
$scope.roleList2 = [
{ "roleName" : "User default expanded", "roleId" : "role1", "expanded" : true, "children" : [
{ "roleName" : "subUser1 non-openable folder", "roleId" : "role11" },
{ "roleName" : "subUser2", "roleId" : "role12", "expanded" : true, "children" : [
{ "roleName" : "subUser2-1", "roleId" : "role121", "expanded" : true, "children" : [
{ "roleName" : "subUser2-1-1 folder ico", "roleId" : "role1211" },
{ "roleName" : "subUser2-1-2 file ico", "roleId" : "role1212" , "fileicon" : true}
]}
]}
]},
{ "roleName" : "Admin default collapsed", "roleId" : "role2", "children" : [
{ "roleName" : "subAdmin1", "roleId" : "role11", "children" : [] },
{ "roleName" : "subAdmin2", "roleId" : "role12", "children" : [
{ "roleName" : "subAdmin2-1", "roleId" : "role121", "children" : [
{ "roleName" : "subAdmin2-1-1", "roleId" : "role1211", "children" : [] },
{ "roleName" : "subAdmin2-1-2", "roleId" : "role1212", "children" : [] }
]}
]}
]}
];
//roleList1 to treeview
$scope.roleList = $scope.roleList2;
});
})();
(function(angular){'use strict';angular.module('angularTreeview',[]).directive('treeModel',['$compile',function($compile){return{restrict:'A',link:function(scope,element,attrs){var treeId=attrs.treeId;var treeModel=attrs.treeModel;var nodeId=attrs.nodeId;var nodeLabel=attrs.nodeLabel;var nodeChildren=attrs.nodeChildren;var searchQuery=attrs.searchQuery;var template='<ul>'+'<li data-ng-repeat="node in '+treeModel+' | filter:'+searchQuery+' ">'+'<i class="collapsed" data-ng-class="{nopointer:...