最近在項目當中遇到一個需求,須要以樹結構的方式展現一些數據,並可對每個樹節點作內容的編輯以及樹節點的添加和刪除,恰好據說有zTree這個插件能夠實現這樣的需求,因此在項目的這個需求完成以後,在博客裏用一個小demo的形式記錄一下zTree的簡單實用方法。php
zTree的官網地址是:http://www.treejs.cn/v3/main.php#_zTreeInfocss
下載地址是:https://gitee.com/zTree/zTree_v3html
zTreeStyle.css
jquery.min.js
jquery.ztree.core.min.js
jquery.ztree.excheck.min.js
jquery.ztree.exedit.min.jsnode
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>zTree的簡單實用</title> <link rel="stylesheet" href="zTreeStyle.css"> <script src="jquery.min.js"></script> <script src="jquery.ztree.core-3.5.min.js"></script> <script src="jquery.ztree.excheck-3.5.min.js"></script> <script src="jquery.ztree.exedit-3.5.min.js"></script> <style> .ztree li {line-height: 25px;} .ztree li span.button.switch {height: 20px;margin-top: -7px;} .ztree li span.button.ico_open, .ztree li span.button.ico_close {background-image: none; width: 0px;margin-left: -2px;} .ztree li span.button.ico_docu {background-image: none; width: 0px;} .ztree li a {height: 25px;} .ztree li span {font-size: 14px;font-family: "PingFang SC", "Hiragino GB", "Microsoft YaHei", "Helvetica Neue", "Helvetica", "ARIAL"} .ztree li a.curSelectedNode {height: 18px;} .ztree li a.curSelectedNode_Edit{height: 18px;} .ztree li a.curSelectedNode_Edit span {display: inline-block;height: 18px;} .ztree li a.curSelectedNode_Edit input {display: inline-block;height: 18px;font-size: 14px;width: 100%;min-width: 150px;font-family: "PingFang SC", "Hiragino GB", "Microsoft YaHei", "Helvetica Neue", "Helvetica", "ARIAL"} .ztree li a.curSelectedNode {max-width: 260px;overflow: hidden;} .ztree li span {max-width: 185px;display: inline-block;overflow: hidden;} .ztree li span input{width: 150px;} .ztree li span.button.add {margin-left: 2px;background-position: -144px 0;vertical-align: top;background-image: url(imgs/jui.png);background-position-x: -278px;background-position-y: -237px;} .ztree li span.button.edit {margin-left: 3px;background-position: -144px 0;vertical-align: top;background-image: url(imgs/jui.png);background-position-x: 2px;background-position-y: 3px;} .ztree li span.button.remove {background-position: -144px 0;vertical-align: top;background-image: url(imgs/jui.png);background-position-x: -138px;background-position-y: -137px;} .ztree li span.button.roots_close, .ztree li span.button.root_close, .ztree li span.button.center_close, .ztree li span.button.bottom_close {background-image: url(imgs/jui.png);background-position-x: -77px;background-position-y: -196px;} .ztree li span.button.roots_open, .ztree li span.button.root_open, .ztree li span.button.center_open, .ztree li span.button.bottom_open {background-image: url(imgs/jui.png);background-position-x: -117px;background-position-y: -196px;} </style> </head> <body> <div> <ul id="treeDemo" class="ztree"></ul> </div> </body> <script> //zTree的配置 var setting = { view: { addHoverDom: addHoverDom, removeHoverDom: removeHoverDom, selectedMulti: false }, edit: { enable: true, editNameSelectAll: true, showRemoveBtn: showRemoveBtn, showRenameBtn: showRenameBtn }, data: { simpleData: { enable: true } }, callback: { onClick:zTreeOnClick, //點擊選中事件 beforeDrag: beforeDrag, // 可設置是否容許拖拽功能
beforeEditName: beforeEditName, beforeRemove: beforeRemove, beforeRename: beforeRename, onRemove: onRemove, onRename: onRename } }; //zTree初始化加載的節點樹 var zNodes =[ { id:1, pId:0, name:"父節點 1", open:true}, //open屬性的值true和false,表示是否在初始化加載後展開子節點 { id:11, pId:1, name:"子節點 1-1"}, { id:12, pId:1, name:"子節點 1-2"}, { id:13, pId:1, name:"子節點 1-3"}, { id:2, pId:0, name:"父節點 2", open:true}, { id:21, pId:2, name:"子節點 2-1"}, { id:22, pId:2, name:"子節點 2-2"}, { id:23, pId:2, name:"子節點 2-3"}, { id:3, pId:0, name:"父節點 3", open:false}, { id:31, pId:3, name:"子節點 3-1"}, { id:32, pId:3, name:"子節點 3-2"}, { id:33, pId:3, name:"子節點 3-3"}, ]; var log, className = "dark"; function zTreeOnClick(event, treeId, treeNode) { console.log('zTreeOnClick'); console.log(treeNode) var treeObj = $.fn.zTree.getZTreeObj("treeDemo"), nodes = treeObj.getCheckedNodes(true), v = ""; for (var i = 0; i < nodes.length; i++) { v += nodes[i].name + ","; console.log("節點id:" + nodes[i].id + "節點名稱" + v); //獲取選中節點的值 } }
function beforeDrag(treeId, treeNode) {
return false; //false=禁用拖拽功能, true=開啓
} function beforeEditName(treeId, treeNode) { console.log('beforeEditName'); console.log(treeNode) className = (className === "dark" ? "":"dark"); var zTree = $.fn.zTree.getZTreeObj("treeDemo"); zTree.selectNode(treeNode); zTree.editName(treeNode); } function beforeRemove(treeId, treeNode) { console.log('beforeRemove'); className = (className === "dark" ? "":"dark"); var zTree = $.fn.zTree.getZTreeObj("treeDemo"); zTree.selectNode(treeNode); return confirm("確認刪除 節點 -- " + treeNode.name + " 嗎?"); } function onRemove(e, treeId, treeNode) { //肯定刪除節點 console.log('onRemove'); console.log(treeNode) } function beforeRename(treeId, treeNode, newName, isCancel) { console.log('beforeRename'); className = (className === "dark" ? "":"dark"); if (newName.length == 0) { setTimeout(function() { var zTree = $.fn.zTree.getZTreeObj("treeDemo"); zTree.cancelEditName(); alert("節點名稱不能爲空。"); }, 0); return false; } return true; } function onRename(e, treeId, treeNode, isCancel) { console.log('onRename') console.log(treeNode) } function showRemoveBtn(treeId, treeNode) { return true; } function showRenameBtn(treeId, treeNode) { return true; } var newCount = 1; function addHoverDom(treeId, treeNode) { var sObj = $("#" + treeNode.tId + "_span"); if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return; var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='添加子節點' onfocus='this.blur();'></span>"; sObj.after(addStr); $('.edit').attr('title', '編輯'); $('.remove').attr('title', '刪除'); var btn = $("#addBtn_"+treeNode.tId); if (btn) btn.bind("click", function(){ var zTree = $.fn.zTree.getZTreeObj("treeDemo"); var obj = {id:(100 + newCount), pId:treeNode.id, name:"新增子節點 " + (newCount++)}; zTree.addNodes(treeNode, obj); console.log(obj) console.log('添加成功') return false; }); }; function removeHoverDom(treeId, treeNode) { $("#addBtn_"+treeNode.tId).unbind().remove(); }; $(document).ready(function(){ console.log('ready'); $.fn.zTree.init($("#treeDemo"), setting, zNodes); }); </script> </html>
其中的<style></style>部分是自定義的樣式,主要用來更換插件默認的添加、刪除、編輯、展開和收縮的小圖標的jquery
一、初始化加載頁面後: git
二、選中某個節點後,會出現添加、編輯、刪除的操做圖標:ui
三、在第一個父節點新增了一個子節點:this
四、在某一個節點點擊刪除按鈕後的確認提示:url
五、點擊了某一個節點的編輯按鈕後,呈現可編輯狀態:spa
六、編輯完成後點擊空白處,便可完成編輯:
七、每一個子節點下還能夠再無限新增子節點
注:以上代碼部分的操做,只是針對DOM作了增刪改的操做,若是在具體項目業務中使用的話,仍是要另外本身編寫相應代碼,來保存操做的數據,這裏再也不一一寫出。