drag_super.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <!DOCTYPE html>
  2. <HTML>
  3. <HEAD>
  4. <TITLE> ZTREE DEMO - beforeDrag / onDrag / beforeDrop / onDrop</TITLE>
  5. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  6. <link rel="stylesheet" href="../../../css/demo.css" type="text/css">
  7. <link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
  8. <script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
  9. <script type="text/javascript" src="../../../js/jquery.ztree.core-3.5.js"></script>
  10. <script type="text/javascript" src="../../../js/jquery.ztree.excheck-3.5.js"></script>
  11. <script type="text/javascript" src="../../../js/jquery.ztree.exedit-3.5.js"></script>
  12. <SCRIPT type="text/javascript">
  13. <!--
  14. var setting = {
  15. edit: {
  16. drag: {
  17. autoExpandTrigger: true,
  18. prev: dropPrev,
  19. inner: dropInner,
  20. next: dropNext
  21. },
  22. enable: true,
  23. showRemoveBtn: false,
  24. showRenameBtn: false
  25. },
  26. data: {
  27. simpleData: {
  28. enable: true
  29. }
  30. },
  31. callback: {
  32. beforeDrag: beforeDrag,
  33. beforeDrop: beforeDrop,
  34. beforeDragOpen: beforeDragOpen,
  35. onDrag: onDrag,
  36. onDrop: onDrop,
  37. onExpand: onExpand
  38. }
  39. };
  40. var zNodes =[
  41. { id:1, pId:0, name:"can drag 1", open:true},
  42. { id:11, pId:1, name:"can drag 1-1"},
  43. { id:12, pId:1, name:"can drag 1-2"},
  44. { id:121, pId:12, name:"can drag 1-2-1"},
  45. { id:122, pId:12, name:"can drag 1-2-2"},
  46. { id:123, pId:12, name:"can drag 1-2-3"},
  47. { id:13, pId:1, name:"can't drag 1-3", open:true, drag:false},
  48. { id:131, pId:13, name:"can't drag 1-3-1", drag:false},
  49. { id:132, pId:13, name:"can't drag 1-3-2", drag:false},
  50. { id:132, pId:13, name:"can't drag 1-3-3", drag:false},
  51. { id:2, pId:0, name:"can't drag my child out 2", open:true, childOuter:false},
  52. { id:21, pId:2, name:"can't be parent 2-1", dropInner:false},
  53. { id:22, pId:2, name:"can't be root 2-2", dropRoot:false},
  54. { id:23, pId:2, name:"try to drag me 2-3"},
  55. { id:3, pId:0, name:"can't add/sort my child 3", open:true, childOrder:false, dropInner:false},
  56. { id:31, pId:3, name:"can drag 3-1"},
  57. { id:32, pId:3, name:"can drag 3-2"},
  58. { id:33, pId:3, name:"can drag 3-3"}
  59. ];
  60. function dropPrev(treeId, nodes, targetNode) {
  61. var pNode = targetNode.getParentNode();
  62. if (pNode && pNode.dropInner === false) {
  63. return false;
  64. } else {
  65. for (var i=0,l=curDragNodes.length; i<l; i++) {
  66. var curPNode = curDragNodes[i].getParentNode();
  67. if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
  68. return false;
  69. }
  70. }
  71. }
  72. return true;
  73. }
  74. function dropInner(treeId, nodes, targetNode) {
  75. if (targetNode && targetNode.dropInner === false) {
  76. return false;
  77. } else {
  78. for (var i=0,l=curDragNodes.length; i<l; i++) {
  79. if (!targetNode && curDragNodes[i].dropRoot === false) {
  80. return false;
  81. } else if (curDragNodes[i].parentTId && curDragNodes[i].getParentNode() !== targetNode && curDragNodes[i].getParentNode().childOuter === false) {
  82. return false;
  83. }
  84. }
  85. }
  86. return true;
  87. }
  88. function dropNext(treeId, nodes, targetNode) {
  89. var pNode = targetNode.getParentNode();
  90. if (pNode && pNode.dropInner === false) {
  91. return false;
  92. } else {
  93. for (var i=0,l=curDragNodes.length; i<l; i++) {
  94. var curPNode = curDragNodes[i].getParentNode();
  95. if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
  96. return false;
  97. }
  98. }
  99. }
  100. return true;
  101. }
  102. var log, className = "dark", curDragNodes, autoExpandNode;
  103. function beforeDrag(treeId, treeNodes) {
  104. className = (className === "dark" ? "":"dark");
  105. showLog("[ "+getTime()+" beforeDrag ]&nbsp;&nbsp;&nbsp;&nbsp; drag: " + treeNodes.length + " nodes." );
  106. for (var i=0,l=treeNodes.length; i<l; i++) {
  107. if (treeNodes[i].drag === false) {
  108. curDragNodes = null;
  109. return false;
  110. } else if (treeNodes[i].parentTId && treeNodes[i].getParentNode().childDrag === false) {
  111. curDragNodes = null;
  112. return false;
  113. }
  114. }
  115. curDragNodes = treeNodes;
  116. return true;
  117. }
  118. function beforeDragOpen(treeId, treeNode) {
  119. autoExpandNode = treeNode;
  120. return true;
  121. }
  122. function beforeDrop(treeId, treeNodes, targetNode, moveType, isCopy) {
  123. className = (className === "dark" ? "":"dark");
  124. showLog("[ "+getTime()+" beforeDrop ]&nbsp;&nbsp;&nbsp;&nbsp; moveType:" + moveType);
  125. showLog("target: " + (targetNode ? targetNode.name : "root") + " -- is "+ (isCopy==null? "cancel" : isCopy ? "copy" : "move"));
  126. return true;
  127. }
  128. function onDrag(event, treeId, treeNodes) {
  129. className = (className === "dark" ? "":"dark");
  130. showLog("[ "+getTime()+" onDrag ]&nbsp;&nbsp;&nbsp;&nbsp; drag: " + treeNodes.length + " nodes." );
  131. }
  132. function onDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {
  133. className = (className === "dark" ? "":"dark");
  134. showLog("[ "+getTime()+" onDrop ]&nbsp;&nbsp;&nbsp;&nbsp; moveType:" + moveType);
  135. showLog("target: " + (targetNode ? targetNode.name : "root") + " -- is "+ (isCopy==null? "cancel" : isCopy ? "copy" : "move"))
  136. }
  137. function onExpand(event, treeId, treeNode) {
  138. if (treeNode === autoExpandNode) {
  139. className = (className === "dark" ? "":"dark");
  140. showLog("[ "+getTime()+" onExpand ]&nbsp;&nbsp;&nbsp;&nbsp;" + treeNode.name);
  141. }
  142. }
  143. function showLog(str) {
  144. if (!log) log = $("#log");
  145. log.append("<li class='"+className+"'>"+str+"</li>");
  146. if(log.children("li").length > 8) {
  147. log.get(0).removeChild(log.children("li")[0]);
  148. }
  149. }
  150. function getTime() {
  151. var now= new Date(),
  152. h=now.getHours(),
  153. m=now.getMinutes(),
  154. s=now.getSeconds(),
  155. ms=now.getMilliseconds();
  156. return (h+":"+m+":"+s+ " " +ms);
  157. }
  158. function setTrigger() {
  159. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  160. zTree.setting.edit.drag.autoExpandTrigger = $("#callbackTrigger").attr("checked");
  161. }
  162. $(document).ready(function(){
  163. $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  164. $("#callbackTrigger").bind("change", {}, setTrigger);
  165. });
  166. //-->
  167. </SCRIPT>
  168. </HEAD>
  169. <BODY>
  170. <h1>Advanced Drag Node Operation</h1>
  171. <h6>[ File Path: exedit/drag_super.html ]</h6>
  172. <div class="content_wrap">
  173. <div class="zTreeDemoBackground left">
  174. <ul id="treeDemo" class="ztree"></ul>
  175. </div>
  176. <div class="right">
  177. <ul class="info">
  178. <li class="title"><h2>1, 'beforeDrag / onDrag'<br/>&nbsp;&nbsp;'beforeDrop / onDrop' callback function</h2>
  179. <ul class="list">
  180. <li>This Demo shows the configuration parameters to achieve a more flexible use of drag and drop rules, the operation can be compared with "Normal Drag Node Operation" example.</li>
  181. <li>This Demo default allows copy & move.</li>
  182. <li class="highlight_red">This Demo set 'setting.edit.drag.prev / inner / next' as 'function', you can dynamically set according to the node, which can not use beforeDrop callback function, the effect of drag and drop will be better.</li>
  183. <li><p><span class="highlight_red">After be familiar with these configurations, I think you can design a more flexible and rational rules!</span><br/>
  184. &nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="callbackTrigger" checked /> 拖拽节点时自动展开父节点是否触发 callback<br/>
  185. drag / drop log:<br/>
  186. <ul id="log" class="log"></ul></p></li>
  187. </ul>
  188. </li>
  189. <li class="title"><h2>2, Explanation of setting</h2>
  190. <ul class="list">
  191. <li>Same as 'Normal Drag Node Operation'</li>
  192. </ul>
  193. </li>
  194. <li class="title"><h2>3, Explanation of treeNode</h2>
  195. <ul class="list">
  196. <li>Same as 'Normal Drag Node Operation'</li>
  197. </ul>
  198. </li>
  199. </ul>
  200. </div>
  201. </div>
  202. </BODY>
  203. </HTML>