-
Notifications
You must be signed in to change notification settings - Fork 1
/
TreeTable.vue
380 lines (376 loc) · 10.1 KB
/
TreeTable.vue
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
<template>
<el-table
class="tree-table"
:ref="ref"
:data="data"
:row-key="rowKey"
v-bind="$attrs"
v-on="{ ...$listeners, select, 'select-all': selectAll, 'selection-change': selectionChange }"
>
<slot name="prev"></slot>
<template v-for="(column, index) in cols">
<el-table-column
v-if="column.editable || column.editableMethod"
:key="`${column.prop}-edit`"
v-bind="column"
>
<template slot-scope="{ row, $index }">
<editable-elements
v-if="
!column.editableMethod || column.editableMethod(row, column, row[column.prop], $index)
"
:model="row"
:item="{ ...column, focus: index === focusCol && $index === focusRow }"
@change="change(row, $event, column)"
></editable-elements>
<span v-else class="uneditable">{{
column.formatter
? column.formatter(row, column, row[column.prop], $index)
: row[column.prop]
}}</span>
</template>
</el-table-column>
<el-table-column v-else :key="column.prop" v-bind="column"> </el-table-column>
</template>
<slot></slot>
</el-table>
</template>
<script>
import EditableElements from './EditableElements'
export default {
name: 'TreeTable',
inheritAttrs: false,
components: { EditableElements },
props: {
props: {
type: Object,
default() {
return null
}
},
data: {
type: Array,
default() {
return []
}
},
columns: {
type: Array,
default() {
return []
}
},
rowKey: {
type: [String, Function],
default: 'id'
},
defaultCheckedKeys: {
// String 只可能是 clear, 作用为清空
type: [Array, String],
default() {
return []
}
},
currentNodeKey: {
type: [String, Number],
default: ''
},
focusRow: {
type: Number,
default: 0
},
focusCol: {
type: Number,
default: 0
},
level: {
type: Number,
default: 1
},
refreshLevel: {
type: [String, Number],
default: ''
},
checkStrictly: {
type: Boolean,
default: false
},
checkAll: {
type: Boolean,
default: true
}
},
data() {
return {
ref: 'elTable',
maxLevel: 0,
treeData: {},
children: '',
selectable: false,
timeout: null
}
},
computed: {
cols() {
return this.props
? this.columns.map(column => ({
...column,
prop: column[this.props.prop || 'prop'],
label: column[this.props.label || 'label']
}))
: this.columns
},
isEditable() {
return this.columns.some(item => item.editable || item.editableMethod)
}
},
watch: {
level: {
handler: 'expandToLevel',
immediate: true
},
refreshLevel: {
handler: 'expandToLevel'
},
data: {
async handler() {
await this.handleData()
this.setDefaultCheckedKeys()
this.setCurrentNodeKey()
this.refreshLayout()
},
deep: true,
immediate: true
},
defaultCheckedKeys: 'setDefaultCheckedKeys',
currentNodeKey: 'setCurrentNodeKey'
},
methods: {
async expandToLevel() {
if (!this.$refs[this.ref]) return
if (!this.maxLevel) {
await this.handleData()
}
let level = 0
if (this.level <= 0) {
level = this.maxLevel - 2
} else {
level = this.level - 2
}
for (const key in this.treeData) {
if (this.treeData.hasOwnProperty(key)) {
this.treeData[key].expanded = this.treeData[key].level <= level
}
}
this.$nextTick(() => {
this.$refs[this.ref].doLayout()
})
},
handleData() {
this.$nextTick(() => {
if (!this.$refs[this.ref]) return
const { treeData, selectable } = this.$refs[this.ref].store.states
this.treeData = treeData
this.selectable = selectable
const levels = Object.values(this.treeData).map(({ level }) => level)
if (levels.length) {
this.maxLevel = Math.max.apply(null, levels) + 2
} else {
this.maxLevel = 0
}
this.$emit('max-level', this.maxLevel)
this.children = this.$refs[this.ref].treeProps.children
return Promise.resolve()
})
},
// 设置默认选中
async setDefaultCheckedKeys() {
if (!this.$refs[this.ref]) return
if (this.defaultCheckedKeys === 'clear') {
this.$refs[this.ref].clearSelection()
return
}
if (!this.children) {
await this.handleData()
}
if (this.defaultCheckedKeys.length) {
let rows = []
const findChildren = arr => {
arr[this.children].forEach(item => {
rows.push(item)
if (item[this.children]) {
findChildren(item)
}
})
}
const findRows = arr => {
if (this.checkStrictly) {
arr.forEach(item => {
if (this.defaultCheckedKeys.includes(item[this.rowKey])) {
rows.push(item)
}
if (item[this.children]) {
findRows(item[this.children])
}
})
} else {
arr.forEach(item => {
if (this.defaultCheckedKeys.includes(item[this.rowKey])) {
rows.push(item)
if (item[this.children]) {
findChildren(item)
}
} else if (item[this.children]) {
findRows(item[this.children])
}
})
}
}
findRows(this.data)
rows.forEach(row => {
this.$refs[this.ref].toggleRowSelection(row, true)
})
}
},
async setCurrentNodeKey() {
if (!this.$refs[this.ref]) return
if (this.currentNodeKey === 'clear') {
this.$refs[this.ref].setCurrentRow(null)
return
}
if (!this.children) {
await this.handleData()
}
if (this.currentNodeKey) {
let row = null
const findRow = arr => {
arr.some(item => {
if (this.currentNodeKey === item[this.rowKey]) {
row = item
return true
} else if (item[this.children]) {
return findRow(item[this.children])
} else {
return false
}
})
}
findRow(this.data)
if (row) {
this.$refs[this.ref].setCurrentRow(row)
}
}
},
// 重新计算高度
refreshLayout() {
if (!this.isEditable) return
this.$nextTick(() => {
if (!this.$refs[this.ref]) return
setTimeout(() => {
this.$refs[this.ref].doLayout()
}, 200)
})
},
select(selection, row) {
if (!this.checkStrictly) {
const selected = selection.includes(row)
this.$emit('select', selection, row)
this.selectChildren(row, selected, selection)
return
}
this.$emit('select', selection, row)
},
selectAll(selection) {
if (this.checkAll) {
// tableData 第一层只要有在 selection 里面就是全选
const isSelect = this.data.some(item => selection.includes(item))
// if (isSelect) {
// selection.forEach(item => {
// this.selectChildren(item, isSelect)
// })
// } else {
// this.data.forEach(item => {
// this.selectChildren(item, isSelect)
// })
// }
// 解决父不可勾选时, 全选时子也不会勾选
this.data.forEach(item => {
this.selectChildren(item, isSelect)
})
}
this.$nextTick(() => {
this.$emit('select-all', selection)
})
},
/**
* @description: 设置子元素是否选中
* @param {row: Object} 父元素
* @param {selected: Boolean} 是否选中
* @param {selection: Array} 是否 emit selection
*/
selectChildren(row, selected, selection) {
if (row[this.children] && Array.isArray(row[this.children])) {
row[this.children].forEach(item => {
const selectable = this.selectable ? this.selectable(item) : true
selectable && this.toggleSelection(item, selected)
if (selection) {
if (selected && !selection.includes(item)) {
selection = selection.concat(item)
this.$emit('select', selection, item)
}
if (!selected && selection.includes(item)) {
selection = selection.filter(ele => ele !== item)
this.$emit('select', selection, item)
}
const result = this.selectChildren(item, selected, selection)
if (result) selection = result
} else {
this.selectChildren(item, selected, selection)
}
})
if (selection) return selection
}
},
selectionChange(selection) {
this.debounce(this.emitSelectionChange, 100, selection)
},
emitSelectionChange(selection) {
this.$emit('selection-change', selection)
},
toggleSelection(row, select) {
row &&
this.$nextTick(() => {
this.$refs[this.ref] && this.$refs[this.ref].toggleRowSelection(row, select)
})
},
cancelAll() {
this.$refs[this.ref] && this.$refs[this.ref].clearSelection()
},
// 防抖
debounce(fun, wait, params) {
clearTimeout(this.timeout)
this.timeout = setTimeout(fun, wait, params)
},
change(row, e, column) {
this.$emit('row-change', row, e, column.prop)
}
},
mounted() {
for (let key in this.$refs[this.ref]) {
if (!(key in this) && typeof this.$refs[this.ref][key] === 'function') {
this[key] = this.$refs[this.ref][key].bind(this.$refs[this.ref])
}
}
}
}
</script>
<style lang="less" scoped>
.tree-table {
/deep/ .el-table__body-wrapper {
.cell {
display: flex;
align-items: center;
}
}
}
</style>