功能虽然非常强大,但UI上提供的插件不像JQuery那么多,而且只能通过directive定义扩展的UI插件,虽然国外已经提供了一些基于
directive的Tree功能实现,但毕竟不像ZTree那样强大,而且Tree是做项目中很长用的一个基本功能。
因此,花了一点时间做了一个例子将ZTree应用到AngularJS中。
zTree和后台数据的交互
首先,肯定是在页面中引入Angularjs的相关脚本,例如模块(e.g. app.js)、控制器(e.g. controller.js)、Angularjs的脚本并进行相关标记的使用,然后引入zTree的样式包和zTreed 脚本,可以参看一下代码:
<!DOCTYPE html>
<html lang="zh-CN" ng-app="app">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>树型菜单</title>
<link href="plugins/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet">
<link href="css/zTreeStyle.css" rel="stylesheet">
</head>
<% include ./../include/header.html %>
<% include ./../include/top-menu.html %>
<div id="content" class="content clearfix" ng-controller="wtConfigInfo">
<ul tree id="tree" style="font:normal 12px/35px 'Arial'color:#dcdcdc" class="ztree" ng-model="selectNode" value="1" >
</div>
<% include ./../include/footer.html %>
<script src="plugins/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="plugins/bootstrap-3.3.5/js/bootstrap.min.js" type="text/javascript"></script>
<script src="..//js/angular.min.js" type="text/javascript"></script>
<script src="..//js/angular/app.js" type="text/javascript"></script>
<script src="..//js/angular/controllers.js" type="text/javascript"></script>
<script src="../plugins/zTree/jquery.ztree.all-3.5.js" type="text/javascript"></script>
</body>
</html>
在上面的在ul标签中添加了指令tree,这样在加载angularjs中,就可通过指令 tree来进行菜单数据的获取。具体的代码可参考以下代码:
var app = angular.module('app', [])
//树形结构
app.directive('tree',function(){
return{
require:'?ngModel',
restrict:'A',
link:function($scope,element,attrs,ngModel){
var setting = {
data :{
simpleData:{
enable:true
}
},
callback:{
beforeClick:function(treeId, treeNode) {//点击菜单时进行的处理
var zTree = $.fn.zTree.getZTreeObj("tree")
if (treeNode.isParent) {
zTree.expandNode(treeNode)
return false
} else {
window.location.href=treeNode.url
return true
}
}
}
}
//向控制器发送消息,进行菜单数据的获取
$scope.$emit("menu",attrs["value"])//此处attrs["value"]为ul中的value值,此处作为标记使用
//接受控制器返回的菜单的消息
$scope.$on("menuData",function(event,data){
$.fn.zTree.init(element, setting, data)//进行初始化树形菜单
var zTree = $.fn.zTree.getZTreeObj("tree")
var selectName = $("#selectName").val()
if(typeof selectName == "undefined" || selectName == ""){
zTree.selectNode(zTree.getNodeByParam("id","1"))//默认第一个选中
$("#selectName").val(zTree.getSelectedNodes()[0].code)//赋值
}else{
for(var i =0i<data.lengthi++){
if(data[i]["code"] == selectName ){
zTree.selectNode(zTree.getNodeByParam("code", data[i]["code"]))
}
}
}
})
}
}
})
在上述代码中,使用$scope.$emit("menu",attrs["value"])向父控制器发送请求数据,在控制器代码中可以接受此消息,并使用$http向后台进行数据的请求,并将从数据库中请求来的数据发送个子控制器。控制器的代码可参考如下:
function wtConfigInfo($scope,$http){
//接受子控制器发送的消息
$scope.$on("menu",function(event,params){
$http.get("/commonfuncode").success(function(data){
//发送消息给子控制器
$scope.$broadcast("menuData",dealMenuData(data,params))
})
})
}
这样,就完成了zTree和后台数据的交互。
利用指令集成ZTree的实例
<!doctype html>
<html lang="en" ng-app="app">
<head>
<meta charset="utf-8">
<title>ZTree</title>
<link rel="stylesheet" href="css/app.css">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/animations.css">
<link rel="stylesheet" href="css/zTreeStyle/zTreeStyle.css">
<script src="lib/jquery-1.10.2.min.js"></script>
<script src="lib/jquery.ztree.all-3.5.js"></script>
<script src="lib/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<body ng-controller='MyController'>
<ul tree class="ztree" ng-model="selectNode"></ul>
</body>
<pre>
{{selectNode | json}}
</pre>
</body>
</html>
app.js
'use strict'
/* App Module */
var appModule = angular.module('app', [])
appModule.directive('tree', function () {
return {
require: '?ngModel',
restrict: 'A',
link: function ($scope, element, attrs, ngModel) {
//var opts = angular.extend({}, $scope.$eval(attrs.nlUploadify))
var setting = {
data: {
key: {
title: "t"
},
simpleData: {
enable: true
}
},
callback: {
onClick: function (event, treeId, treeNode, clickFlag) {
$scope.$apply(function () {
ngModel.$setViewValue(treeNode)
})
}
}
}
var zNodes = [
{ id: 1, pId: 0, name: "普通的父节点", t: "我很普通,随便点我吧", open: true },
{ id: 11, pId: 1, name: "叶子节点 - 1", t: "我很普通,随便点我吧" },
{ id: 12, pId: 1, name: "叶子节点 - 2", t: "我很普通,随便点我吧" },
{ id: 13, pId: 1, name: "叶子节点 - 3", t: "我很普通,随便点我吧" },
{ id: 2, pId: 0, name: "NB的父节点", t: "点我可以,但是不能点我的子节点,有本事点一个你试试看?", open: true },
{ id: 21, pId: 2, name: "叶子节点2 - 1", t: "你哪个单位的?敢随便点我?小心点儿..", click: false },
{ id: 22, pId: 2, name: "叶子节点2 - 2", t: "我有老爸罩着呢,点击我的小心点儿..", click: false },
{ id: 23, pId: 2, name: "叶子节点2 - 3", t: "好歹我也是个领导,别普通群众就来点击我..", click: false },
{ id: 3, pId: 0, name: "郁闷的父节点", t: "别点我,我好害怕...我的子节点随便点吧...", open: true, click: false },
{ id: 31, pId: 3, name: "叶子节点3 - 1", t: "唉,随便点我吧" },
{ id: 32, pId: 3, name: "叶子节点3 - 2", t: "唉,随便点我吧" },
{ id: 33, pId: 3, name: "叶子节点3 - 3", t: "唉,随便点我吧" }
]
$.fn.zTree.init(element, setting, zNodes)
}
}
})
appModule.controller('MyController', function ($scope) {
})
实现功能:定义tree这个属性,使<ul tree class="ztree" ng-model="selectNode"></ul>自动变成一个有数据的tree,点击树节点,自动变更model 的selectNode。
很简单最流行的方法是调mongo 因为 mongo可以通过restful服务调用
另外早在十几年前 应该是00年 oracle sqlserver都支持XML服务了 只不过默认不开启
或者可以写一个通用的接口 java的 php什么语言都可以 只要传入SQL 返回json结果集就行了
-------我是华丽的分割线---------
不懂可以继续追问
会给你更好地建议,帮助解决可困难,喂百度知道做贡献
树控件可以作为一个DOM元素或属性。复制脚本和CSS为你的项目添加一个脚本和链接到你的页面。
<script type="text/javascript" src="/angular-tree-control.js"></script>
<link rel="stylesheet" type="text/css" href="css/tree-control.css">
<link rel="stylesheet" type="text/css" href="css/tree-control-attribute.css">
添加一个依赖于您的应用程序模块
angular.module('myApp', ['treeControl'])
将树元素添加到您的模板
<!-- as a Dom element -->
<treecontrol class="tree-classic"
tree-model="dataForTheTree"
options="treeOptions"
on-selection="showSelected(node)"
selected-node="node1">
employee: {{node.name}} age {{node.age}}
</treecontrol>
<!-- as an attribute -->
<div treecontrol class="tree-classic"
tree-model="dataForTheTree"
options="treeOptions"
on-selection="showSelected(node)"
selected-node="node1">
employee: {{node.name}} age {{node.age}}
</div>
并为树添加数据
$scope.treeOptions = {
nodeChildren: "children",
dirSelectable: true,
injectClasses: {
ul: "a1",
li: "a2",
liSelected: "a7",
iExpanded: "a3",
iCollapsed: "a4",
iLeaf: "a5",
label: "a6",
labelSelected: "a8"
}
}
$scope.dataForTheTree =
[
{ "name" : "Joe", "age" : "21", "children" : [
{ "name" : "Smith", "age" : "42", "children" : [] },
{ "name" : "Gary", "age" : "21", "children" : [
{ "name" : "Jenifer", "age" : "23", "children" : [
{ "name" : "Dani", "age" : "32", "children" : [] },
{ "name" : "Max", "age" : "34", "children" : [] }
]}
]}
]},
{ "name" : "Albert", "age" : "33", "children" : [] },
{ "name" : "Ron", "age" : "29", "children" : [] }
]
样式
树控件呈现以下的DOM结构
<treecontrol class="tree-classic">
<ul>
<li class="tree-expanded">
<i class="tree-branch-head"></i>
<i class="tree-leaf-head"></i>
<div class="tree-label">
... label - expanded angular template is in the treecontrol element ...
</div>
<treeitem>
<ul>
<li class="tree-leaf">
<i class="tree-branch-head"></i>
<i class="tree-leaf-head"></i>
<div class="tree-label tree-selected">
... label - expanded angular template is in the treecontrol element ...
</div>
</li>
<li class="tree-leaf">
<i class="tree-branch-head"></i>
<i class="tree-leaf-head"></i>
<div class="tree-label">
... label - expanded angular template is in the treecontrol element ...
</div>
</li>
</ul>
</treeitem>
</li>
</ul>
</treecontrol>
CSS类用于内置样式的树控件。附加的类可以使用options.injectclasses成员加入(见上文)
树扩展,树倒塌,树叶放在“UL”元素
树枝头,树的叶头-被放置在'i'元素。我们使用这些类来放置树的图标
树选择放置于标签div
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)