editable.vue
5.34 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
<style lang="scss">
.z-table-column__cell-editable {
display: inline-flex;
align-items: center;
justify-content: space-between;
width: 100%;
.el-icon-edit {
color: rgba(151, 151, 151, 0.5);
&:hover {
color: $primary;
}
}
.el-icon-check {
color: $green;
}
.el-icon-close {
color: $red;
}
.el-icon-edit,
.el-icon-check,
.el-icon-close {
cursor: pointer;
margin-left: 5px;
font-size: 14px;
}
}
</style>
<template>
<el-table :data="tableData | tableDataFilter" :size="tableSize" v-bind="_props" @header-click="onHeaderClick" @cell-click="onCellClick" @cell-dblclick="onCellDblclick">
<slot name="left"></slot>
<template v-for="(item, index) in columns">
<el-table-column v-bind="item" :key="index">
<slot :name="`header-${item.prop}`" slot="header"></slot>
<template #default="{ row, column }">
<cell-editor
:disabled="disabled || item.editable === false"
:editable="item.editable !== false && (row.$editable || (tableEditCell.index === row.$index && tableEditCell.prop === item.prop))"
:component="item.component"
:value="row[column.property]"
@input="onCellInput"
@edit-click="setEditCell(row, column)"
@edit-confirm="onEditConfirm"
>
<template v-if="$scopedSlots[`editor-${item.prop}`]">
<slot :name="`editor-${item.prop}`" :value="row[column.property]" :onInput="onCellInput"></slot>
</template>
</cell-editor>
</template>
</el-table-column>
</template>
<slot></slot>
<slot name="append"></slot>
</el-table>
</template>
<script>
import TableNormal from './normal';
import tableProps from './props';
import { cloneDeep, get, set } from '../utils';
export default {
name: 'TableEditable',
extends: TableNormal,
components: {
cellEditor: {
props: {
value: [String, Number, Array, Object],
component: { type: String, default: 'el-input' },
editable: Boolean,
disabled: Boolean,
},
watch: {
editable(val) {
if (val && this.component === 'el-input') {
this.$nextTick(() => {
this.$children[0] && this.$children[0].focus && this.$children[0].focus();
});
}
},
},
render(h) {
if (this.editable) {
let editorRender = [
h(this.component, {
props: { value: this.value, size: 'mini' },
on: {
input: value => {
this.$emit('input', value);
},
},
}),
];
if (this.$scopedSlots.default) {
editorRender = [this.$scopedSlots.default()];
}
if (!this.disabled) {
const handlerItems = [h('i', { attrs: { title: '确定', class: 'el-icon-check' }, on: { click: () => this.$emit('edit-confirm', this.value) } })];
// handlerItems.push(h('i', { attrs: { title: '取消', class: 'el-icon-close' }, on: { click: () => this.$emit('edit-confirm') } }));
const handler = h('span', handlerItems);
editorRender.push(handler);
}
return h('span', { class: 'z-table-column__cell-editable' }, editorRender);
}
const valueRender = [h('span', this.value)];
if (!this.disabled) {
valueRender.push(h('i', { attrs: { title: '编辑', class: 'el-icon-edit' }, on: { click: () => this.$emit('edit-click') } }));
}
return h('span', { class: 'z-table-column__cell-editable' }, valueRender);
},
},
},
props: {
value: {
type: Array,
default() {
return [];
},
},
columns: {
type: Array,
default() {
return [];
},
},
clickable: Boolean,
disabled: Boolean,
...tableProps,
},
watch: {
value(val) {
this.tableData = val || [];
},
},
data() {
return {
tableData: this.value,
tableRowTemplate: { $editable: true }, // 行数据模板
tableEditCell: {}, // 正在编辑的单元格
tableSelection: [], // 表格已选中
};
},
filters: {
tableDataFilter(value) {
return value.map((item, index) => ({ ...item, $index: index }));
},
},
methods: {
onHeaderClick() {
if (this.clickable) {
this.cancelEditCell();
}
},
onCellClick(row, column) {
if (this.clickable) {
if (row.$index !== this.tableEditCell.index || column.property !== this.tableEditCell.prop) {
this.tableEditCell = {};
}
}
},
onCellDblclick(row, column) {
if (this.clickable) {
this.setEditCell(row, column);
}
},
setEditCell(row, column) {
this.tableEditCell = { index: row.$index, prop: column.property };
},
onEditConfirm(value) {
this.$emit('cell-edit-confirm', { ...this.tableEditCell, value });
this.cancelEditCell();
},
cancelEditCell() {
this.tableEditCell = {};
},
onCellInput(value) {
const tableData = cloneDeep(this.tableData);
const tableRow = tableData[this.tableEditCell.index];
set(tableRow, this.tableEditCell.prop, value);
tableData[this.tableEditCell.index] = tableRow;
this.$set(this.tableData, this.tableEditCell.index, tableRow);
},
},
};
</script>