简介
zTree 是一个依靠 jQuery 实现的多功能 “树插件”。优异的性能、灵活的配置、多种功能的组合是 zTree 最大优点。专门适合项目开发,尤其是 树状菜单、树状数据的Web显示、权限管理等等。
zTree 是开源免费的软件(MIT 许可证)。在开源的作用下,zTree 越来越完善,目前已经拥有了不少粉丝,并且今后还会推出更多的 zTree 扩展功能库,让 zTree 更加强大。
特点
● zTree v3.0 将核心代码按照功能进行了分割,不需要的代码可以不用加载
● 采用了延迟加载技术,上万节点轻松加载,即使在 IE6 下也能基本做到秒杀
● 兼容 IE、FireFox、Chrome、Opera、Safari 等浏览器
● 支持 JSON 数据
● 支持静态和 Ajax 异步加载节点数据
● 支持任意更换皮肤 / 自定义图标(依靠css)
● 支持极其灵活的 checkbox 或radio 选择功能
● 提供多种事件响应回调
● 灵活的编辑(增/删/改/查)功能,可随意拖拽节点,还可以多节点拖拽哟
● 在一个页面内可同时生成多个 Tree 实例
● 简单的参数配置实现灵活多变的功能
导入组件
<link rel
="stylesheet" href
="plugins/ztree/css/zTreeStyle/zTreeStyle.css" type
="text/css">
<script type
="text/javascript" src
="plugins/ztree/js/jquery-1.4.4.min.js"></script
>
<script type
="text/javascript" src
="plugins/ztree/js/jquery.ztree.core-3.5.js"></script
>
<script type
="text/javascript" src
="plugins/ztree/js/jquery.ztree.excheck-3.5.js"></script
>
案列
<!DOCTYPE html
>
<html lang
="en">
<head>
<meta charset
="UTF-8">
<title>ztree的demo
</title
>
<link rel
="stylesheet" href
="plugins/ztree/css/zTreeStyle/zTreeStyle.css">
<script src
="plugins/ztree/js/jquery-1.4.4.min.js"></script
>
<script src
="plugins/ztree/js/jquery.ztree.all-3.5.js"></script
>
<SCRIPT LANGUAGE
="JavaScript">
var zTreeObj
;
var setting
= {
data
: {
simpleData
: {
enable
: true,
idKey
: "id",
pIdKey
: "pId",
rootPId
: 0
}
},
check
: {
enable
: true
}
};
var zNodes
=[
{id
:"1",name
:"test1",pId
:"0"},
{id
:"2",name
:"test2",pId
:"0"},
{id
:"11",name
:"test1_1",pId
:"1"},
{id
:"12",name
:"test1_2",pId
:"1"},
{id
:"21",name
:"test2_1",pId
:"2"},
{id
:"22",name
:"test2_2",pId
:"2",checked
:true},
{id
:"121",name
:"test1_2_1",pId
:"12"}
]
$
(document
).ready(function(){
zTreeObj
= $
.fn
.zTree
.init($
("#treeDemo"), setting
, zNodes
);
zTreeObj
.expandAll(true);
});
function
save() {
let idsStr
="";
let checkedNodes
= zTreeObj
.getCheckedNodes(true);
for (let i
= 0; i
< checkedNodes
.length
; i
++) {
if((checkedNodes
.length
-1)==i
){
idsStr
+= checkedNodes
[i
].id
;
}else{
idsStr
+= checkedNodes
[i
].id
+",";
}
}
alert(idsStr
);
}
</SCRIPT
>
</head
>
<body>
<div>
<button onclick
="save()">保存
</button
>
<ul id
="treeDemo" class="ztree"></ul
>
</div
>
</body
>
</html
>