dragTreeTable.vue
19.1 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
<template>
<div
ref="table"
class="drag-tree-table"
:class="{ border: border !== undefined }"
>
<div class="drag-tree-table-header">
<column
v-for="(item, index) in data.columns"
:key="index"
:width="item.width"
:flex="item.flex"
:border="border === undefined ? resize : border"
:class="['align-' + item.titleAlign, 'colIndex' + index]"
>
<!-- <input
v-if="item.type == 'checkbox'"
class="checkbox"
type="checkbox"
@click="onCheckAll($event, item.onChange)"
> -->
<span v-html="item.title"> </span>
<!-- 筛选暂时去掉 -->
<!-- <span v-if="item.type ==='switch'">
<el-dropdown trigger="click" @command="handleClick">
<span class="el-dropdown-link">
| 状态<i class="iconfont"></i>
</span>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item v-for="(item,index) in statusOptions" :key="index" :command="item.value" :class="{active: isActive===index}" @click.native="relationClick(index)">{{ item.label }}</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</span> -->
<!-- <div
v-show="resize !== undefined"
class="resize-line"
@mousedown="mousedown(index, $event)"
></div> -->
</column>
</div>
<div
class="drag-tree-table-body"
:class="isDraing ? 'is-draging' : ''"
@dragover="draging"
@dragend="drop"
>
<row
v-for="(item, index) in data.lists"
:key="index"
depth="0"
:columns="data.columns"
:isdraggable="isdraggable"
:model="item"
:custom_field="custom_field"
:on-check="onSingleCheckChange"
:border="border === undefined ? resize : border"
:is-contain-children="isContainChildren"
@getList="getList"
@handleClickRow="handleClickRow"
/>
</div>
<div class="drag-line"></div>
</div>
</template>
<script>
import row from './row.vue'
import column from './column.vue'
import space from './space.vue'
import func from './func'
document.body.ondrop = function(event) {
event.preventDefault()
event.stopPropagation()
}
export default {
name: 'DragTreeTable',
components: {
row,
column
// space
},
props: {
isdraggable: {
type: Boolean,
default: true
},
data: Object,
onDrag: Function,
fixed: String | Boolean,
height: String | Number,
border: String,
onlySameLevelCanDrag: String,
hightRowChange: String,
resize: String,
beforeDragOver: Function
},
data() {
return {
dragX: 0,
dragY: 0,
dragId: '',
targetId: '',
whereInsert: '',
isDraing: false,
custom_field: {
id: 'id',
parentId: 'parentId',
sortValue: 'sortValue',
lists: 'children',
open: 'open',
checked: 'checked',
highlight: 'highlight'
// level: 'level'
},
onCheckChange: null,
isContainChildren: false,
mouse: {
status: 0,
startX: 0,
curColWidth: 0,
curIndex: 0
},
statusOptions: [
{
value: '',
label: '全部'
},
{
value: '1',
label: '启用'
},
{
value: '0',
label: '禁用'
}
],
isActive: 0
}
},
computed: {
// bodyStyle() {
// return {
// overflow:
// this.fixed !== undefined && this.fixed !=== false ? 'auto' : 'hidden',
// height:
// this.fixed !== undefined && this.fixed !=== false
// ? (this.height || 400) + 'px'
// : 'auto'
// }
// }
},
mounted() {
if (this.data.custom_field) {
this.custom_field = Object.assign(
{},
this.custom_field,
this.data.custom_field
)
}
setTimeout(() => {
this.data.columns.map((item) => {
if (item.type === 'checkbox') {
this.onCheckChange = item.onChange
this.isContainChildren = item.isContainChildren
}
})
}, 100)
window.addEventListener('mouseup', (e) => {
if (this.mouse.status) {
const curX = e.clientX
let line = document.querySelector('.drag-line')
line.style.left = '-10000px'
this.mouse.status = 0
const curWidth = this.mouse.curColWidth
const subWidth = curX - this.mouse.startX
const lastWidth = curWidth + subWidth
const cols = document.querySelectorAll(
'.colIndex' + this.mouse.curIndex
)
for (let index = 0; index < cols.length; index++) {
const element = cols[index]
element.style.width = lastWidth + 'px'
}
// 更新数据源
this.data.columns[this.mouse.curIndex].width = lastWidth
}
})
window.addEventListener('mousemove', (e) => {
if (this.mouse.status) {
const endX = e.clientX
const tableLeft = document
.querySelector('.drag-tree-table')
.getBoundingClientRect().left
let line = document.querySelector('.drag-line')
line.style.left = endX - tableLeft + 'px'
}
})
},
methods: {
draging(e) {
this.isDraing = true
if (e.pageX === this.dragX && e.pageY === this.dragY) return
this.dragX = e.pageX
this.dragY = e.clientY
this.filter(e.pageX, e.clientY)
if (e.clientY < 100) {
window.scrollTo(0, scrollY - 6)
} else if (e.clientY > document.body.clientHeight - 160) {
window.scrollTo(0, scrollY + 6)
}
},
drop(event) {
func.clearHoverStatus()
this.resetTreeData()
this.isDraing = false
if (this.targetId !== undefined) {
if (this.hightRowChange !== undefined) {
this.$nextTick(() => {
let rowEle = document.querySelector(
"[tree-id='" + window.dragId + "']"
)
rowEle.style.backgroundColor = 'rgba(64,158,255,0)'
setTimeout(() => {
rowEle.style.backgroundColor = 'rgba(64,158,255,0)'
}, 2000)
})
}
}
},
// 查找匹配的行,处理拖拽样式
filter(x, y) {
let rows = document.querySelectorAll('.tree-row')
this.targetId = undefined
const dragRect = window.dragParentNode.getBoundingClientRect()
const dragW = dragRect.left + window.dragParentNode.clientWidth
const dragH = dragRect.top + window.dragParentNode.clientHeight
if (x >= dragRect.left && x <= dragW && y >= dragRect.top && y <= dragH) {
// 当前正在拖拽原始块不允许插入
return
}
let hoverBlock
let targetId
let whereInsert = ''
for (let i = 0; i < rows.length; i++) {
const row = rows[i]
const rect = row.getBoundingClientRect()
const rx = rect.left
const ry = rect.top
const rw = row.clientWidth
const rh = row.clientHeight
if (x > rx && x < rx + rw && y > ry && y < ry + rh) {
const diffY = y - ry
const pId = row.getAttribute('tree-p-id')
// 不允许改变层级结构,只能改变上下顺序逻辑
if (
this.onlySameLevelCanDrag !== undefined &&
pId !== window.dragPId
) {
return
}
targetId = row.getAttribute('tree-id')
hoverBlock = row.children[row.children.length - 1]
let rowHeight = row.offsetHeight
if (diffY / rowHeight > 3 / 4) {
whereInsert = 'bottom'
} else if (diffY / rowHeight > 1 / 4) {
if (this.onlySameLevelCanDrag !== undefined) {
// 不允许改变层级结构,只能改变上下顺序逻辑
return
}
whereInsert = 'center'
} else {
whereInsert = 'top'
}
break
}
}
if (targetId === undefined) {
// 匹配不到清空上一个状态
func.clearHoverStatus()
let whereInsert = ''
return
}
let canDrag = true
if (this.beforeDragOver) {
const curRow = this.getItemById(this.data.lists, window.dragId)
const targetRow = this.getItemById(this.data.lists, targetId)
canDrag = this.beforeDragOver(curRow, targetRow, whereInsert)
}
if (canDrag === false) return
hoverBlock.style.display = 'block'
// let rowHeight = row.offsetHeight
if (whereInsert === 'bottom') {
if (hoverBlock.children[2].style.opacity !== '0.5') {
func.clearHoverStatus()
hoverBlock.children[2].style.opacity = 0.5
}
} else if (whereInsert === 'center') {
if (hoverBlock.children[1].style.opacity !== '0.5') {
func.clearHoverStatus()
hoverBlock.children[1].style.opacity = 0.5
}
} else {
if (hoverBlock.children[0].style.opacity !== '0.5') {
func.clearHoverStatus()
hoverBlock.children[0].style.opacity = 0.5
}
}
this.targetId = targetId
this.whereInsert = whereInsert
},
resetTreeData() {
if (this.targetId === undefined) return
const listKey = this.custom_field.lists
const parentIdKey = this.custom_field.parentId
const idKey = this.custom_field.id
// const levelKey = this.custom_field.level
const newList = []
const curList = this.data.lists
const _this = this
let curDragItem = null
let taggetItem = null
function pushData(curList, needPushList) {
for (let i = 0; i < curList.length; i++) {
const item = curList[i]
let obj = func.deepClone(item)
obj[listKey] = []
if (_this.targetId === item[idKey]) {
curDragItem = _this.getItemById(_this.data.lists, window.dragId)
taggetItem = _this.getItemById(_this.data.lists, _this.targetId)
// curDragItem[levelKey] = item[levelKey]
if (_this.whereInsert === 'top') {
curDragItem[parentIdKey] = item[parentIdKey]
needPushList.push(curDragItem)
needPushList.push(obj)
} else if (_this.whereInsert === 'center') {
curDragItem[parentIdKey] = item[idKey]
obj.open = true
obj[listKey].push(curDragItem)
needPushList.push(obj)
} else {
curDragItem[parentIdKey] = item[parentIdKey]
needPushList.push(obj)
needPushList.push(curDragItem)
}
} else {
if (window.dragId !== item[idKey]) {
needPushList.push(obj)
}
}
if (item[listKey] && item[listKey].length) {
pushData(item[listKey], obj[listKey])
}
}
}
pushData(curList, newList)
this.resetOrder(newList)
// _this.$emit('updateSave', newList, curDragItem, taggetItem)
this.onDrag(newList, curDragItem, taggetItem, _this.whereInsert)
},
// 重置所有数据的顺序order
resetOrder(list) {
const listKey = this.custom_field.lists
for (let i = 0; i < list.length; i++) {
list[i][this.custom_field.sortValue] = i
if (list[i][listKey] && list[i][listKey].length) {
this.resetOrder(list[i][listKey])
}
}
},
// 根据id获取当前行
getItemById(lists, id) {
let curItem = null
const listKey = this.custom_field.lists
const idKey = this.custom_field.id
function getchild(curList) {
for (let i = 0; i < curList.length; i++) {
let item = curList[i]
if (item[idKey] === id) {
curItem = JSON.parse(JSON.stringify(item))
break
} else if (item[listKey] && item[listKey].length) {
getchild(item[listKey])
}
}
}
getchild(lists)
return curItem
},
// 对外暴漏
DelById(id) {
const listKey = this.custom_field.lists
const orderKey = this.custom_field.sortValue
const idKey = this.custom_field.id
const newList = []
const curList = this.data.lists
function pushData(curList, needPushList) {
let order = 0
for (let i = 0; i < curList.length; i++) {
const item = curList[i]
if (item[idKey] !== id) {
let obj = func.deepClone(item)
obj[orderKey] = order
obj[listKey] = []
needPushList.push(obj)
order++
if (item[listKey] && item[listKey].length) {
pushData(item[listKey], obj[listKey])
}
}
}
}
pushData(curList, newList)
return newList
},
// 递归设置属性,只允许设置组件内置属性
deepSetAttr(key, val, list, ids) {
const listKey = this.custom_field.lists
for (let i = 0; i < list.length; i++) {
if (ids !== undefined) {
if (ids.includes(list[i][this.custom_field['id']])) {
list[i][this.custom_field[key]] = val
}
} else {
list[i][this.custom_field[key]] = val
}
if (list[i][listKey] && list[i][listKey].length) {
this.deepSetAttr(key, val, list[i][listKey], ids)
}
}
},
ZipAll(id, deep = true) {
let list = func.deepClone(this.data.lists)
this.deepSetAttr('open', false, list)
this.data.lists = list
},
OpenAll(id, deep = true) {
let list = func.deepClone(this.data.lists)
this.deepSetAttr('open', true, list)
this.data.lists = list
},
GetLevelById(id) {
let row = this.$refs.table.querySelector('[tree-id="' + id + '"]')
let level = row.getAttribute('data-level') * 1
return level
},
HighlightRow(id, isHighlight = true, deep = false) {
let list = func.deepClone(this.data.lists)
let ids = [id]
if (deep === true) {
ids = ids.concat(this.GetChildIds(id, true))
}
this.deepSetAttr('highlight', isHighlight, list, ids)
this.data.lists = list
},
AddRow(pId, data) {
const deepList = func.deepClone(this.data.lists)
let _this = this
function deep(list) {
const listKey = _this.custom_field.lists
for (let i = 0; i < list.length; i++) {
if (list[i][_this.custom_field['id']] === pId) {
list[i][_this.custom_field['open']] = true
let newRow = Object.assign({}, data)
newRow[_this.custom_field['parentId']] = pId
if (list[i][listKey]) {
newRow[_this.custom_field['order']] = list[i][listKey].length
list[i][listKey].push(newRow)
} else {
list[i][listKey] = []
newRow[_this.custom_field['order']] = 0
list[i][listKey].push(newRow)
}
}
if (list[i][listKey] && list[i][listKey].length) {
deep(list[i][listKey])
}
}
}
deep(deepList)
this.data.lists = deepList
},
EditRow(id, data) {
const deepList = func.deepClone(this.data.lists)
let _this = this
function deep(list) {
const listKey = _this.custom_field.lists
for (let i = 0; i < list.length; i++) {
if (list[i][_this.custom_field['id']] === id) {
let newRow = Object.assign({}, list[i], data)
list[i] = newRow
}
if (list[i][listKey] && list[i][listKey].length) {
deep(list[i][listKey])
}
}
}
deep(deepList)
this.data.lists = deepList
},
GetChildIds(id, deep = true) {
let ids = []
const _this = this
function getChilds(list, id) {
const listKey = _this.custom_field.lists
for (let i = 0; i < list.length; i++) {
let currentPid = ''
let pid = list[i][_this.custom_field['parentId']]
if (id === pid) {
currentPid = list[i][_this.custom_field['id']]
ids.push(currentPid)
} else {
currentPid = id
}
if (deep === true || id === currentPid) {
if (list[i][listKey] && list[i][listKey].length) {
getChilds(list[i][listKey], currentPid)
}
}
}
}
getChilds(this.data.lists, id)
return ids
},
// 全选按钮事件
onCheckAll(evt, func) {
this.setAllCheckData(this.data.lists, !!evt.target.checked)
const checkedList = this.getCheckedList(this.data.lists)
func && func(checkedList)
},
// 单个CheckBox勾选触发
onSingleCheckChange() {
const checkedList = this.getCheckedList(this.data.lists)
this.onCheckChange && this.onCheckChange(checkedList)
},
// 根据flag批量处理数据
setAllCheckData(curList, flag) {
const listKey = this.custom_field.lists
for (let i = 0; i < curList.length; i++) {
let item = curList[i]
this.$set(item, 'checked', flag)
if (item[listKey] && item[listKey].length) {
this.setAllCheckData(item[listKey], flag)
}
}
},
// 获取所有选中的行
getCheckedList(lists) {
const listKey = this.custom_field.lists
let checkedList = []
const deepList = func.deepClone(lists)
function getchild(curList) {
for (let i = 0; i < curList.length; i++) {
let item = curList[i]
if (item.checked && item.isShowCheckbox !== false) {
checkedList.push(item)
}
if (item[listKey] && item[listKey].length) {
getchild(item[listKey])
}
}
}
getchild(deepList)
return checkedList
},
mousedown(curIndex, e) {
const startX = e.target.getBoundingClientRect().x
const curColWidth = e.target.parentElement.offsetWidth
this.mouse = {
status: 1,
startX,
curIndex,
curColWidth
}
},
handleClick(command) {
this.$emit('filterHandler', command)
},
relationClick(index) {
this.isActive = index
},
getList(val) {
this.$emit('getList', val)
},
handleClickRow(val) {
this.$emit('handleClickRow', val)
}
}
}
</script>
<style lang="scss">
.drag-tree-table {
position: relative;
margin: 20px 0;
color: #303133;
border: 1px solid #f7f8fb;
// border-bottom: 0 none;
font-size: 14px;
&.border {
// border: 1px solid #eee;
// border-right: none;
}
}
.drag-line {
position: absolute;
top: 0;
left: -1000px;
height: 100%;
width: 1px;
background: #ccc;
}
.drag-tree-table-header {
display: flex;
// padding: 15px 10px;
// background: #f5f7fa;
// border-bottom: 1px solid #f7f8fb;
height: 50px;
line-height: 26px;
box-sizing: border-box;
font-weight: 600;
.align-left {
text-align: left;
}
.align-right {
text-align: right;
}
.align-center {
text-align: center;
}
.tree-column {
user-select: none;
}
}
.tree-icon-hidden {
visibility: hidden;
}
.is-draging .tree-row:hover {
background: transparent !important;
}
.tree-row {
// background-color: rgba(64, 158, 255, 0);
transition: background-color 0.5s linear;
font-size: 12px;
}
</style>