实现思路: 1、ztree中节点是通过一个json的Array作为数据源、,所以直接操作json字符串,然后转为json对象数组。 2、然后用新构成的json数组对象重新初始化ztree对象。 复制代码 代码如下: <BR> var zTreeObj1;<BR> var zTreeObj2;<BR> var leftDivStr = "[";<BR> var rightDivStr = "[";<BR> var setting = {<BR> edit: {<BR> enable: false,<BR> showRemoveBtn: false,<BR> showRenameBtn: false<BR> },<BR> data: {<BR> simpleData: {<BR> enable: true<BR> }<BR> },<BR> callback: {<BR> //onClick : menuOnClick<BR> }<BR> };<BR> function menuOnClick(event, treeId, treeNode, clickFlag) {<br><br> }<BR> //注册toSource函数,解决ie不支持Array的toSource()方法的问题<BR> Array.prototype.toSource = function (){<BR> var str = "[";<BR> for(var i = 0 ;i<this.length;i++){<BR> str+="{id:\""+this[i].id+<BR> "\",pId:\""+this[i].pId<BR> +"\",name:\""+this[i].name<BR> +"\",isParent:\""+this[i].isParent<BR> +"\",file:\""+this[i].file<BR> +"\",icon:\""+this[i].icon<BR> +"\",open:\""+this[i].open<BR> +"\"},";<BR> }<BR> if(this.length != 0){<BR> str = str.substring(0, str.length-1);<BR> }<BR> str +="]";<BR> return str; <P> } ;<BR> //注册unique函数,去掉array中重复的对象(id相同即为同一对象)<BR> Array.prototype.unique = function (){<BR> var r = new Array();<BR> label:for(var i = 0, n = this.length; i < n; i++) {<BR> for(var x = 0, y = r.length; x < y; x++) {<BR> if(r[x].id == this[i].id) {<BR> continue label;<BR> }<BR> }<BR> r[r.length] = this[i];<BR> }<BR> return r; <P> } ;<BR> //初始数据<BR> var zNodes =[<BR> { id:1, pId:0, name:"父节点 1", open:true},<BR> { id:11, pId:1, name:"叶子节点 1-1",open:true},<BR> { id:111, pId:11, name:"叶子节点 11-1"},<BR> { id:112, pId:11, name:"叶子节点 11-2"},<BR> { id:12, pId:1, name:"叶子节点 1-2",open:true},<BR> { id:121, pId:12, name:"叶子节点 12-1"},<BR> { id:122, pId:12, name:"叶子节点 12-2"},<BR> { id:13, pId:1, name:"叶子节点 1-3"},<BR> { id:2, pId:0, name:"父节点 2", open:true},<BR> { id:21, pId:2, name:"叶子节点 2-1"},<BR> { id:22, pId:2, name:"叶子节点 2-2"},<BR> { id:23, pId:2, name:"叶子节点 2-3"},<BR> { id:3, pId:0, name:"父节点 3", open:true},<BR> { id:31, pId:3, name:"叶子节点 3-1"},<BR> { id:32, pId:3, name:"叶子节点 3-2"},<BR> { id:33, pId:3, name:"叶子节点 3-3"}<BR> ];<br><br> for(var i=0;i<zNodes.length;i++){<BR> leftDivStr+="{id:"+zNodes[i].id+",pId:"+zNodes[i].pId+",name:\""+zNodes[i].name+"\",open:"+zNodes[i].open+"},";<BR> }<BR> leftDivStr = leftDivStr.substring(0,leftDivStr.length-1);<BR> leftDivStr+="]";<BR> rightDivStr += "]";<br><br> //查找被移动节点的所有父节点<BR> function findParentNodes(treeNode, parentNodes){<BR> parentNodes += "{id:"+treeNode.id+",pId:"+treeNode.pId+<BR> ",name:\""+treeNode.name+"\",open:"+treeNode.open+"},";<BR> if(treeNode != null && treeNode.getParentNode()!= null){<BR> parentNodes =findParentNodes(treeNode.getParentNode(),parentNodes);<br><br> }<BR> return parentNodes;<BR> } <BR> //移动节点<BR> function moveNodes(zTreeFrom,treeNode,zTreeTo,divStrFrom,divStrTo){<BR> /////////////////////////////////treeNode的所有父节点<BR> var parentNodes ="[";<BR> if(treeNode.pId != null){<BR> parentNodes = findParentNodes(treeNode,parentNodes);<BR> parentNodes = parentNodes.substring(0,parentNodes.length-1);<BR> }<br><br> parentNodes +="]";<BR> //alert(parentNodes);<BR> var parentNodesArray = eval(parentNodes);<BR> ///////////////////////////////<BR> var nodes = "[";<BR> nodes+= "{id:"+treeNode.id+",pId:"+treeNode.pId+",name:\""+treeNode.name+"\",open:"+treeNode.open+"},";<BR> nodes = operChildrenNodes(treeNode,nodes);<BR> nodes = nodes.substring(0,nodes.length-1);<BR> nodes+="]";<BR> var nodesArray = eval(nodes);<BR> var divFromArray = eval(divStrFrom);<BR> var divToArray = eval(divStrTo);<BR> for(var i = 0;i<nodesArray.length;i++){//删除节点<BR> for(var j = 0;j<divFromArray.length;j++){<BR> if(divFromArray[j].id == nodesArray[i].id){<BR> divFromArray.splice(j,1);<BR> }<BR> }<BR> }<br><br> divToArray = divToArray.concat(nodesArray);//增加节点<BR> divToArray = divToArray.concat(parentNodesArray);<br><br> ///////////////////////////////////////////////////////////////////////////////////////去重复<BR> divFromArray = divFromArray.unique();<BR> divToArray = divToArray.unique();<BR> ///////////////////////////////////////////////////////////////////////////////////////////去重复<br><br> if(zTreeFrom.setting.treeId == "treeDemo"){<BR> leftDivStr = divFromArray.toSource();<BR> rightDivStr =divToArray.toSource();<BR> $.fn.zTree.init($("#treeDemo"), setting, divFromArray);<BR> $.fn.zTree.init($("#treeDemo2"), setting,divToArray);<br><br> }else{<BR> leftDivStr = divToArray.toSource();<BR> rightDivStr =divFromArray.toSource();<BR> $.fn.zTree.init($("#treeDemo2"), setting, divFromArray);<BR> $.fn.zTree.init($("#treeDemo"), setting,divToArray);<BR> }<BR> }<br><br> <BR> //查找指定节点下的所有子节点<BR> function operChildrenNodes(treeNode,nodes){<BR> if(treeNode.children!= undefined){//是父节点,有子节点<BR> for(var j = 0;j<treeNode.children.length;j++){<BR> var childNode = treeNode.children[j];<BR> nodes+="{id:"+childNode.id+",pId:"+childNode.pId+",name:\""+childNode.name+"\",open:"+childNode.open+"},";<BR> nodes = operChildrenNodes(childNode,nodes);<BR> }<BR> }else{//没子节点<BR> }<BR> return nodes;<BR> }<br><br> <BR> $(document).ready(function(){<BR> zTreeObj1 = $.fn.zTree.init($("#treeDemo"), setting, zNodes);<BR> zTreeObj2 = $.fn.zTree.init($("#treeDemo2"), setting);<BR> $(function() {<BR> $("#toRight").click(function() {<BR> moveNodes(zTreeObj1,zTreeObj1.getSelectedNodes()[0],zTreeObj2,leftDivStr,rightDivStr);<BR> });<BR> $("#toLeft").click(function(){<BR> moveNodes(zTreeObj2,zTreeObj2.getSelectedNodes()[0],zTreeObj1,rightDivStr,leftDivStr);<br><br> }); <BR> $("#show").click(function(){<BR> var leftDiv = new Array();<BR> var leftDivStrArray = eval(leftDivStr);<BR> for(var i = 0;i<leftDivStrArray.length;i++){<BR> leftDiv[i] = leftDivStrArray[i].id;<BR> }<BR> var rightDivStrArray = eval(rightDivStr);<BR> var rightDiv = new Array();<BR> for(var i = 0;i<rightDivStrArray.length;i++){<BR> rightDiv[i] = rightDivStrArray[i].id;<BR> }<BR> alert(leftDiv);<BR> alert(rightDiv);<br><br> }); <BR> });<BR> });<br><br> <BR> html代码: 复制代码 代码如下: >> show