未验证 提交 72f953c8 编写于 作者: L Leo Caan (陈栋) 提交者: GitHub

fix(table): recursive updateTableDataRecord (#1024)

无刷新更新表格数据时,支持递归查找,用于树状数据时。同时新增 findTableDataRecord 函数,用于支持无刷新新增数据到树状表格中
上级 d76cfd7f
...@@ -129,6 +129,7 @@ ...@@ -129,6 +129,7 @@
getDataSource, getDataSource,
setTableData, setTableData,
updateTableDataRecord, updateTableDataRecord,
findTableDataRecord,
fetch, fetch,
getRowKey, getRowKey,
reload, reload,
...@@ -266,6 +267,7 @@ ...@@ -266,6 +267,7 @@
setPagination, setPagination,
setTableData, setTableData,
updateTableDataRecord, updateTableDataRecord,
findTableDataRecord,
redoHeight, redoHeight,
setSelectedRowKeys, setSelectedRowKeys,
setColumns, setColumns,
......
...@@ -149,18 +149,8 @@ export function useDataSource( ...@@ -149,18 +149,8 @@ export function useDataSource(
rowKey: string | number, rowKey: string | number,
record: Recordable record: Recordable
): Recordable | undefined { ): Recordable | undefined {
if (!dataSourceRef.value || dataSourceRef.value.length == 0) return; const row = findTableDataRecord(rowKey);
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;
}
});
if (row) { if (row) {
for (const field in row) { for (const field in row) {
if (Reflect.has(record, field)) row[field] = record[field]; if (Reflect.has(record, field)) row[field] = record[field];
...@@ -169,6 +159,43 @@ export function useDataSource( ...@@ -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) { async function fetch(opt?: FetchParams) {
const { api, searchInfo, fetchSetting, beforeFetch, afterFetch, useSearchForm, pagination } = const { api, searchInfo, fetchSetting, beforeFetch, afterFetch, useSearchForm, pagination } =
unref(propsRef); unref(propsRef);
...@@ -280,6 +307,7 @@ export function useDataSource( ...@@ -280,6 +307,7 @@ export function useDataSource(
reload, reload,
updateTableData, updateTableData,
updateTableDataRecord, updateTableDataRecord,
findTableDataRecord,
handleTableChange, handleTableChange,
}; };
} }
...@@ -122,6 +122,9 @@ export function useTable(tableProps?: Props): [ ...@@ -122,6 +122,9 @@ export function useTable(tableProps?: Props): [
updateTableDataRecord: (rowKey: string | number, record: Recordable) => { updateTableDataRecord: (rowKey: string | number, record: Recordable) => {
return getTableInstance().updateTableDataRecord(rowKey, record); return getTableInstance().updateTableDataRecord(rowKey, record);
}, },
findTableDataRecord: (rowKey: string | number) => {
return getTableInstance().findTableDataRecord(rowKey);
},
getRowSelection: () => { getRowSelection: () => {
return toRaw(getTableInstance().getRowSelection()); return toRaw(getTableInstance().getRowSelection());
}, },
......
...@@ -95,6 +95,7 @@ export interface TableActionType { ...@@ -95,6 +95,7 @@ export interface TableActionType {
setPagination: (info: Partial<PaginationProps>) => void; setPagination: (info: Partial<PaginationProps>) => void;
setTableData: <T = Recordable>(values: T[]) => void; setTableData: <T = Recordable>(values: T[]) => void;
updateTableDataRecord: (rowKey: string | number, record: Recordable) => Recordable | void; updateTableDataRecord: (rowKey: string | number, record: Recordable) => Recordable | void;
findTableDataRecord: (rowKey: string | number) => Recordable | void;
getColumns: (opt?: GetColumnsParams) => BasicColumn[]; getColumns: (opt?: GetColumnsParams) => BasicColumn[];
setColumns: (columns: BasicColumn[] | string[]) => void; setColumns: (columns: BasicColumn[] | string[]) => void;
getDataSource: <T = Recordable>() => T[]; getDataSource: <T = Recordable>() => T[];
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册