<!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - copyNode / moveNode</TITLE> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="../js/jquery-3.3.1.js"></script> <link rel="stylesheet" href="../bower_components/ztree/css/zTreeStyle/demo.css" type="text/css"> <link rel="stylesheet" href="../bower_components/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css"> <script type="text/javascript" src="../bower_components/ztree/js/jquery-1.4.4.min.js"></script> <script type="text/javascript" src="../bower_components/ztree/js/jquery.ztree.core-3.5.js"></script> <script type="text/javascript" src="../bower_components/ztree/js/jquery.ztree.excheck-3.5.js"></script> <script type="text/javascript" src="../bower_components/ztree/js/jquery.ztree.exedit-3.5.js"></script> <SCRIPT type="text/javascript"> <!-- var setting = { view: { selectedMulti: false }, edit: { enable: true, showRemoveBtn: false, showRenameBtn: false }, data: { simpleData: { enable: true } }, callback: { beforeClick: beforeClick } }; var zNodes =[ { id:1, pId:0, name:"父节点 1", open:true}, { 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:true }, { id:31, pId:3, name:"叶子节点 3-1"}, { id:32, pId:3, name:"叶子节点 3-2"}, { id:33, pId:3, name:"叶子节点 3-3"} ]; function fontCss(treeNode) { var aObj = $("#" + treeNode.tId + "_a"); aObj.removeClass("copy").removeClass("cut"); if (treeNode === curSrcNode) { if (curType == "copy") { aObj.addClass(curType); } else { aObj.addClass(curType); } } } function beforeClick(treeId, treeNode) { alert('你选择前点击了' + treeNode.name); return !treeNode.isCur; //点剪切/复制以前,点击此结点isCur还未定义,所以强制返回true,点剪贴/复制以后返回false } var curSrcNode, curType; function setCurSrcNode(treeNode) { var zTree = $.fn.zTree.getZTreeObj("sys"); if (curSrcNode) { //当第一次处理()选中的结点时,curSrcNode还未定义,返回false,第二次处理()时,返回true delete curSrcNode.isCur; var tmpNode = curSrcNode; curSrcNode = null; //粘贴完第一个后,为了重复利用,需要清空curSrcNode,方便再次选择 fontCss(tmpNode); } curSrcNode = treeNode; if (!treeNode) return; //如果不存在,退出该函数 curSrcNode.isCur = true; zTree.cancelSelectedNode(); //处理好后,取消选中的点 fontCss(curSrcNode); } function copy(e) { var zTree = $.fn.zTree.getZTreeObj("sys"), nodes = zTree.getSelectedNodes(); if (nodes.length == 0) { alert("请先选择一个节点"); return; } curType = "copy"; alert('你选择了' + nodes[0].name); setCurSrcNode(nodes[0]); } function cut(e) { var zTree = $.fn.zTree.getZTreeObj("sys"), nodes = zTree.getSelectedNodes(); if (nodes.length == 0) { alert("请先选择一个节点"); return; } curType = "cut"; setCurSrcNode(nodes[0]); } function paste(e) { //点击paste按扭时,判断curSrcNode是否定义 if (!curSrcNode) { alert("请先选择一个节点进行 复制 / 剪切"); return; } var zTree = $.fn.zTree.getZTreeObj("sys"), nodes = zTree.getSelectedNodes(), targetNode = nodes.length>0? nodes[0]:null; //如果选中多个,则只对第一个进行处理,如果为空,则定义为null alert(curSrcNode.parentTId); if (curSrcNode === targetNode) { //同输出 alert("不能移动,源节点 与 目标节点相同"); return; } else if (curType === "cut" && ((!!targetNode && curSrcNode.parentTId === targetNode.tId) || (!targetNode && !curSrcNode.parentTId))) { //当为cut的时候,一:1.目标结点存在 2.目标结点是选中的结点的父结点 // 二: 1.目标结点 和 选中的结点的父结点 都是root(我也不知道昨说,这么说,你应该知道) alert("不能移动,源节点 已经存在于 目标节点中"); return; } else if (curType === "copy") { targetNode = zTree.copyNode(targetNode, curSrcNode, "inner"); //复制函数 } else if (curType === "cut") { targetNode = zTree.moveNode(targetNode, curSrcNode, "inner"); //剪贴函数 if (!targetNode) { alert("剪切失败,源节点是目标节点的父节点"); } targetNode = curSrcNode; } setCurSrcNode(); delete targetNode.isCur; zTree.selectNode(targetNode); } $(document).ready(function(){ $.fn.zTree.init($("#sys"), setting, zNodes); $("#copy").bind("click", copy); $("#cut").bind("click", cut); $("#paste").bind("click", paste); }); //--> </SCRIPT> </HEAD> <BODY> <div class="content_wrap"> <div class="zTreeDemoBackground left"> <ul id="sys" class="ztree"></ul> </div> <div class="right"> [ <a id="copy" href="#" title="复制" οnclick="return false;">复制</a> [ <a id="cut" href="#" title="剪切" οnclick="return false;">剪切</a> ] [ <a id="paste" href="#" title="粘贴" οnclick="return false;">粘贴</a> ] </div> </div> </BODY> </HTML>