Skip to content

Commit 8e4f486

Browse files
committed
feat(table): add updateTableDataRecord method
添加updateTableDataRecord以便可以根据指定的rowKey来直接更新行数据而无需reload
1 parent 5212ea7 commit 8e4f486

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

src/components/Table/src/BasicTable.vue

+2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
getDataSourceRef,
130130
getDataSource,
131131
setTableData,
132+
updateTableDataRecord,
132133
fetch,
133134
getRowKey,
134135
reload,
@@ -265,6 +266,7 @@
265266
deleteSelectRowByKey,
266267
setPagination,
267268
setTableData,
269+
updateTableDataRecord,
268270
redoHeight,
269271
setSelectedRowKeys,
270272
setColumns,

src/components/Table/src/hooks/useDataSource.ts

+21
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,26 @@ export function useDataSource(
149149
return dataSourceRef.value[index];
150150
}
151151

152+
function updateTableDataRecord(
153+
rowKey: string | number,
154+
record: Recordable
155+
): Recordable | undefined {
156+
if (!dataSourceRef.value || dataSourceRef.value.length == 0) return;
157+
const rowKeyName = unref(getRowKey);
158+
if (typeof rowKeyName !== 'string') {
159+
return;
160+
}
161+
const row = dataSourceRef.value.find(
162+
(r) => Reflect.has(r, rowKeyName as string) && r[rowKeyName as string] === rowKey
163+
);
164+
if (row) {
165+
for (const field in row) {
166+
if (Reflect.has(record, field)) row[field] = record[field];
167+
}
168+
return row;
169+
}
170+
}
171+
152172
async function fetch(opt?: FetchParams) {
153173
const { api, searchInfo, fetchSetting, beforeFetch, afterFetch, useSearchForm, pagination } =
154174
unref(propsRef);
@@ -255,6 +275,7 @@ export function useDataSource(
255275
fetch,
256276
reload,
257277
updateTableData,
278+
updateTableDataRecord,
258279
handleTableChange,
259280
};
260281
}

src/components/Table/src/hooks/useTable.ts

+3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ export function useTable(tableProps?: Props): [
120120
updateTableData: (index: number, key: string, value: any) => {
121121
return getTableInstance().updateTableData(index, key, value);
122122
},
123+
updateTableDataRecord: (rowKey: string | number, record: Recordable) => {
124+
return getTableInstance().updateTableDataRecord(rowKey, record);
125+
},
123126
getRowSelection: () => {
124127
return toRaw(getTableInstance().getRowSelection());
125128
},

src/components/Table/src/types/table.ts

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export interface TableActionType {
9494
deleteSelectRowByKey: (key: string) => void;
9595
setPagination: (info: Partial<PaginationProps>) => void;
9696
setTableData: <T = Recordable>(values: T[]) => void;
97+
updateTableDataRecord: (rowKey: string | number, record: Recordable) => Recordable | void;
9798
getColumns: (opt?: GetColumnsParams) => BasicColumn[];
9899
setColumns: (columns: BasicColumn[] | string[]) => void;
99100
getDataSource: <T = Recordable>() => T[];

0 commit comments

Comments
 (0)