index.vue
9.11 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
<style>
.eagle-table {
width: 100%;
}
</style>
<template>
<div @keyup.enter="tableEditCell = {}">
<div style="padding-bottom: 10px;">
<el-button @click="handleNew" size="mini">新增</el-button>
<el-button @click="handleEdit" size="mini" v-if="!rowEditable && tableSelection.length > 0">编辑</el-button>
<el-button @click="handleSave" size="mini" type="primary" v-if="rowEditable">完成</el-button>
<el-button @click="handleCancel" size="mini" type="plain" v-if="rowNew">取消</el-button>
</div>
<el-table class="eagle-table" ref="table" :data="tableData" v-bind="{ size: 'small', ...tableProps }"
v-on="tableEvents" @cell-dblclick="onCellDblclick" @selection-change="onSelectionChange"
>
<!-- 默认表格插槽 -->
<slot name="default"></slot>
<!-- 根据配置列表生成表格列 -->
<template v-if="tableList && tableList.length > 0">
<template v-for="(item, index) in tableList">
<template v-if="bindItemVisible(item.visible)">
<!-- 如果有表格列具名插槽 -->
<slot v-if="$scopedSlots[item.keyPath.join('-')]" :name="item.keyPath.join('-')" v-bind="item"></slot>
<!-- 默认表格列渲染 -->
<el-table-column v-else v-bind="item" :prop="item.fullKey || item.key" :key="index" :min-width="minWidth || item.minWidth || item['min-width'] || (editable ? 140 : undefined)">
<template #default="{ row, column, $index }">
<cell-editable
:editable="row.$editable || (editable && (tableEditCell.index === row.$index && tableEditCell.key === (item.agentKey || item.fullKey || item.key)))"
:row="row" :item="item" @update="onCellUpdate" @done="onCellUpdateDone" @cancel="onCellUpdateCancel" :btn-visible="!row.$editable"
>
<!-- 如果有表格列值渲染具名插槽 -->
<slot v-if="$scopedSlots[`value-${item.keyPath.join('-')}`]" :name="`value-${item.keyPath.join('-')}`" v-bind="item"
:row="row" :value="$_get(row, item.fullKey)" :column="column" :index="$index"
></slot>
<!-- 如果表格列配置了值渲染参数 -->
<component v-else-if="item.render" :is="item.render.type" v-bind="item.render.props" :style="item.render.style">
<template v-if="item.render.children">{{ bindItemRenderChildren(item, { row, column, $index }) }}</template>
<template v-else>{{ $_get(row, item.fullKey) }}</template>
</component>
</cell-editable>
</template>
</el-table-column>
</template>
</template>
</template>
<!-- 已生成列表后的追加列插槽 -->
<slot name="column-append"></slot>
<!-- 末尾列插槽 -->
<slot name="column-end"></slot>
</el-table>
</div>
</template>
<script>
import { cloneDeep, get, set } from '../form-new/util';
import CellEditable from './cell-editable';
export default {
name: 'TableNew',
components: { CellEditable },
props: {
// 用于实例化本组件绑定v-model的值
value: Array,
// 配置列表
list: {
type: Array,
required: true
},
// 表格参数
tableProps: {
type: Object,
default() { return {} }
},
// 表格事件
tableEvents: Object,
// 是否可编辑
editable: Boolean,
// 列宽
minWidth: Number,
},
data() {
return {
tableList: [], // 表格配置列表
tableData: [], // 表格数据
tableRowTemplate: { $editable: true }, // 行数据模板
tableEditCell: {}, // 正在编辑的单元格
tableSelection: [], // 表格已选中
};
},
computed: {
// 表格实体
instance: {
get() {
return this.$refs.table;
}
},
// 行编辑状态
rowEditable() {
let result = false;
for (const row of this.tableData) {
if (row.$editable) {
result = true;
break;
}
}
return result;
},
// 存在新行
rowNew() {
let result = false;
for (const row of this.tableData) {
if (row.$new) {
result = true;
break;
}
}
return result;
}
},
watch: {
value: {
handler(val = []) {
this.tableData = val.map((o, i) => ({ ...o, $index: i }));
},
immediate: true
},
list: {
handler(val) {
// 深度克隆传入的列表,避免原始值被修改
const newList = cloneDeep(this.list);
// 生成列表值的全路径key,即列表项为对象时,对象内的key与上一级的key合并作为全路径key
const generateFullKey = (list, parentKey) => {
list.forEach(item => {
if (item.group && item.list) {
if (item.group.key) {
item.fullKey = `${parentKey ? `${parentKey}.${item.group.key}` : item.group.key}`;
} else {
item.fullKey = parentKey || item.key;
}
generateFullKey(item.list, item.fullKey);
} else {
item.fullKey = `${parentKey ? `${parentKey}.${item.key}` : item.key}`;
}
});
};
// 生成fullKey
generateFullKey(newList);
// 创建输出列表
const result = [];
const tableRowTemplate = {};
// 生成列表值的全路径key,即列表项为对象时,对象内的key与上一级的key合并作为全路径key
const generateFlatList = (list) => {
list.forEach(item => {
if (item.group && item.list) {
generateFlatList(item.list);
} else if (!item.group && !item.list) {
result.push({ ...item, keyPath: item.fullKey.split('.') });
set(tableRowTemplate, item.fullKey, undefined);
tableRowTemplate.$editable = true;
}
});
};
generateFlatList(newList);
this.tableList = result;
this.tableRowTemplate = tableRowTemplate;
},
immediate: true
}
},
methods: {
$_get: get,
handleNew() {
const tableData = cloneDeep(this.tableData);
tableData.push({ ...this.tableRowTemplate, $new: true });
this.tableEditCell = {};
this.emitTableData(tableData);
},
handleEdit() {
const tableData = cloneDeep(this.tableData);
const selectionIndexArr = this.tableSelection.map(i => i.$index);
tableData.forEach((r, i) => {
if (selectionIndexArr.includes(r.$index)) {
tableData[i].$editable = true;
}
});
this.tableEditCell = {};
this.emitTableData(tableData);
},
handleSave() {
const tableData = cloneDeep(this.tableData);
tableData.forEach((r, i) => {
delete tableData[i].$editable;
delete tableData[i].$new;
});
this.tableEditCell = {};
this.emitTableData(tableData);
},
handleCancel() {
let tableData = cloneDeep(this.tableData);
tableData = tableData.filter(row => !row.$new);
this.emitTableData(tableData);
},
// 更新表格数据
emitTableData(tableData) {
if (this.$listeners['input']) {
this.$emit('input', tableData);
} else {
this.tableData = tableData;
}
},
// 绑定表格列显示隐藏状态
bindItemVisible(visible = true) {
let result = visible;
if (typeof visible === 'function') {
result = visible(this.tableData);
}
return result;
},
// 处理表格项渲染时的子值
bindItemRenderChildren(item, scope) {
return item.render.children instanceof Function ? item.render.children(scope) : this.$_get(scope.row, item.fullKey)
},
// 双击单元格
onCellDblclick(row, column, cell, event) {
if (this.editable) {
this.tableEditCell = { index: row.$index, key: column.property };
}
},
// 编辑表格更新值
onCellUpdate({ oldValue, value, row, key, fullKey }) {
this.setCellValue({ value, row, key, fullKey });
},
// 编辑表格确认
onCellUpdateDone({ oldValue, value, row, key, fullKey }) {
this.tableEditCell = {};
if (this.$listeners['cell-edit']) {
const { tableData } = this.setCellValue({ value, row, key, fullKey });
if (this.$listeners['input']) {
this.$emit('input', tableData);
}
this.$emit('cell-edit', { row, key, fullKey, value });
}
},
// 表格取消编辑
onCellUpdateCancel({ oldValue, value, row, key, fullKey }) {
this.setCellValue({ value: oldValue, row, key, fullKey });
},
// 设置表格值
setCellValue({ value, row, key, fullKey }) {
const tableData = cloneDeep(this.tableData);
const tableRow = tableData[row.$index];
set(tableRow, fullKey, value);
tableData[row.$index] = tableRow;
this.$set(this.tableData, row.$index, tableRow);
return { tableData, tableRow };
},
// 表格选中
onSelectionChange(selection) {
this.tableSelection = selection;
this.$emit('selection', selection);
}
}
};
</script>