dragWithOther.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <!DOCTYPE html>
  2. <HTML>
  3. <HEAD>
  4. <TITLE> ZTREE DEMO - drag with other DOM</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 MoveTest = {
  15. errorMsg: "放错了...请选择正确的类别!",
  16. curTarget: null,
  17. curTmpTarget: null,
  18. noSel: function() {
  19. try {
  20. window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
  21. } catch(e){}
  22. },
  23. dragTree2Dom: function(treeId, treeNodes) {
  24. return !treeNodes[0].isParent;
  25. },
  26. prevTree: function(treeId, treeNodes, targetNode) {
  27. return !targetNode.isParent && targetNode.parentTId == treeNodes[0].parentTId;
  28. },
  29. nextTree: function(treeId, treeNodes, targetNode) {
  30. return !targetNode.isParent && targetNode.parentTId == treeNodes[0].parentTId;
  31. },
  32. innerTree: function(treeId, treeNodes, targetNode) {
  33. return targetNode!=null && targetNode.isParent && targetNode.tId == treeNodes[0].parentTId;
  34. },
  35. dropTree2Dom: function(e, treeId, treeNodes, targetNode, moveType) {
  36. var domId = "dom_" + treeNodes[0].getParentNode().id;
  37. if (moveType == null && (domId == e.target.id || $(e.target).parents("#" + domId).length > 0)) {
  38. var zTree = $.fn.zTree.getZTreeObj("treeDemo");
  39. zTree.removeNode(treeNodes[0]);
  40. var newDom = $("span[domId=" + treeNodes[0].id + "]");
  41. if (newDom.length > 0) {
  42. newDom.removeClass("domBtn_Disabled");
  43. newDom.addClass("domBtn");
  44. } else {
  45. $("#" + domId).append("<span class='domBtn' domId='" + treeNodes[0].id + "'>" + treeNodes[0].name + "</span>");
  46. }
  47. MoveTest.updateType();
  48. } else if ( $(e.target).parents(".domBtnDiv").length > 0) {
  49. alert(MoveTest.errorMsg);
  50. }
  51. },
  52. dom2Tree: function(e, treeId, treeNode) {
  53. var target = MoveTest.curTarget, tmpTarget = MoveTest.curTmpTarget;
  54. if (!target) return;
  55. var zTree = $.fn.zTree.getZTreeObj("treeDemo"), parentNode;
  56. if (treeNode != null && treeNode.isParent && "dom_" + treeNode.id == target.parent().attr("id")) {
  57. parentNode = treeNode;
  58. } else if (treeNode != null && !treeNode.isParent && "dom_" + treeNode.getParentNode().id == target.parent().attr("id")) {
  59. parentNode = treeNode.getParentNode();
  60. }
  61. if (tmpTarget) tmpTarget.remove();
  62. if (!!parentNode) {
  63. var nodes = zTree.addNodes(parentNode, {id:target.attr("domId"), name: target.text()});
  64. zTree.selectNode(nodes[0]);
  65. } else {
  66. target.removeClass("domBtn_Disabled");
  67. target.addClass("domBtn");
  68. alert(MoveTest.errorMsg);
  69. }
  70. MoveTest.updateType();
  71. MoveTest.curTarget = null;
  72. MoveTest.curTmpTarget = null;
  73. },
  74. updateType: function() {
  75. var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
  76. nodes = zTree.getNodes();
  77. for (var i=0, l=nodes.length; i<l; i++) {
  78. var num = nodes[i].children ? nodes[i].children.length : 0;
  79. nodes[i].name = nodes[i].name.replace(/ \(.*\)/gi, "") + " (" + num + ")";
  80. zTree.updateNode(nodes[i]);
  81. }
  82. },
  83. bindDom: function() {
  84. $(".domBtnDiv").bind("mousedown", MoveTest.bindMouseDown);
  85. },
  86. bindMouseDown: function(e) {
  87. var target = e.target;
  88. if (target!=null && target.className=="domBtn") {
  89. var doc = $(document), target = $(target),
  90. docScrollTop = doc.scrollTop(),
  91. docScrollLeft = doc.scrollLeft();
  92. target.addClass("domBtn_Disabled");
  93. target.removeClass("domBtn");
  94. curDom = $("<span class='dom_tmp domBtn'>" + target.text() + "</span>");
  95. curDom.appendTo("body");
  96. curDom.css({
  97. "top": (e.clientY + docScrollTop + 3) + "px",
  98. "left": (e.clientX + docScrollLeft + 3) + "px"
  99. });
  100. MoveTest.curTarget = target;
  101. MoveTest.curTmpTarget = curDom;
  102. doc.bind("mousemove", MoveTest.bindMouseMove);
  103. doc.bind("mouseup", MoveTest.bindMouseUp);
  104. doc.bind("selectstart", MoveTest.docSelect);
  105. }
  106. if(e.preventDefault) {
  107. e.preventDefault();
  108. }
  109. },
  110. bindMouseMove: function(e) {
  111. MoveTest.noSel();
  112. var doc = $(document),
  113. docScrollTop = doc.scrollTop(),
  114. docScrollLeft = doc.scrollLeft(),
  115. tmpTarget = MoveTest.curTmpTarget;
  116. if (tmpTarget) {
  117. tmpTarget.css({
  118. "top": (e.clientY + docScrollTop + 3) + "px",
  119. "left": (e.clientX + docScrollLeft + 3) + "px"
  120. });
  121. }
  122. return false;
  123. },
  124. bindMouseUp: function(e) {
  125. var doc = $(document);
  126. doc.unbind("mousemove", MoveTest.bindMouseMove);
  127. doc.unbind("mouseup", MoveTest.bindMouseUp);
  128. doc.unbind("selectstart", MoveTest.docSelect);
  129. var target = MoveTest.curTarget, tmpTarget = MoveTest.curTmpTarget;
  130. if (tmpTarget) tmpTarget.remove();
  131. if ($(e.target).parents("#treeDemo").length == 0) {
  132. if (target) {
  133. target.removeClass("domBtn_Disabled");
  134. target.addClass("domBtn");
  135. }
  136. MoveTest.curTarget = null;
  137. MoveTest.curTmpTarget = null;
  138. }
  139. },
  140. bindSelect: function() {
  141. return false;
  142. }
  143. };
  144. var setting = {
  145. edit: {
  146. enable: true,
  147. showRemoveBtn: false,
  148. showRenameBtn: false,
  149. drag: {
  150. prev: MoveTest.prevTree,
  151. next: MoveTest.nextTree,
  152. inner: MoveTest.innerTree
  153. }
  154. },
  155. data: {
  156. keep: {
  157. parent: true,
  158. leaf: true
  159. },
  160. simpleData: {
  161. enable: true
  162. }
  163. },
  164. callback: {
  165. beforeDrag: MoveTest.dragTree2Dom,
  166. onDrop: MoveTest.dropTree2Dom,
  167. onMouseUp: MoveTest.dom2Tree
  168. },
  169. view: {
  170. selectedMulti: false
  171. }
  172. };
  173. var zNodes =[
  174. { id:1, pId:0, name:"植物", isParent: true, open:true},
  175. { id:2, pId:0, name:"动物", isParent: true, open:true},
  176. { id:20, pId:2, name:"大象"},
  177. { id:29, pId:2, name:"鲨鱼"},
  178. { id:10, pId:1, name:"大白菜"},
  179. { id:19, pId:1, name:"西红柿"}
  180. ];
  181. $(document).ready(function(){
  182. $.fn.zTree.init($("#treeDemo"), setting, zNodes);
  183. MoveTest.updateType();
  184. MoveTest.bindDom();
  185. });
  186. //-->
  187. </SCRIPT>
  188. <style type="text/css">
  189. .dom_line {margin:2px;border-bottom:1px gray dotted;height:1px}
  190. .domBtnDiv {display:block;padding:2px;border:1px gray dotted;background-color:powderblue}
  191. .categoryDiv {display:inline-block; width:335px}
  192. .domBtn {display:inline-block;cursor:pointer;padding:2px;margin:2px 10px;border:1px gray solid;background-color:#FFE6B0}
  193. .domBtn_Disabled {display:inline-block;cursor:default;padding:2px;margin:2px 10px;border:1px gray solid;background-color:#DFDFDF;color:#999999}
  194. .dom_tmp {position:absolute;font-size:12px;}
  195. </style>
  196. </HEAD>
  197. <BODY>
  198. <h1>与其他 DOM 拖拽互动</h1>
  199. <h6>[ 文件路径: super/dragWithOther.html ]</h6>
  200. <div class="content_wrap">
  201. <div class="zTreeDemoBackground left">
  202. <ul id="treeDemo" class="ztree"></ul>
  203. </div>
  204. <div class="right">
  205. <ul class="info">
  206. <li class="title"><h2>实现方法说明</h2>
  207. <ul class="list">
  208. <li>zTree v3.2 版本修正了 onDrag/onDrop 中的 event 对象,因此可以较容易的控制将节点拖拽到其他 DOM </li>
  209. <li class="highlight_red">将其他 DOM 拖拽到 zTree 需要自己制作相关的拖拽代码</li>
  210. <li class="highlight_red">这仅仅是一个最简单的演示,如果需要更炫的效果,需要制作更复杂的代码</li>
  211. </ul>
  212. </li>
  213. <li class="title"><h2>请拖拽下面内容到 树节点上</h2>
  214. <div class="domBtnDiv">
  215. <div id="dom_1" class="categoryDiv"><span class="domBtn" domId="11">大树</span><span class="domBtn" domId="12">小草</span><span class="domBtn" domId="13">花朵</span></div>
  216. <div class="dom_line"></div>
  217. <div id="dom_2" class="categoryDiv"><span class="domBtn" domId="21">老虎</span><span class="domBtn" domId="22">狗熊</span><span class="domBtn" domId="23">狮子</span></div>
  218. </div>
  219. <span class="highlight_red">也可以把二级节点拖拽到以上图层 </span>
  220. </li>
  221. </ul>
  222. </div>
  223. </div>
  224. </BODY>
  225. </HTML>