|
1 | 1 | <script> |
2 | 2 | import { Vue, Component, Prop } from 'vue-property-decorator'; |
3 | 3 | import _ from 'lodash'; |
| 4 | +import DialogForm from './form/dialog'; |
4 | 5 |
|
5 | 6 | @Component |
6 | 7 | export default class AdminTable extends Vue { |
7 | 8 | @Prop({ type: Array, default: () => [] }) columns; |
8 | 9 | @Prop({ type: Array, default: () => [] }) value; |
9 | 10 | @Prop({ type: [Function, Array] }) actions; |
10 | 11 | @Prop({ type: Object, default: () => ({}) }) actionColumnProps; |
| 12 | + @Prop({ type: Function, default: _.noop }) onTableCellFormSubmit; |
11 | 13 |
|
12 | 14 | get actionColumn() { |
13 | 15 | const ColumnRender = require('./column-render').default; |
@@ -60,24 +62,65 @@ export default class AdminTable extends Vue { |
60 | 62 | )); |
61 | 63 | } |
62 | 64 |
|
| 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 | +
|
63 | 103 | renderCell(column) { |
64 | 104 | const ColumnRender = require('./column-render').default; |
65 | 105 | 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 | + ) |
73 | 118 | } |
74 | 119 | } |
75 | 120 |
|
76 | 121 | renderTableColumn(column) { |
77 | 122 | const scopedSlots = {}; |
78 | | - if (column.renderCell) { |
79 | | - scopedSlots.default = this.renderCell(column); |
80 | | - } |
| 123 | + scopedSlots.default = this.renderCell(column); |
81 | 124 | return ( |
82 | 125 | <el-table-column |
83 | 126 | key={column.prop} |
|
0 commit comments