From 8e4f486fcf835f0b6f2a95676dba268ffdd0566e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E6=9C=A8?= Date: Tue, 8 Jun 2021 01:08:04 +0800 Subject: [PATCH] feat(table): add updateTableDataRecord method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加updateTableDataRecord以便可以根据指定的rowKey来直接更新行数据而无需reload --- src/components/Table/src/BasicTable.vue | 2 ++ .../Table/src/hooks/useDataSource.ts | 21 +++++++++++++++++++ src/components/Table/src/hooks/useTable.ts | 3 +++ src/components/Table/src/types/table.ts | 1 + 4 files changed, 27 insertions(+) diff --git a/src/components/Table/src/BasicTable.vue b/src/components/Table/src/BasicTable.vue index 546af304e..0ee35b229 100644 --- a/src/components/Table/src/BasicTable.vue +++ b/src/components/Table/src/BasicTable.vue @@ -129,6 +129,7 @@ getDataSourceRef, getDataSource, setTableData, + updateTableDataRecord, fetch, getRowKey, reload, @@ -265,6 +266,7 @@ deleteSelectRowByKey, setPagination, setTableData, + updateTableDataRecord, redoHeight, setSelectedRowKeys, setColumns, diff --git a/src/components/Table/src/hooks/useDataSource.ts b/src/components/Table/src/hooks/useDataSource.ts index 1d161abd6..6dfd15cf0 100644 --- a/src/components/Table/src/hooks/useDataSource.ts +++ b/src/components/Table/src/hooks/useDataSource.ts @@ -149,6 +149,26 @@ export function useDataSource( return dataSourceRef.value[index]; } + function updateTableDataRecord( + rowKey: string | number, + record: Recordable + ): Recordable | undefined { + if (!dataSourceRef.value || dataSourceRef.value.length == 0) return; + const rowKeyName = unref(getRowKey); + if (typeof rowKeyName !== 'string') { + return; + } + const row = dataSourceRef.value.find( + (r) => Reflect.has(r, rowKeyName as string) && r[rowKeyName as string] === rowKey + ); + if (row) { + for (const field in row) { + if (Reflect.has(record, field)) row[field] = record[field]; + } + return row; + } + } + async function fetch(opt?: FetchParams) { const { api, searchInfo, fetchSetting, beforeFetch, afterFetch, useSearchForm, pagination } = unref(propsRef); @@ -255,6 +275,7 @@ export function useDataSource( fetch, reload, updateTableData, + updateTableDataRecord, handleTableChange, }; } diff --git a/src/components/Table/src/hooks/useTable.ts b/src/components/Table/src/hooks/useTable.ts index 740420a50..59f82226d 100644 --- a/src/components/Table/src/hooks/useTable.ts +++ b/src/components/Table/src/hooks/useTable.ts @@ -120,6 +120,9 @@ export function useTable(tableProps?: Props): [ updateTableData: (index: number, key: string, value: any) => { return getTableInstance().updateTableData(index, key, value); }, + updateTableDataRecord: (rowKey: string | number, record: Recordable) => { + return getTableInstance().updateTableDataRecord(rowKey, record); + }, getRowSelection: () => { return toRaw(getTableInstance().getRowSelection()); }, diff --git a/src/components/Table/src/types/table.ts b/src/components/Table/src/types/table.ts index 9b5b217ab..09ea636da 100644 --- a/src/components/Table/src/types/table.ts +++ b/src/components/Table/src/types/table.ts @@ -94,6 +94,7 @@ export interface TableActionType { deleteSelectRowByKey: (key: string) => void; setPagination: (info: Partial) => void; setTableData: (values: T[]) => void; + updateTableDataRecord: (rowKey: string | number, record: Recordable) => Recordable | void; getColumns: (opt?: GetColumnsParams) => BasicColumn[]; setColumns: (columns: BasicColumn[] | string[]) => void; getDataSource: () => T[];