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