Skip to content

Commit

Permalink
feat(table): add updateTableDataRecord method
Browse files Browse the repository at this point in the history
添加updateTableDataRecord以便可以根据指定的rowKey来直接更新行数据而无需reload
  • Loading branch information
mynetfan committed Jun 7, 2021
1 parent 5212ea7 commit 8e4f486
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/components/Table/src/BasicTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
getDataSourceRef,
getDataSource,
setTableData,
updateTableDataRecord,
fetch,
getRowKey,
reload,
Expand Down Expand Up @@ -265,6 +266,7 @@
deleteSelectRowByKey,
setPagination,
setTableData,
updateTableDataRecord,
redoHeight,
setSelectedRowKeys,
setColumns,
Expand Down
21 changes: 21 additions & 0 deletions src/components/Table/src/hooks/useDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -255,6 +275,7 @@ export function useDataSource(
fetch,
reload,
updateTableData,
updateTableDataRecord,
handleTableChange,
};
}
3 changes: 3 additions & 0 deletions src/components/Table/src/hooks/useTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
},
Expand Down
1 change: 1 addition & 0 deletions src/components/Table/src/types/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export interface TableActionType {
deleteSelectRowByKey: (key: string) => void;
setPagination: (info: Partial<PaginationProps>) => void;
setTableData: <T = Recordable>(values: T[]) => void;
updateTableDataRecord: (rowKey: string | number, record: Recordable) => Recordable | void;
getColumns: (opt?: GetColumnsParams) => BasicColumn[];
setColumns: (columns: BasicColumn[] | string[]) => void;
getDataSource: <T = Recordable>() => T[];
Expand Down

0 comments on commit 8e4f486

Please sign in to comment.