前面的話:zTree 是一個依靠 jQuery 實現的多功能 “樹插件”。優異的性能、靈活的配置、多種功能的組合是 zTree 最大優點。專門適合項目開發,尤其是 樹狀菜單、樹狀數據。
ztree官方文檔:http://www.treejs.cn/v3/api.php
現在寫了一個小的demo,具體可以參考官方文檔,從文檔上拿來一串json數據,放在前端的代碼裏面,方便大家查看效果,以及方便後端返回的數據。
<!DOCTYPE html>
<html>
<head>
<title>ztree</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="ztree_v3/css/zTreeStyle/zTreeStyle.css" />
<link rel="stylesheet" type="text/css" href="ztree_v3/ztree_custom.css" />
<script src="js/jquery-2.1.1.min.js"></script>
<script src="ztree_v3/js/jquery.ztree.core-3.5.min.js"></script>
<script src="ztree_v3/js/jquery.ztree.excheck-3.5.min.js"></script>
<script src="ztree_v3/js/jquery.ztree.exedit-3.5.min.js"></script>
</head>
<body>
<div class="content_wrap">
<div class="zTreeDemoBackground left">
<ul id="sys" class="ztree"></ul>
</div>
</div>
</body>
<script type="text/javascript">
var setting = {
view: {
showLine: false, //不顯示連接線
//showIcon: showIconForTree //不顯示文件夾圖標(調用showIconForTree())
},
data: {
simpleData: {
enable: true
}
}
};
var nodes = [
{ id: 1, pId: 0, name: "父節點1 - 展開", open: false }, //根據pId參數指定父結點
{ id: 11, pId: 1, name: "父節點11 - 摺疊" },
{ id: 111, pId: 11, name: "葉子節點111" },
{ id: 112, pId: 11, name: "葉子節點112" },
{ id: 113, pId: 11, name: "葉子節點113" },
{ id: 114, pId: 11, name: "葉子節點114" },
{ id: 12, pId: 1, name: "父節點12 - 摺疊" },
{ id: 121, pId: 12, name: "葉子節點121" },
{ id: 122, pId: 12, name: "葉子節點122" },
{ id: 123, pId: 12, name: "葉子節點123" },
{ id: 124, pId: 12, name: "葉子節點124" },
{ id: 13, pId: 1, name: "父節點13 - 沒有子節點", isParent: false },
{ id: 2, pId: 0, name: "父節點2 - 摺疊" },
{ id: 21, pId: 2, name: "父節點21 - 展開", open: false },
{ id: 211, pId: 21, name: "葉子節點211" },
{ id: 212, pId: 21, name: "葉子節點212" },
{ id: 213, pId: 21, name: "葉子節點213" },
{ id: 214, pId: 21, name: "葉子節點214" },
{ id: 22, pId: 2, name: "父節點22 - 摺疊" },
{ id: 221, pId: 22, name: "葉子節點221" },
{ id: 222, pId: 22, name: "葉子節點222" },
{ id: 223, pId: 22, name: "葉子節點223" },
{ id: 224, pId: 22, name: "葉子節點224" },
{ id: 23, pId: 2, name: "父節點23 - 摺疊" },
{ id: 231, pId: 23, name: "葉子節點231" },
{ id: 232, pId: 23, name: "葉子節點232" },
{ id: 233, pId: 23, name: "葉子節點233" },
{ id: 234, pId: 23, name: "葉子節點234" },
{ id: 3, pId: 0, name: "父節點3 - 沒有子節點", isParent: true }
];
/*function showIconForTree(treeId, treeNode) {
return !treeNode.isParent;
};*/
$(document).ready(function() {
$.fn.zTree.init($("#sys"), setting, nodes);
});
</script>
</html>
在瀏覽器裏面打開,效果如圖所示:
把demo放在了github上面,有需要的可以自行下載;github:https://github.com/wangxiaoting666/ztree
現在如果是把數據放在json裏面,通過ajax去請求,動態渲染出來。
這裏的一切插件直接去前面給到的ztree的官方網站上去下載到本地,就可以直接引用了。
demo如下:
<!DOCTYPE html>
<html>
<head>
<title>ztree</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="ztree_v3/css/zTreeStyle/zTreeStyle.css" />
<link rel="stylesheet" type="text/css" href="ztree_v3/ztree_custom.css" />
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="ztree_v3/js/jquery.ztree.core-3.5.min.js"></script>
<script src="ztree_v3/js/jquery.ztree.excheck-3.5.min.js"></script>
<script src="ztree_v3/js/jquery.ztree.exedit-3.5.min.js"></script>
</head>
<body>
<ul id="zTree" class="ztree" style="background: #0b2b5f;">
</ul>
</body>
<script type="text/javascript">
//樹形菜單
var zTreeObj; //定義ztree對象
initTree(); //初始化ztree
var setting = {
check: {
enable: true,
chkStyle: "checkbox",
chkboxType: {
"Y": "s",
"N": "s"
}
},
view: {
selectedMulti: true,
showLine: false
},
data: {
key: {
name: "name"
},
simpleData: {
enable: true,
pIdKey: "pId",
}
},
};
//請求數據
function initTree() {
$.get("test.json", function(data) {
console.log(JSON.stringify(data));
zTreeObj = $.fn.zTree.init($("#zTree"), setting, data);
zTreeObj.expandAll(true);
});
}
</script>
</html>
test.json數據
自己動手,寫一些模擬的json數據吧。
[
{
"id": 1,
"pId": null,
"name": "特物聯",
"iconSkin": null,
"checked": null,
"isParent": true,
"token": null
},
{
"id": 2,
"pId": 1,
"name": "管理部",
"iconSkin": null,
"checked": null,
"isParent": false,
"token": null
},
{
"id": 157,
"pId": 2,
"name": "我問問",
"iconSkin": null,
"checked": null,
"isParent": false,
"token": null
},
{
"id": 158,
"pId": 157,
"name": "嗚嗚嗚",
"iconSkin": null,
"checked": null,
"isParent": false,
"token": null
},
{
"id": 160,
"pId": 158,
"name": "熱熱",
"iconSkin": null,
"checked": null,
"isParent": false,
"token": null
},
{
"id": 159,
"pId": 2,
"name": "熱熱",
"iconSkin": null,
"checked": null,
"isParent": false,
"token": null
},
{
"id": 134,
"pId": 1,
"name": "研發部",
"iconSkin": null,
"checked": null,
"isParent": false,
"token": null
},
{
"id": 140,
"pId": 1,
"name": "安環部",
"iconSkin": null,
"checked": null,
"isParent": false,
"token": null
},
{
"id": 143,
"pId": 1,
"name": "會議部",
"iconSkin": null,
"checked": null,
"isParent": false,
"token": null
},
{
"id": 152,
"pId": 1,
"name": "生產部",
"iconSkin": null,
"checked": null,
"isParent": false,
"token": null
}
]
另外:
往期合集
一些demo
jQuery的ztree仿windows文件新建和拖拽效果
https://www.jianshu.com/p/bfa67325719c
ztree實現編輯和刪除功能
https://www.jianshu.com/p/95d1df89665f
ztree實現根節點單擊事件,顯示節點信息
https://www.jianshu.com/p/1e0ca6d8afad