Commit ee7c31db44fd8f99f0d26da368e1d82b5630f309
1 parent
a36825a6
feat(table): add `onValid` for editRow
为table的可编辑行添加校验方法
Showing
4 changed files
with
40 additions
and
11 deletions
CHANGELOG.zh_CN.md
src/components/Table/src/components/editable/EditableCell.vue
... | ... | @@ -364,13 +364,7 @@ |
364 | 364 | /* eslint-disable */ |
365 | 365 | props.record.onSubmitEdit = async () => { |
366 | 366 | if (isArray(props.record?.submitCbs)) { |
367 | - const validFns = (props.record?.validCbs || []).map((fn) => fn()); | |
368 | - | |
369 | - const res = await Promise.all(validFns); | |
370 | - | |
371 | - const pass = res.every((item) => !!item); | |
372 | - | |
373 | - if (!pass) return; | |
367 | + if (!props.record?.onValid?.()) return; | |
374 | 368 | const submitFns = props.record?.submitCbs || []; |
375 | 369 | submitFns.forEach((fn) => fn(false, false)); |
376 | 370 | table.emit?.('edit-row-end'); | ... | ... |
src/components/Table/src/components/editable/index.ts
... | ... | @@ -3,6 +3,7 @@ import type { BasicColumn } from '/@/components/Table/src/types/table'; |
3 | 3 | import { h, Ref } from 'vue'; |
4 | 4 | |
5 | 5 | import EditableCell from './EditableCell.vue'; |
6 | +import { isArray } from '/@/utils/is'; | |
6 | 7 | |
7 | 8 | interface Params { |
8 | 9 | text: string; |
... | ... | @@ -12,12 +13,23 @@ interface Params { |
12 | 13 | |
13 | 14 | export function renderEditCell(column: BasicColumn) { |
14 | 15 | return ({ text: value, record, index }: Params) => { |
16 | + record.onValid = async () => { | |
17 | + if (isArray(record?.validCbs)) { | |
18 | + const validFns = (record?.validCbs || []).map((fn) => fn()); | |
19 | + const res = await Promise.all(validFns); | |
20 | + return res.every((item) => !!item); | |
21 | + } else { | |
22 | + return false; | |
23 | + } | |
24 | + }; | |
25 | + | |
15 | 26 | record.onEdit = async (edit: boolean, submit = false) => { |
16 | 27 | if (!submit) { |
17 | 28 | record.editable = edit; |
18 | 29 | } |
19 | 30 | |
20 | 31 | if (!edit && submit) { |
32 | + if (!(await record.onValid())) return false; | |
21 | 33 | const res = await record.onSubmitEdit?.(); |
22 | 34 | if (res) { |
23 | 35 | record.editable = false; |
... | ... | @@ -44,6 +56,7 @@ export function renderEditCell(column: BasicColumn) { |
44 | 56 | export type EditRecordRow<T = Recordable> = Partial< |
45 | 57 | { |
46 | 58 | onEdit: (editable: boolean, submit?: boolean) => Promise<boolean>; |
59 | + onValid: () => Promise<boolean>; | |
47 | 60 | editable: boolean; |
48 | 61 | onCancel: Fn; |
49 | 62 | onSubmit: Fn; | ... | ... |
src/views/demo/table/EditRowTable.vue
... | ... | @@ -21,6 +21,8 @@ |
21 | 21 | |
22 | 22 | import { demoListApi } from '/@/api/demo/table'; |
23 | 23 | import { treeOptionsListApi } from '/@/api/demo/tree'; |
24 | + import { cloneDeep } from 'lodash-es'; | |
25 | + import { useMessage } from '/@/hooks/web/useMessage'; | |
24 | 26 | |
25 | 27 | const columns: BasicColumn[] = [ |
26 | 28 | { |
... | ... | @@ -162,6 +164,7 @@ |
162 | 164 | export default defineComponent({ |
163 | 165 | components: { BasicTable, TableAction }, |
164 | 166 | setup() { |
167 | + const { createMessage: msg } = useMessage(); | |
165 | 168 | const currentEditKeyRef = ref(''); |
166 | 169 | const [registerTable] = useTable({ |
167 | 170 | title: '可编辑行示例', |
... | ... | @@ -192,9 +195,26 @@ |
192 | 195 | } |
193 | 196 | |
194 | 197 | async function handleSave(record: EditRecordRow) { |
195 | - const pass = await record.onEdit?.(false, true); | |
196 | - if (pass) { | |
197 | - currentEditKeyRef.value = ''; | |
198 | + // 校验 | |
199 | + msg.loading({ content: '正在保存...', duration: 0, key: 'saving' }); | |
200 | + const valid = await record.onValid?.(); | |
201 | + if (valid) { | |
202 | + try { | |
203 | + const data = cloneDeep(record.editValueRefs); | |
204 | + console.log(data); | |
205 | + //TODO 此处将数据提交给服务器保存 | |
206 | + // ... | |
207 | + // 保存之后提交编辑状态 | |
208 | + const pass = await record.onEdit?.(false, true); | |
209 | + if (pass) { | |
210 | + currentEditKeyRef.value = ''; | |
211 | + } | |
212 | + msg.success({ content: '数据已保存', key: 'saving' }); | |
213 | + } catch (error) { | |
214 | + msg.error({ content: '保存失败', key: 'saving' }); | |
215 | + } | |
216 | + } else { | |
217 | + msg.error({ content: '请填写正确的数据', key: 'saving' }); | |
198 | 218 | } |
199 | 219 | } |
200 | 220 | ... | ... |