ztree使用实例
Posted samve
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ztree使用实例相关的知识,希望对你有一定的参考价值。
一、获取点击选中的值:
<link rel="stylesheet" href="./static/libs/ztree/css/metroStyle/metroStyle.css" type="text/css"><!-- ztree -->
<script src="./static/libs/ztree/js/jquery.ztree.core-3.5.js"></script><!-- ztree -->
<script src="./static/libs/ztree/js/jquery.ztree.exedit.js"></script><!-- ztree -->
<script src="./static/libs/ztree/js/jquery.ztree.excheck-3.5.js"></script><!-- ztree -->
<script src="./static/libs/ztree/js/jquery.ztree.exhide-3.5.js"></script><!-- ztree -->
<style>
ul.ztree {border: 0 solid #617775;background: #ffffff;width:100%;height:100%;overflow-y:hidden;overflow-x:hidden;}
</style>
<ul id="treeDemo" class="ztree"></ul>
var setting = {
check: {
enable: true,
chkboxType: {
"Y": "ps",
"N": "ps"
}//设置子父关联关系
},
data: {
simpleData: {
enable: true
}
},
callback: {
beforeClick: beforeClick,
onCheck: onCheck
}
};
function beforeClick(treeId, treeNode) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.checkNode(treeNode, !treeNode.checked, null, true);
return false;
}
function onCheck(e, treeId, treeNode) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
nodes = zTree.getCheckedNodes(true),
v = "";
for (var i=0, l=nodes.length; i<l; i++) {
if(nodes[i].isParent){
}else{
v += nodes[i].name + ",";//name为获取节点名称 ,id为获取id,pId为获取pId
}
}
if (v.length > 0 ) {
v = v.substring(0, v.length-1);
}
alert(v);
}
var zNodes =[
{ id:1, pId:0, name:"根部门", open:true},
{ id:11, pId:1, name:"一级部门1", open:true},
{ id:111, pId:11, name:"一级部门1子部门1"},
{ id:112, pId:11, name:"一级部门1子部门2"},
{ id:12, pId:1, name:"一级部门2", open:true},
{ id:121, pId:12, name:"一级部门2子部门1"},
{ id:122, pId:12, name:"一级部门2子部门2"}
];
$.fn.zTree.init($("#treeDemo"), setting, zNodes);
二、点击获取节点值:
var setting = {
data: {
simpleData: {
enable: true
}
},
callback: {
onClick: onClick
}
};
function onClick(event, treeId, treeNode) {
alert(treeNode.id + ", " + treeNode.name);
//你的ajax代码
}
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($("#treeDemo"), setting, zNodes);
三、获取第一个父节点的第一个子节点 :
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
console.log(zTree.getNodes()[0].children[0]);
四、获取所有节点:
一般情况下,通过zTree官方API的方法获取得到的是一个包含所有子节点的根节点(子节点都处于 children 属性下)
var treeObj = $.fn.zTree.getZTreeObj(“tree");
var nodes = treeObj.getNodes();
当我们需要一次获取所有树节点的时候,有两种方法:
一种是通过递归的方式(此处就不提及了)。
另一种就是通过zTreet提供的方法:
var treeObj = $.fn.zTree.getZTreeObj(”tree“);
var node = treeObj.getNodes();
var nodes = treeObj.transformToArray(node);
这种方法得到的就是整颗树的所有节点集合了。