-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
857 lines (842 loc) · 27.9 KB
/
index.ts
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/* eslint-disable no-unused-vars */
import Vue, { CreateElement, VNodeChildren, VNode } from 'vue'
import XEUtils from 'xe-utils'
import {
VXETable,
Table,
Grid,
RowInfo,
ColumnOptions,
ColumnCellRenderParams
} from 'vxe-table'
/* eslint-enable no-unused-vars */
function hasChilds (_vm: VirtualTree, row: RowInfo) {
const childList = row[_vm.treeOpts.children]
return childList && childList.length
}
function renderDefaultForm (h: CreateElement, _vm: VirtualTree) {
const { proxyConfig, proxyOpts, formData, formConfig, formOpts } = _vm
if (formConfig && formOpts.items && formOpts.items.length) {
if (!formOpts.inited) {
formOpts.inited = true
const beforeItem = proxyOpts.beforeItem
if (proxyOpts && beforeItem) {
formOpts.items.forEach((item: any) => {
beforeItem.call(_vm, { $grid: _vm, item })
})
}
}
return [
h('vxe-form', {
props: Object.assign({}, formOpts, {
data: proxyConfig && proxyOpts.form ? formData : formOpts.data
}),
on: {
submit: _vm.submitEvent,
reset: _vm.resetEvent,
'submit-invalid': _vm.submitInvalidEvent,
'toggle-collapse': _vm.togglCollapseEvent
},
ref: 'form'
})
]
}
return []
}
function getToolbarSlots (_vm: VirtualTree) {
const { $scopedSlots, toolbarOpts } = _vm
const toolbarOptSlots = toolbarOpts.slots
let $buttons
let $tools
const slots: { [key: string]: any } = {}
if (toolbarOptSlots) {
$buttons = toolbarOptSlots.buttons
$tools = toolbarOptSlots.tools
if ($buttons && $scopedSlots[$buttons]) {
$buttons = $scopedSlots[$buttons]
}
if ($tools && $scopedSlots[$tools]) {
$tools = $scopedSlots[$tools]
}
}
if ($buttons) {
slots.buttons = $buttons
}
if ($tools) {
slots.tools = $tools
}
return slots
}
function getPagerSlots (_vm: VirtualTree) {
const { $scopedSlots, pagerOpts } = _vm
const pagerOptSlots = pagerOpts.slots
const slots: { [key: string]: any } = {}
let $left
let $right
if (pagerOptSlots) {
$left = pagerOptSlots.left
$right = pagerOptSlots.right
if ($left && $scopedSlots[$left]) {
$left = $scopedSlots[$left]
}
if ($right && $scopedSlots[$right]) {
$right = $scopedSlots[$right]
}
}
if ($left) {
slots.left = $left
}
if ($right) {
slots.right = $right
}
return slots
}
function getTableOns (_vm: VirtualTree) {
const { $listeners, proxyConfig, proxyOpts } = _vm
const ons: { [key: string]: Function } = {}
XEUtils.each($listeners, (cb, type) => {
ons[type] = (...args: any[]) => {
_vm.$emit(type, ...args)
}
})
ons['checkbox-all'] = _vm.checkboxAllEvent
ons['checkbox-change'] = _vm.checkboxChangeEvent
if (proxyConfig) {
if (proxyOpts.sort) {
ons['sort-change'] = _vm.sortChangeEvent
}
if (proxyOpts.filter) {
ons['filter-change'] = _vm.filterChangeEvent
}
}
return ons
}
function errorLog (log: string, args?: any) {
console.error(args ? XEUtils.template(log, args) : log)
}
declare module 'vxe-table/lib/vxe-table' {
interface VXETableStatic {
Vue: typeof Vue;
Grid: Grid;
Table: Table;
}
}
interface VirtualTree extends Grid {
$refs: {
xTable: Table;
[key: string]: any;
};
_loadTreeData(data: RowInfo[]): Promise<any>;
handleColumns(columns: ColumnOptions[]): ColumnOptions[];
toVirtualTree(treeData: RowInfo[]): RowInfo[];
handleExpanding(row: RowInfo): RowInfo[];
handleCollapsing(row: RowInfo): RowInfo[];
[key: string]: any;
}
interface VirtualTreeOptions {
data?: (this: VirtualTree) => any;
computed?: { [key: string]: (this: VirtualTree) => any }
watch?: { [key: string]: (this: VirtualTree, ...args: any[]) => any }
created?: (this: VirtualTree) => any;
render?: (this: VirtualTree, h: CreateElement) => VNode;
methods?: { [key: string]: (this: VirtualTree, ...args: any[]) => any }
[key: string]: any;
}
function registerComponent (vxetable: typeof VXETable) {
const { setup, t } = vxetable
const GlobalConfig = setup()
const propKeys = Object.keys(vxetable.Table.props).filter(name => ['data', 'treeConfig'].indexOf(name) === -1)
const options: VirtualTreeOptions = {
name: 'VxeVirtualTree',
extends: vxetable.Grid,
data () {
return {
removeList: [],
treeLazyLoadeds: []
}
},
computed: {
treeOpts () {
return Object.assign({}, GlobalConfig.table.treeConfig, this.treeConfig)
},
checkboxOpts () {
return Object.assign({}, GlobalConfig.table.checkboxConfig, this.checkboxConfig)
},
tableExtendProps () {
let rest: { [key: string]: any } = {}
propKeys.forEach(key => {
rest[key] = this[key]
})
if (rest.checkboxConfig) {
rest.checkboxConfig = this.checkboxOpts
}
return rest
}
},
watch: {
columns (value: ColumnOptions[]) {
this.handleColumns(value)
},
data (value: any[]) {
this.loadData(value)
}
},
created () {
const { $vxe, treeOpts, data, columns } = this
Object.assign(this, {
fullTreeData: [],
treeTableData: [],
fullTreeRowMap: new Map()
})
if (this.keepSource) {
errorLog($vxe.t('vxe.error.notProp'), ['keep-source'])
}
if (treeOpts.line) {
errorLog($vxe.t('vxe.error.notProp'), ['checkbox-config.line'])
}
if (columns) {
this.handleColumns(columns)
}
if (data) {
this.reloadData(data)
}
},
render (h: CreateElement) {
const { vSize, isZMax } = this
const $scopedSlots: any = this.$scopedSlots
const hasForm = !!($scopedSlots.form || this.formConfig)
const hasToolbar = !!($scopedSlots.toolbar || this.toolbarConfig || this.toolbar)
const hasPager = !!($scopedSlots.pager || this.pagerConfig)
return h('div', {
class: ['vxe-grid', 'vxe-virtual-tree', {
[`size--${vSize}`]: vSize,
't--animat': !!this.animat,
'is--round': this.round,
'is--maximize': isZMax,
'is--loading': this.loading || this.tableLoading
}],
style: this.renderStyle
}, [
/**
* 渲染表单
*/
hasForm ? h('div', {
ref: 'formWrapper',
staticClass: 'vxe-grid--form-wrapper'
}, $scopedSlots.form
? $scopedSlots.form.call(this, { $grid: this }, h)
: renderDefaultForm(h, this)
) : null,
/**
* 渲染工具栏
*/
hasToolbar ? h('div', {
ref: 'toolbarWrapper',
class: 'vxe-grid--toolbar-wrapper'
}, $scopedSlots.toolbar
? $scopedSlots.toolbar.call(this, { $grid: this }, h)
: [
h('vxe-toolbar', {
props: this.toolbarOpts,
ref: 'xToolbar',
scopedSlots: getToolbarSlots(this)
})
]
) : null,
/**
* 渲染表格顶部区域
*/
$scopedSlots.top ? h('div', {
ref: 'topWrapper',
staticClass: 'vxe-grid--top-wrapper'
}, $scopedSlots.top.call(this, { $grid: this }, h)) : null,
/**
* 渲染表格
*/
h('vxe-table', {
props: this.tableProps,
on: getTableOns(this),
scopedSlots: $scopedSlots,
ref: 'xTable'
}, this.$slots.default),
/**
* 渲染表格底部区域
*/
$scopedSlots.bottom ? h('div', {
ref: 'bottomWrapper',
staticClass: 'vxe-grid--bottom-wrapper'
}, $scopedSlots.bottom.call(this, { $grid: this }, h)) : null,
/**
* 渲染分页
*/
hasPager ? h('div', {
ref: 'pagerWrapper',
staticClass: 'vxe-grid--pager-wrapper'
}, $scopedSlots.pager
? $scopedSlots.pager.call(this, { $grid: this }, h)
: [
h('vxe-pager', {
props: this.pagerProps,
on: {
'page-change': this.pageChangeEvent
},
scopedSlots: getPagerSlots(this)
})
]
) : null
])
},
methods: {
loadColumn (columns: ColumnOptions[]) {
return this.$nextTick().then(() => {
const { $vxe, $scopedSlots, renderTreeIcon, treeOpts } = this
XEUtils.eachTree(columns, column => {
if (column.treeNode) {
if (!column.slots) {
column.slots = {}
}
column.slots.icon = renderTreeIcon
}
if (column.slots) {
XEUtils.each(column.slots, (func, name, slots) => {
if (!XEUtils.isFunction(func)) {
if ($scopedSlots[func]) {
slots[name] = $scopedSlots[func]
} else {
slots[name] = null
errorLog($vxe.t('vxe.error.notSlot'), [func])
}
}
})
}
}, treeOpts)
this.$refs.xTable.loadColumn(columns)
})
},
renderTreeIcon (params: ColumnCellRenderParams, h: CreateElement, cellVNodes: VNodeChildren) {
const { treeLazyLoadeds, treeOpts } = this
let { isHidden, row } = params
const { children, hasChild, indent, lazy, trigger, iconLoaded, showIcon, iconOpen, iconClose } = treeOpts
let rowChilds = row[children]
let hasLazyChilds = false
let isAceived = false
let isLazyLoaded = false
let on: { [key: string]: Function } = {}
if (!isHidden) {
isAceived = row._X_EXPAND
if (lazy) {
isLazyLoaded = treeLazyLoadeds.indexOf(row) > -1
hasLazyChilds = row[hasChild]
}
}
if (!trigger || trigger === 'default') {
on.click = (evnt: Event) => this.triggerTreeExpandEvent(evnt, params)
}
return [
h('div', {
class: ['vxe-cell--tree-node', {
'is--active': isAceived
}],
style: {
paddingLeft: `${row._X_LEVEL * indent}px`
}
}, [
showIcon && ((rowChilds && rowChilds.length) || hasLazyChilds) ? [
h('div', {
class: 'vxe-tree--btn-wrapper',
on
}, [
h('i', {
class: ['vxe-tree--node-btn', isLazyLoaded ? (iconLoaded || GlobalConfig.icon.TABLE_TREE_LOADED) : (isAceived ? (iconOpen || GlobalConfig.icon.TABLE_TREE_OPEN) : (iconClose || GlobalConfig.icon.TABLE_TREE_CLOSE))]
})
])
] : null,
h('div', {
class: 'vxe-tree-cell'
}, cellVNodes)
])
]
},
_loadTreeData (data: RowInfo[]) {
const selectRow = this.getRadioRecord()
return this.$nextTick()
.then(() => this.$refs.xTable.loadData(data))
.then(() => {
if (selectRow) {
this.setRadioRow(selectRow)
}
})
},
loadData (data: any[]) {
return this._loadTreeData(this.toVirtualTree(data))
},
reloadData (data: any[]) {
return this.$nextTick()
.then(() => this.$refs.xTable.reloadData(this.toVirtualTree(data)))
.then(() => this.handleDefaultTreeExpand())
},
isTreeExpandByRow (row: RowInfo) {
return !!row._X_EXPAND
},
setTreeExpansion (rows: RowInfo | RowInfo[], expanded: boolean) {
return this.setTreeExpand(rows, expanded)
},
handleAsyncTreeExpandChilds (row: RowInfo) {
const { treeLazyLoadeds, treeOpts, checkboxOpts } = this
const { loadMethod, children } = treeOpts
const { checkStrictly } = checkboxOpts
return new Promise(resolve => {
if (loadMethod) {
treeLazyLoadeds.push(row)
loadMethod({ row }).catch(() => []).then((childs: any[]) => {
row._X_LOADED = true
XEUtils.remove(treeLazyLoadeds, item => item === row)
if (!XEUtils.isArray(childs)) {
childs = []
}
if (childs) {
row[children] = childs.map(item => {
item._X_LOADED = false
item._X_EXPAND = false
item._X_INSERT = false
item._X_LEVEL = row._X_LEVEL + 1
return item
})
if (childs.length && !row._X_EXPAND) {
this.virtualExpand(row, true)
}
// 如果当前节点已选中,则展开后子节点也被选中
if (!checkStrictly && this.isCheckedByCheckboxRow(row)) {
this.setCheckboxRow(childs, true)
}
}
resolve(this.$nextTick().then(() => this.recalculate()))
})
} else {
resolve()
}
})
},
setTreeExpand (rows: any, expanded: boolean) {
const { treeLazyLoadeds, treeOpts, tableFullData, treeNodeColumn } = this
const { lazy, hasChild, accordion, toggleMethod } = treeOpts
const result: any[] = []
if (rows) {
if (!XEUtils.isArray(rows)) {
rows = [rows]
}
const columnIndex = this.getColumnIndex(treeNodeColumn)
const $columnIndex = this.getVMColumnIndex(treeNodeColumn)
let validRows = toggleMethod ? rows.filter((row: RowInfo) => toggleMethod({ expanded, column: treeNodeColumn, row, columnIndex, $columnIndex })) : rows
if (accordion) {
validRows = validRows.length ? [validRows[validRows.length - 1]] : []
// 同一级只能展开一个
const matchObj = XEUtils.findTree(tableFullData, item => item === rows[0], treeOpts)
if (matchObj) {
matchObj.items.forEach(row => {
row._X_EXPAND = false
})
}
}
validRows.forEach((row: any) => {
const isLoad = lazy && row[hasChild] && !row._X_LOADED && treeLazyLoadeds.indexOf(row) === -1
// 是否使用懒加载
if (expanded && isLoad) {
result.push(this.handleAsyncTreeExpandChilds(row))
} else {
if (hasChilds(this, row)) {
this.virtualExpand(row, !!expanded)
}
}
})
return Promise.all(result).then(() => {
this._loadTreeData(this.treeTableData)
return this.recalculate()
})
}
return this.$nextTick()
},
setAllTreeExpansion (expanded: boolean) {
return this.setAllTreeExpand(expanded)
},
setAllTreeExpand (expanded: boolean) {
return this._loadTreeData(this.virtualAllExpand(expanded))
},
toggleTreeExpansion (row: RowInfo) {
return this.toggleTreeExpand(row)
},
triggerTreeExpandEvent (evnt: Event, params: ColumnCellRenderParams) {
const { treeOpts, treeLazyLoadeds } = this
const { row, column } = params
const { lazy } = treeOpts
if (!lazy || treeLazyLoadeds.indexOf(row) === -1) {
const expanded = !this.isTreeExpandByRow(row)
this.setTreeExpand(row, expanded)
this.$emit('toggle-tree-expand', { expanded, column, row, $event: evnt })
}
},
toggleTreeExpand (row: RowInfo) {
return this._loadTreeData(this.virtualExpand(row, !row._X_EXPAND))
},
getTreeExpandRecords () {
const { fullTreeData, treeOpts } = this
const treeExpandRecords: RowInfo[] = []
XEUtils.eachTree(fullTreeData, row => {
if (row._X_EXPAND && hasChilds(this, row)) {
treeExpandRecords.push(row)
}
}, treeOpts)
return treeExpandRecords
},
clearTreeExpand () {
return this.setAllTreeExpand(false)
},
handleColumns (columns: ColumnOptions[]) {
const { $vxe, renderTreeIcon, checkboxOpts } = this
if (columns) {
if ((!checkboxOpts.checkField || !checkboxOpts.halfField) && columns.some(conf => conf.type === 'checkbox')) {
errorLog($vxe.t('vxe.error.reqProp'), ['table.checkbox-config.checkField | table.checkbox-config.halfField'])
return []
}
const treeNodeColumn = columns.find(conf => conf.treeNode)
if (treeNodeColumn) {
let slots = treeNodeColumn.slots || {}
slots.icon = renderTreeIcon
treeNodeColumn.slots = slots
this.treeNodeColumn = treeNodeColumn
}
return columns
}
return []
},
/**
* 获取表格数据集,包含新增、删除
* 不支持修改
*/
getRecordset () {
return {
insertRecords: this.getInsertRecords(),
removeRecords: this.getRemoveRecords(),
updateRecords: []
}
},
isInsertByRow (row: RowInfo) {
return !!row._X_INSERT
},
getInsertRecords () {
const { treeOpts } = this
const insertRecords: RowInfo[] = []
XEUtils.eachTree(this.fullTreeData, row => {
if (row._X_INSERT) {
insertRecords.push(row)
}
}, treeOpts)
return insertRecords
},
insert (records: RowInfo | RowInfo[]) {
return this.insertAt(records, null)
},
/**
* 支持任意层级插入与删除
*/
insertAt (records: any, row: number | RowInfo | null) {
const { fullTreeData, treeTableData, treeOpts } = this
if (!XEUtils.isArray(records)) {
records = [records]
}
let newRecords = records.map((record: any) => this.defineField(Object.assign({
_X_LOADED: false,
_X_EXPAND: false,
_X_INSERT: true,
_X_LEVEL: 0
}, record)))
if (!row) {
fullTreeData.unshift(...newRecords)
treeTableData.unshift(...newRecords)
} else {
if (row === -1) {
fullTreeData.push(...newRecords)
treeTableData.push(...newRecords)
} else {
let matchObj = XEUtils.findTree(fullTreeData, item => item === row, treeOpts)
if (!matchObj || matchObj.index === -1) {
throw new Error(t('vxe.error.unableInsert'))
}
let { items, index, nodes } = matchObj
let rowIndex = treeTableData.indexOf(row)
if (rowIndex > -1) {
treeTableData.splice(rowIndex, 0, ...newRecords)
}
items.splice(index, 0, ...newRecords)
newRecords.forEach((item: any) => {
item._X_LEVEL = nodes.length - 1
})
}
}
return this._loadTreeData(treeTableData).then(() => {
return {
row: newRecords.length ? newRecords[newRecords.length - 1] : null,
rows: newRecords
}
})
},
/**
* 获取已删除的数据
*/
getRemoveRecords () {
return this.removeList
},
removeSelecteds () {
return this.removeCheckboxRow()
},
/**
* 删除选中数据
*/
removeCheckboxRow () {
return this.remove(this.getCheckboxRecords()).then((params: any) => {
this.clearSelection()
return params
})
},
remove (rows: any) {
const { removeList, fullTreeData, treeOpts } = this
let rest: RowInfo[] = []
if (!rows) {
rows = fullTreeData
} else if (!XEUtils.isArray(rows)) {
rows = [rows]
}
rows.forEach((row: any) => {
let matchObj = XEUtils.findTree(fullTreeData, item => item === row, treeOpts)
if (matchObj) {
const { item, items, index, parent }: any = matchObj
if (!this.isInsertByRow(row)) {
removeList.push(row)
}
if (parent) {
let isExpand = this.isTreeExpandByRow(parent)
if (isExpand) {
this.handleCollapsing(parent)
}
items.splice(index, 1)
if (isExpand) {
this.handleExpanding(parent)
}
} else {
this.handleCollapsing(item)
items.splice(index, 1)
this.treeTableData.splice(this.treeTableData.indexOf(item), 1)
}
rest.push(item)
}
})
return this._loadTreeData(this.treeTableData).then(() => {
return { row: rest.length ? rest[rest.length - 1] : null, rows: rest }
})
},
/**
* 处理默认展开树节点
*/
handleDefaultTreeExpand () {
let { treeConfig, treeOpts, tableFullData } = this
if (treeConfig) {
let { children, expandAll, expandRowKeys } = treeOpts
if (expandAll) {
this.setAllTreeExpand(true)
} else if (expandRowKeys && this.rowId) {
let rowkey = this.rowId
expandRowKeys.forEach((rowid: any) => {
let matchObj = XEUtils.findTree(tableFullData, item => rowid === XEUtils.get(item, rowkey), treeOpts)
let rowChildren = matchObj ? matchObj.item[children] : 0
if (rowChildren && rowChildren.length) {
this.setTreeExpand(matchObj.item, true)
}
})
}
}
},
/**
* 定义树属性
*/
toVirtualTree (treeData: RowInfo[]) {
const { treeOpts } = this
let fullTreeRowMap = this.fullTreeRowMap
fullTreeRowMap.clear()
XEUtils.eachTree(treeData, (item, index, items, paths, parent, nodes) => {
item._X_LOADED = false
item._X_EXPAND = false
item._X_INSERT = false
item._X_LEVEL = nodes.length - 1
fullTreeRowMap.set(item, { item, index, items, paths, parent, nodes })
}, treeOpts)
this.fullTreeData = treeData.slice(0)
this.treeTableData = treeData.slice(0)
return treeData
},
/**
* 展开/收起树节点
*/
virtualExpand (row: RowInfo, expanded: boolean) {
const { treeOpts, treeNodeColumn } = this
const { toggleMethod } = treeOpts
const columnIndex = this.getColumnIndex(treeNodeColumn)
const $columnIndex = this.getVMColumnIndex(treeNodeColumn)
if (!toggleMethod || toggleMethod({ expanded, row, column: treeNodeColumn, columnIndex, $columnIndex })) {
if (row._X_EXPAND !== expanded) {
if (row._X_EXPAND) {
this.handleCollapsing(row)
} else {
this.handleExpanding(row)
}
}
}
return this.treeTableData
},
// 展开节点
handleExpanding (row: RowInfo) {
if (hasChilds(this, row)) {
const { treeTableData, treeOpts } = this
let childRows = row[treeOpts.children]
let expandList: RowInfo[] = []
let rowIndex = treeTableData.indexOf(row)
if (rowIndex === -1) {
throw new Error('Expanding error')
}
const expandMaps: Map<RowInfo, Number> = new Map()
XEUtils.eachTree(childRows, (item, index, obj, paths, parent, nodes) => {
if (!parent || (parent._X_EXPAND && expandMaps.has(parent))) {
expandMaps.set(item, 1)
expandList.push(item)
}
}, treeOpts)
row._X_EXPAND = true
treeTableData.splice(rowIndex + 1, 0, ...expandList)
}
return this.treeTableData
},
// 收起节点
handleCollapsing (row: RowInfo) {
if (hasChilds(this, row)) {
const { treeTableData, treeOpts } = this
let childRows = row[treeOpts.children]
let nodeChildList: RowInfo[] = []
XEUtils.eachTree(childRows, item => {
nodeChildList.push(item)
}, treeOpts)
row._X_EXPAND = false
this.treeTableData = treeTableData.filter((item: any) => nodeChildList.indexOf(item) === -1)
}
return this.treeTableData
},
/**
* 展开/收起所有树节点
*/
virtualAllExpand (expanded: boolean) {
const { treeOpts } = this
if (expanded) {
const tableList: RowInfo[] = []
XEUtils.eachTree(this.fullTreeData, row => {
row._X_EXPAND = expanded
tableList.push(row)
}, treeOpts)
this.treeTableData = tableList
} else {
XEUtils.eachTree(this.fullTreeData, row => {
row._X_EXPAND = expanded
}, treeOpts)
this.treeTableData = this.fullTreeData.slice(0)
}
return this.treeTableData
},
checkboxAllEvent (params: any) {
const { checkboxOpts, treeOpts } = this
const { checkField, halfField, checkStrictly } = checkboxOpts
const { checked } = params
if (checkField && !checkStrictly) {
XEUtils.eachTree(this.fullTreeData, row => {
row[checkField] = checked
if (halfField) {
row[halfField] = false
}
}, treeOpts)
}
this.$emit('checkbox-all', params)
},
checkboxChangeEvent (params: any) {
const { checkboxOpts, treeOpts } = this
const { checkField, halfField, checkStrictly } = checkboxOpts
const { row, checked } = params
if (checkField && !checkStrictly) {
XEUtils.eachTree([row], row => {
row[checkField] = checked
if (halfField) {
row[halfField] = false
}
}, treeOpts)
this.checkParentNodeSelection(row)
}
this.$emit('checkbox-change', params)
},
checkParentNodeSelection (row: RowInfo) {
const { checkboxOpts, treeOpts } = this
const { children } = treeOpts
const { checkField, halfField, checkStrictly } = checkboxOpts
const matchObj = XEUtils.findTree(this.fullTreeData, item => item === row, treeOpts)
if (matchObj && checkField && !checkStrictly) {
const parentRow: RowInfo = matchObj.parent
if (parentRow) {
const isAll = parentRow[children].every((item: RowInfo) => item[checkField])
if (halfField && !isAll) {
parentRow[halfField] = parentRow[children].some((item: RowInfo) => item[checkField] || item[halfField])
}
parentRow[checkField] = isAll
this.checkParentNodeSelection(parentRow)
} else {
this.$refs.xTable.checkSelectionStatus()
}
}
},
getCheckboxRecords () {
const { checkboxOpts, treeOpts } = this
const { checkField } = checkboxOpts
if (checkField) {
const records: RowInfo[] = []
XEUtils.eachTree(this.fullTreeData, row => {
if (row[checkField]) {
records.push(row)
}
}, treeOpts)
return records
}
return this.$refs.xTable.getCheckboxRecords()
},
getCheckboxIndeterminateRecords () {
const { checkboxOpts, treeOpts } = this
const { halfField } = checkboxOpts
if (halfField) {
const records: RowInfo[] = []
XEUtils.eachTree(this.fullTreeData, row => {
if (row[halfField]) {
records.push(row)
}
}, treeOpts)
return records
}
return this.$refs.xTable.getCheckboxIndeterminateRecords()
}
}
}
vxetable.Vue.component(options.name, options)
}
/**
* 基于 vxe-table 表格的增强插件,实现简单的虚拟树表格
*/
export const VXETablePluginVirtualTree = {
install (vxetable: typeof VXETable) {
registerComponent(vxetable)
}
}
if (typeof window !== 'undefined' && window.VXETable && window.VXETable.use) {
window.VXETable.use(VXETablePluginVirtualTree)
}
export default VXETablePluginVirtualTree