This article introduces the method of "moving a node between two ps, using ztree". Friends who need it can refer to
Implementation ideas:
1. The nodes in ztree are A json Array is used as the data source, so the json string is directly manipulated and then converted into an array of json objects.
2. Then re-initialize the ztree object with the newly formed json array object.
<link rel="stylesheet" href="demo.css" type="text/css"> <link rel="stylesheet" href="zTreeStyle.css" type="text/css"> <script type="text/javascript" src="jquery-1.4.4.min.js"></script> <script type="text/javascript" src="jquery.ztree.core-3.5(1).js"></script> <script type="text/javascript"> var zTreeObj1; var zTreeObj2; var leftpStr = "["; var rightpStr = "["; var setting = { edit: { enable: false, showRemoveBtn: false, showRenameBtn: false }, data: { simpleData: { enable: true } }, callback: { //onClick : menuOnClick } }; function menuOnClick(event, treeId, treeNode, clickFlag) { } //注册toSource函数,解决ie不支持Array的toSource()方法的问题 Array.prototype.toSource = function (){ var str = "["; for(var i = 0 ;i<this.length;i++){ str+="{id:\""+this[i].id+ "\",pId:\""+this[i].pId +"\",name:\""+this[i].name +"\",isParent:\""+this[i].isParent +"\",file:\""+this[i].file +"\",icon:\""+this[i].icon +"\",open:\""+this[i].open +"\"},"; } if(this.length != 0){ str = str.substring(0, str.length-1); } str +="]"; return str; } ; //注册unique函数,去掉array中重复的对象(id相同即为同一对象) Array.prototype.unique = function (){ var r = new Array(); label:for(var i = 0, n = this.length; i < n; i++) { for(var x = 0, y = r.length; x < y; x++) { if(r[x].id == this[i].id) { continue label; } } r[r.length] = this[i]; } return r; } ; //初始数据 var zNodes =[ { id:1, pId:0, name:"父节点 1", open:true}, { id:11, pId:1, name:"叶子节点 1-1",open:true}, { id:111, pId:11, name:"叶子节点 11-1"}, { id:112, pId:11, name:"叶子节点 11-2"}, { id:12, pId:1, name:"叶子节点 1-2",open:true}, { id:121, pId:12, name:"叶子节点 12-1"}, { id:122, pId:12, name:"叶子节点 12-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"} ]; for(var i=0;i<zNodes.length;i++){ leftpStr+="{id:"+zNodes[i].id+",pId:"+zNodes[i].pId+", name:\""+zNodes[i].name+"\",open:"+zNodes[i].open+"},"; } leftpStr = leftpStr.substring(0,leftpStr.length-1); leftpStr+="]"; rightpStr += "]"; //查找被移动节点的所有父节点 function findParentNodes(treeNode, parentNodes){ parentNodes += "{id:"+treeNode.id+",pId:"+treeNode.pId+ ",name:\""+treeNode.name+"\",open:"+treeNode.open+"},"; if(treeNode != null && treeNode.getParentNode()!= null){ parentNodes =findParentNodes(treeNode.getParentNode(),parentNodes); } return parentNodes; } //移动节点 function moveNodes(zTreeFrom,treeNode,zTreeTo,pStrFrom,pStrTo){ /////////////////////////////////treeNode的所有父节点 var parentNodes ="["; if(treeNode.pId != null){ parentNodes = findParentNodes(treeNode,parentNodes); parentNodes = parentNodes.substring(0,parentNodes.length-1); } parentNodes +="]"; //alert(parentNodes); var parentNodesArray = eval(parentNodes); /////////////////////////////// var nodes = "["; nodes+= "{id:"+treeNode.id+",pId:"+treeNode.pId+", name:\""+treeNode.name+"\",open:"+treeNode.open+"},"; nodes = operChildrenNodes(treeNode,nodes); nodes = nodes.substring(0,nodes.length-1); nodes+="]"; var nodesArray = eval(nodes); var pFromArray = eval(pStrFrom); var pToArray = eval(pStrTo); for(var i = 0;i<nodesArray.length;i++){//删除节点 for(var j = 0;j<pFromArray.length;j++){ if(pFromArray[j].id == nodesArray[i].id){ pFromArray.splice(j,1); } } } pToArray = pToArray.concat(nodesArray);//增加节点 pToArray = pToArray.concat(parentNodesArray); //////////////////////去重复 pFromArray = pFromArray.unique(); pToArray = pToArray.unique(); ////////////////////////去重复 if(zTreeFrom.setting.treeId == "treeDemo"){ leftpStr = pFromArray.toSource(); rightpStr =pToArray.toSource(); $.fn.zTree.init($("#treeDemo"), setting, pFromArray); $.fn.zTree.init($("#treeDemo2"), setting,pToArray); }else{ leftpStr = pToArray.toSource(); rightpStr =pFromArray.toSource(); $.fn.zTree.init($("#treeDemo2"), setting, pFromArray); $.fn.zTree.init($("#treeDemo"), setting,pToArray); } } //查找指定节点下的所有子节点 function operChildrenNodes(treeNode,nodes){ if(treeNode.children!= undefined){//是父节点,有子节点 for(var j = 0;j<treeNode.children.length;j++){ var childNode = treeNode.children[j]; nodes+="{id:"+childNode.id+",pId:"+childNode.pId+", name:\""+childNode.name+"\",open:"+childNode.open+"},"; nodes = operChildrenNodes(childNode,nodes); } }else{//没子节点 } return nodes; } $(document).ready(function(){ zTreeObj1 = $.fn.zTree.init($("#treeDemo"), setting, zNodes); zTreeObj2 = $.fn.zTree.init($("#treeDemo2"), setting); $(function() { $("#toRight").click(function() { moveNodes(zTreeObj1,zTreeObj1.getSelectedNodes()[0], zTreeObj2,leftpStr,rightpStr); }); $("#toLeft").click(function(){ moveNodes(zTreeObj2,zTreeObj2.getSelectedNodes()[0], zTreeObj1,rightpStr,leftpStr); }); $("#show").click(function(){ var leftp = new Array(); var leftpStrArray = eval(leftpStr); for(var i = 0;i<leftpStrArray.length;i++){ leftp[i] = leftpStrArray[i].id; } var rightpStrArray = eval(rightpStr); var rightp = new Array(); for(var i = 0;i<rightpStrArray.length;i++){ rightp[i] = rightpStrArray[i].id; } alert(leftp); alert(rightp); }); }); }); </script>
html code:
<body style="cursor: auto;"> <p class="content_wrap"> <p class="zTreeDemoBackground left"> <ul id="treeDemo" class="ztree"></ul> </p> <button id="toRight">>></button> <button id="toLeft"><<</button> <button id="show">show</button> <p class="right"> <ul id="treeDemo2" class="ztree"></ul> </p> </p> </body>
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Introduction to LDAP query under node.js
Methods to prevent SQL injection in node-mysql
The above is the detailed content of nodejs uses ztree to move between two divs. For more information, please follow other related articles on the PHP Chinese website!

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools
