Skip to content

Commit

Permalink
fix(table): recursive updateTableDataRecord (#1024)
Browse files Browse the repository at this point in the history
无刷新更新表格数据时,支持递归查找,用于树状数据时。同时新增 findTableDataRecord 函数,用于支持无刷新新增数据到树状表格中
  • Loading branch information
leocaan authored Aug 2, 2021
1 parent d76cfd7 commit 72f953c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 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 @@
getDataSource,
setTableData,
updateTableDataRecord,
findTableDataRecord,
fetch,
getRowKey,
reload,
Expand Down Expand Up @@ -266,6 +267,7 @@
setPagination,
setTableData,
updateTableDataRecord,
findTableDataRecord,
redoHeight,
setSelectedRowKeys,
setColumns,
Expand Down
52 changes: 40 additions & 12 deletions src/components/Table/src/hooks/useDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,8 @@ export function useDataSource(
rowKey: string | number,
record: Recordable
): Recordable | undefined {
if (!dataSourceRef.value || dataSourceRef.value.length == 0) return;
const rowKeyName = unref(getRowKey);
if (!rowKeyName) {
return;
}
const row = dataSourceRef.value.find((r) => {
if (typeof rowKeyName === 'function') {
return (rowKeyName(r) as string) === rowKey;
} else {
return Reflect.has(r, rowKeyName) && r[rowKeyName] === rowKey;
}
});
const row = findTableDataRecord(rowKey);

if (row) {
for (const field in row) {
if (Reflect.has(record, field)) row[field] = record[field];
Expand All @@ -169,6 +159,43 @@ export function useDataSource(
}
}

function findTableDataRecord(rowKey: string | number) {
if (!dataSourceRef.value || dataSourceRef.value.length == 0) return;

const rowKeyName = unref(getRowKey);
if (!rowKeyName) return;

const { childrenColumnName = 'children' } = unref(propsRef);

const findRow = (array: any[]) => {
let ret;
array.some(function iter(r) {
if (typeof rowKeyName === 'function') {
if ((rowKeyName(r) as string) === rowKey) {
ret = r;
return true;
}
} else {
if (Reflect.has(r, rowKeyName) && r[rowKeyName] === rowKey) {
ret = r;
return true;
}
}
return r[childrenColumnName] && r[childrenColumnName].some(iter);
});
return ret;
};

// const row = dataSourceRef.value.find(r => {
// if (typeof rowKeyName === 'function') {
// return (rowKeyName(r) as string) === rowKey
// } else {
// return Reflect.has(r, rowKeyName) && r[rowKeyName] === rowKey
// }
// })
return findRow(dataSourceRef.value);
}

async function fetch(opt?: FetchParams) {
const { api, searchInfo, fetchSetting, beforeFetch, afterFetch, useSearchForm, pagination } =
unref(propsRef);
Expand Down Expand Up @@ -280,6 +307,7 @@ export function useDataSource(
reload,
updateTableData,
updateTableDataRecord,
findTableDataRecord,
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 @@ -122,6 +122,9 @@ export function useTable(tableProps?: Props): [
updateTableDataRecord: (rowKey: string | number, record: Recordable) => {
return getTableInstance().updateTableDataRecord(rowKey, record);
},
findTableDataRecord: (rowKey: string | number) => {
return getTableInstance().findTableDataRecord(rowKey);
},
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 @@ -95,6 +95,7 @@ export interface TableActionType {
setPagination: (info: Partial<PaginationProps>) => void;
setTableData: <T = Recordable>(values: T[]) => void;
updateTableDataRecord: (rowKey: string | number, record: Recordable) => Recordable | void;
findTableDataRecord: (rowKey: string | number) => Recordable | void;
getColumns: (opt?: GetColumnsParams) => BasicColumn[];
setColumns: (columns: BasicColumn[] | string[]) => void;
getDataSource: <T = Recordable>() => T[];
Expand Down

0 comments on commit 72f953c

Please sign in to comment.