ztree顶级节点不具备删除按钮其下子节点具备,需要设置showRemoveBtn: setRemoveBtn
edit: {
enable: true,
showRemoveBtn: setRemoveBtn,
}
function setRemoveBtn(treeId, treeNode) {
//判断为顶级节点则不显示删除按钮
if(treeNode.level == 0)
return false;
else
return true;
}
<!DOCTYPE html> <HTML> <HEAD> <TITLE> ztree仅并列同级进行推拽(同一个组织下不同部门排序拖拽)</TITLE> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="../../../css/demo.css" type="text/css"> <link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css"> <script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script> <script type="text/javascript" src="../../../js/jquery.ztree.core-3.1.js"></script> <script type="text/javascript" src="../../../js/jquery.ztree.excheck-3.1.js"></script> <script type="text/javascript" src="../../../js/jquery.ztree.exedit-3.1.js"></script> <SCRIPT type="text/javascript"> <!-- var dragId; var setting = { view: { addHoverDom: addHoverDom, removeHoverDom: removeHoverDom, selectedMulti: false }, edit: { enable: true, showRemoveBtn: setRemoveBtn, drag: { prev: true, next: true, inner: false }, editNameSelectAll: true }, data: { simpleData: { enable: true } }, callback: { beforeDrag: beforeDrag, beforeEditName: beforeEditName, beforeRemove: beforeRemove, beforeRename: beforeRename, onRemove: onRemove, onRename: onRename, beforeDrag: beforeDrag, beforeDrop: beforeDrop } }; var zNodes =[ { id:1, pId:0, name:"总公司", open:true, iconSkin:"pIcon01"}, { id:2, pId:1, name:"一分公司", open:true, iconSkin:"pIcon02"}, { id:3, pId:2, name:"总经办", open:true, iconSkin:"pIcon02"}, { id:4, pId:3, name:"总经理", open:true, iconSkin:"pIcon01"}, { id:5, pId:4, name:"研发经理", open:true, iconSkin:"pIcon01"}, { id:6, pId:5, name:"研发部", iconSkin:"icon01"}, { id:7, pId:3, name:"销售经理"}, { id:8, pId:3, name:"售后经理"}, { id:9, pId:1, name:"二分公司",open:true, iconSkin:"icon02"} ]; var log, className = "dark"; function beforeDrag(treeId, treeNodes) { return false; } function beforeEditName(treeId, treeNode) { className = (className === "dark" ? "":"dark"); showLog("[ "+getTime()+" beforeEditName ] " + treeNode.name); var zTree = $.fn.zTree.getZTreeObj("treeDemo"); zTree.selectNode(treeNode); return true; } function beforeRemove(treeId, treeNode) { className = (className === "dark" ? "":"dark"); showLog("[ "+getTime()+" beforeRemove ] " + treeNode.name); var zTree = $.fn.zTree.getZTreeObj("treeDemo"); zTree.selectNode(treeNode); return confirm("确认删除 节点 -- " + treeNode.name + " 吗?"); } function onRemove(e, treeId, treeNode) { showLog("[ "+getTime()+" onRemove ] " + treeNode.name); } function beforeRename(treeId, treeNode, newName) { className = (className === "dark" ? "":"dark"); showLog("[ "+getTime()+" beforeRename ] " + treeNode.name); if (newName.length == 0) { alert("节点名称不能为空."); var zTree = $.fn.zTree.getZTreeObj("treeDemo"); setTimeout(function(){zTree.editName(treeNode)}, 10); return false; } return true; } function onRename(e, treeId, treeNode) { showLog("[ "+getTime()+" onRename ] " + treeNode.name); } function showLog(str) { if (!log) log = $("#log"); log.append("<li class='"+className+"'>"+str+"</li>"); if(log.children("li").length > 8) { log.get(0).removeChild(log.children("li")[0]); } } function getTime() { var now= new Date(), h=now.getHours(), m=now.getMinutes(), s=now.getSeconds(), ms=now.getMilliseconds(); return (h+":"+m+":"+s+ " " +ms); } var newCount = 1; function addHoverDom(treeId, treeNode) { var sObj = $("#" + treeNode.tId + "_span"); if (treeNode.editNameFlag || $("#addBtn_"+treeNode.id).length>0) return; var addStr = "<button type='button' class='add' id='addBtn_" + treeNode.id + "' title='add node' οnfοcus='this.blur();'></button>"; sObj.append(addStr); var btn = $("#addBtn_"+treeNode.id); if (btn) btn.bind("click", function(){ var zTree = $.fn.zTree.getZTreeObj("treeDemo"); zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, name:"new node" + (newCount++)}); return false; }); } function setRemoveBtn(treeId, treeNode) { if(treeNode.level == 0) return false; else return true; } function removeHoverDom(treeId, treeNode) { $("#addBtn_"+treeNode.id).unbind().remove(); }; function beforeDrag(treeId, treeNodes) { for (var i=0,l=treeNodes.length; i<l; i++) { dragId = treeNodes[i].pId; if (treeNodes[i].drag === false) { return false; } } return true; } function beforeDrop(treeId, treeNodes, targetNode, moveType) { if(targetNode.pId == dragId){ return true; }else{ return false; } } function selectAll() { var zTree = $.fn.zTree.getZTreeObj("treeDemo"); zTree.setting.edit.editNameSelectAll = $("#selectAll").attr("checked"); }; $(document).ready(function(){ $.fn.zTree.init($("#treeDemo"), setting, zNodes); $("#selectAll").bind("click", selectAll); }); --> </SCRIPT> <style type="text/css"> .ztree li button.pIcon01_ico_open{margin-right:2px; background: url(../../../css/zTreeStyle/img/diy/1_open.png) no-repeat scroll 0 0 transparent; vertical-align:top; *vertical-align:middle} .ztree li button.pIcon01_ico_close{margin-right:2px; background: url(../../../css/zTreeStyle/img/diy/1_close.png) no-repeat scroll 0 0 transparent; vertical-align:top; *vertical-align:middle} .ztree li button.pIcon02_ico_open, .ztree li button.pIcon02_ico_close{margin-right:2px; background: url(../../../css/zTreeStyle/img/diy/2.png) no-repeat scroll 0 0 transparent; vertical-align:top; *vertical-align:middle} .ztree li button.icon01_ico_docu{margin-right:2px; background: url(../../../css/zTreeStyle/img/diy/3.png) no-repeat scroll 0 0 transparent; vertical-align:top; *vertical-align:middle} .ztree li button.icon02_ico_docu{margin-right:2px; background: url(../../../css/zTreeStyle/img/diy/4.png) no-repeat scroll 0 0 transparent; vertical-align:top; *vertical-align:middle} .ztree li button.icon03_ico_docu{margin-right:2px; background: url(../../../css/zTreeStyle/img/diy/5.png) no-repeat scroll 0 0 transparent; vertical-align:top; *vertical-align:middle} .ztree li button.icon04_ico_docu{margin-right:2px; background: url(../../../css/zTreeStyle/img/diy/6.png) no-repeat scroll 0 0 transparent; vertical-align:top; *vertical-align:middle} .ztree li button.icon05_ico_docu{margin-right:2px; background: url(../../../css/zTreeStyle/img/diy/7.png) no-repeat scroll 0 0 transparent; vertical-align:top; *vertical-align:middle} .ztree li button.icon06_ico_docu{margin-right:2px; background: url(../../../css/zTreeStyle/img/diy/8.png) no-repeat scroll 0 0 transparent; vertical-align:top; *vertical-align:middle} .ztree li button.add {margin-left:2px; margin-right: -1px; background-position:-144px 0; vertical-align:top; *vertical-align:middle} </style> </HEAD> <BODY> <div style="height:500px;"> <ul id="treeDemo" class="ztree"></ul> </div> </BODY> </HTML>