Skip to content

Commit 1219d04

Browse files
committed
feat: 支持在表格页编辑column字段
1 parent a80d35f commit 1219d04

5 files changed

Lines changed: 78 additions & 15 deletions

File tree

src/plugin/components/form/dialog.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div @click="handleShowDialog">
2+
<div @click.stop="handleShowDialog">
33
<slot />
44
<el-dialog
55
:visible="visible"

src/plugin/components/source-page/index.vue

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
:table-events="tablePageEvents"
1212
:table-props="tableProps"
1313
:action-column-props="actionColumnProps"
14+
:on-table-cell-form-submit="handleTableCellFormSubmit"
1415
ref="tablePage"
1516
>
1617
<template #after-filter>
@@ -49,7 +50,7 @@
4950
:columns="sourcePageFormColumns"
5051
:loading="formLoading"
5152
v-model="state.data"
52-
@submit="handleSubmit"
53+
@submit="handleFormPageSubmit"
5354
>
5455
<template v-slot:action="{ value }">
5556
<slot name="form-action" :value="value" />
@@ -285,10 +286,19 @@ export default class AdminSourcePage extends Vue {
285286
}
286287
}
287288
288-
async handleSubmit(data) {
289+
handleTableCellFormSubmit({ data, scope }) {
290+
const originData = scope.row;
291+
return this.handleSubmit({ data: { id: originData.id, ...data }, originData });
292+
}
293+
294+
handleFormPageSubmit(data) {
295+
return this.handleSubmit({ data });
296+
}
297+
298+
async handleSubmit({ data, originData = this.originData, state = this.state } = {}) {
289299
try {
290300
this.formLoading = true;
291-
const hookParams = { data, originData: this.originData, state: this.state, type: this.type, resource: this.resource, namespace: this.namespace };
301+
const hookParams = { data, originData, state, type: this.type, resource: this.resource, namespace: this.namespace };
292302
if (_.isFunction(this.onFormSubmit)) {
293303
await this.onFormSubmit(hookParams);
294304
} else {
@@ -305,7 +315,9 @@ export default class AdminSourcePage extends Vue {
305315
if (_.isFunction(this.afterSubmit)) {
306316
this.afterSubmit(hookParams, newData)
307317
} else {
308-
this.$router.push({ name: `${this.resource}.show`, params: { id: data.id } });
318+
if (this.type !== 'index') {
319+
this.$router.push({ name: `${this.resource}.show`, params: { id: data.id } });
320+
}
309321
}
310322
}
311323
} finally {

src/plugin/components/source-page/table.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
:actions="actions"
2323
:action-column-props="actionColumnProps"
2424
:default-sort="defaultSort"
25+
:on-table-cell-form-submit="onTableCellFormSubmit"
2526
@sort-change="handleSortChange"
2627
/>
2728
</div>
@@ -62,6 +63,7 @@ export default class AdminSourcePageTable extends Vue {
6263
@Prop({ type: Object, default: () => ({}) }) filterProps;
6364
@Prop({ type: Object, default: () => ({}) }) paginationProps;
6465
@Prop({ type: Object, default: () => ({}) }) tableEvents;
66+
@Prop({ type: Function, default: _.noop }) onTableCellFormSubmit;
6567
6668
filterForm = {};
6769
defaultSort = {};

src/plugin/components/table.vue

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<script>
22
import { Vue, Component, Prop } from 'vue-property-decorator';
33
import _ from 'lodash';
4+
import DialogForm from './form/dialog';
45
56
@Component
67
export default class AdminTable extends Vue {
78
@Prop({ type: Array, default: () => [] }) columns;
89
@Prop({ type: Array, default: () => [] }) value;
910
@Prop({ type: [Function, Array] }) actions;
1011
@Prop({ type: Object, default: () => ({}) }) actionColumnProps;
12+
@Prop({ type: Function, default: _.noop }) onTableCellFormSubmit;
1113
1214
get actionColumn() {
1315
const ColumnRender = require('./column-render').default;
@@ -60,24 +62,65 @@ export default class AdminTable extends Vue {
6062
));
6163
}
6264
65+
renderCellForm({ column, scope }) {
66+
const formColumn = _.isFunction(column.form) ? column.form(scope) : column.form;
67+
if (formColumn) {
68+
const formColumns = [
69+
{
70+
prop: column.prop,
71+
label: column.label,
72+
...formColumn
73+
}
74+
];
75+
const submitHandler = async (data) => {
76+
if (_.isFunction(formColumn.submitHandler)) {
77+
await formColumn.submitHandler({ data, scope })
78+
} else {
79+
await this.onTableCellFormSubmit({ data, scope });
80+
}
81+
Object.assign(scope.row, data);
82+
}
83+
return (
84+
<DialogForm
85+
columns={formColumns}
86+
value={_.pick(scope.row, column.prop)}
87+
title={column.label}
88+
submitHandler={submitHandler}
89+
label-position="top"
90+
>
91+
<el-button
92+
icon="el-icon-edit"
93+
size="mini"
94+
style="margin-left: 5px; padding: 4px"
95+
circle
96+
/>
97+
</DialogForm>
98+
)
99+
}
100+
return null;
101+
}
102+
63103
renderCell(column) {
64104
const ColumnRender = require('./column-render').default;
65105
return scope => {
66-
return <ColumnRender
67-
key={column.prop}
68-
value={_.get(scope.row, column.prop)}
69-
scope={scope}
70-
column={column}
71-
renderCell={column.renderCell}
72-
/>
106+
return (
107+
<div class="table-cell">
108+
<ColumnRender
109+
key={column.prop}
110+
value={_.get(scope.row, column.prop)}
111+
scope={scope}
112+
column={column}
113+
renderCell={column.renderCell || (() => <span>{_.get(scope, `row.${column.prop}`, '/')}</span>)}
114+
/>
115+
{this.renderCellForm({ column, scope })}
116+
</div>
117+
)
73118
}
74119
}
75120
76121
renderTableColumn(column) {
77122
const scopedSlots = {};
78-
if (column.renderCell) {
79-
scopedSlots.default = this.renderCell(column);
80-
}
123+
scopedSlots.default = this.renderCell(column);
81124
return (
82125
<el-table-column
83126
key={column.prop}

src/plugin/styles/table.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,11 @@
5353
margin-bottom: 10px;
5454
}
5555
}
56+
57+
.table-cell {
58+
display: inline-flex;
59+
align-items: center;
60+
flex-wrap: wrap;
61+
}
5662
}
5763
}

0 commit comments

Comments
 (0)