Commit 72f953c8d3413a7f5482793258503017a81cc759
Committed by
GitHub
1 parent
d76cfd7f
fix(table): recursive updateTableDataRecord (#1024)
无刷新更新表格数据时,支持递归查找,用于树状数据时。同时新增 findTableDataRecord 函数,用于支持无刷新新增数据到树状表格中
Showing
4 changed files
with
46 additions
and
12 deletions
src/components/Table/src/BasicTable.vue
@@ -129,6 +129,7 @@ | @@ -129,6 +129,7 @@ | ||
129 | getDataSource, | 129 | getDataSource, |
130 | setTableData, | 130 | setTableData, |
131 | updateTableDataRecord, | 131 | updateTableDataRecord, |
132 | + findTableDataRecord, | ||
132 | fetch, | 133 | fetch, |
133 | getRowKey, | 134 | getRowKey, |
134 | reload, | 135 | reload, |
@@ -266,6 +267,7 @@ | @@ -266,6 +267,7 @@ | ||
266 | setPagination, | 267 | setPagination, |
267 | setTableData, | 268 | setTableData, |
268 | updateTableDataRecord, | 269 | updateTableDataRecord, |
270 | + findTableDataRecord, | ||
269 | redoHeight, | 271 | redoHeight, |
270 | setSelectedRowKeys, | 272 | setSelectedRowKeys, |
271 | setColumns, | 273 | setColumns, |
src/components/Table/src/hooks/useDataSource.ts
@@ -149,18 +149,8 @@ export function useDataSource( | @@ -149,18 +149,8 @@ export function useDataSource( | ||
149 | rowKey: string | number, | 149 | rowKey: string | number, |
150 | record: Recordable | 150 | record: Recordable |
151 | ): Recordable | undefined { | 151 | ): Recordable | undefined { |
152 | - if (!dataSourceRef.value || dataSourceRef.value.length == 0) return; | ||
153 | - const rowKeyName = unref(getRowKey); | ||
154 | - if (!rowKeyName) { | ||
155 | - return; | ||
156 | - } | ||
157 | - const row = dataSourceRef.value.find((r) => { | ||
158 | - if (typeof rowKeyName === 'function') { | ||
159 | - return (rowKeyName(r) as string) === rowKey; | ||
160 | - } else { | ||
161 | - return Reflect.has(r, rowKeyName) && r[rowKeyName] === rowKey; | ||
162 | - } | ||
163 | - }); | 152 | + const row = findTableDataRecord(rowKey); |
153 | + | ||
164 | if (row) { | 154 | if (row) { |
165 | for (const field in row) { | 155 | for (const field in row) { |
166 | if (Reflect.has(record, field)) row[field] = record[field]; | 156 | if (Reflect.has(record, field)) row[field] = record[field]; |
@@ -169,6 +159,43 @@ export function useDataSource( | @@ -169,6 +159,43 @@ export function useDataSource( | ||
169 | } | 159 | } |
170 | } | 160 | } |
171 | 161 | ||
162 | + function findTableDataRecord(rowKey: string | number) { | ||
163 | + if (!dataSourceRef.value || dataSourceRef.value.length == 0) return; | ||
164 | + | ||
165 | + const rowKeyName = unref(getRowKey); | ||
166 | + if (!rowKeyName) return; | ||
167 | + | ||
168 | + const { childrenColumnName = 'children' } = unref(propsRef); | ||
169 | + | ||
170 | + const findRow = (array: any[]) => { | ||
171 | + let ret; | ||
172 | + array.some(function iter(r) { | ||
173 | + if (typeof rowKeyName === 'function') { | ||
174 | + if ((rowKeyName(r) as string) === rowKey) { | ||
175 | + ret = r; | ||
176 | + return true; | ||
177 | + } | ||
178 | + } else { | ||
179 | + if (Reflect.has(r, rowKeyName) && r[rowKeyName] === rowKey) { | ||
180 | + ret = r; | ||
181 | + return true; | ||
182 | + } | ||
183 | + } | ||
184 | + return r[childrenColumnName] && r[childrenColumnName].some(iter); | ||
185 | + }); | ||
186 | + return ret; | ||
187 | + }; | ||
188 | + | ||
189 | + // const row = dataSourceRef.value.find(r => { | ||
190 | + // if (typeof rowKeyName === 'function') { | ||
191 | + // return (rowKeyName(r) as string) === rowKey | ||
192 | + // } else { | ||
193 | + // return Reflect.has(r, rowKeyName) && r[rowKeyName] === rowKey | ||
194 | + // } | ||
195 | + // }) | ||
196 | + return findRow(dataSourceRef.value); | ||
197 | + } | ||
198 | + | ||
172 | async function fetch(opt?: FetchParams) { | 199 | async function fetch(opt?: FetchParams) { |
173 | const { api, searchInfo, fetchSetting, beforeFetch, afterFetch, useSearchForm, pagination } = | 200 | const { api, searchInfo, fetchSetting, beforeFetch, afterFetch, useSearchForm, pagination } = |
174 | unref(propsRef); | 201 | unref(propsRef); |
@@ -280,6 +307,7 @@ export function useDataSource( | @@ -280,6 +307,7 @@ export function useDataSource( | ||
280 | reload, | 307 | reload, |
281 | updateTableData, | 308 | updateTableData, |
282 | updateTableDataRecord, | 309 | updateTableDataRecord, |
310 | + findTableDataRecord, | ||
283 | handleTableChange, | 311 | handleTableChange, |
284 | }; | 312 | }; |
285 | } | 313 | } |
src/components/Table/src/hooks/useTable.ts
@@ -122,6 +122,9 @@ export function useTable(tableProps?: Props): [ | @@ -122,6 +122,9 @@ export function useTable(tableProps?: Props): [ | ||
122 | updateTableDataRecord: (rowKey: string | number, record: Recordable) => { | 122 | updateTableDataRecord: (rowKey: string | number, record: Recordable) => { |
123 | return getTableInstance().updateTableDataRecord(rowKey, record); | 123 | return getTableInstance().updateTableDataRecord(rowKey, record); |
124 | }, | 124 | }, |
125 | + findTableDataRecord: (rowKey: string | number) => { | ||
126 | + return getTableInstance().findTableDataRecord(rowKey); | ||
127 | + }, | ||
125 | getRowSelection: () => { | 128 | getRowSelection: () => { |
126 | return toRaw(getTableInstance().getRowSelection()); | 129 | return toRaw(getTableInstance().getRowSelection()); |
127 | }, | 130 | }, |
src/components/Table/src/types/table.ts
@@ -95,6 +95,7 @@ export interface TableActionType { | @@ -95,6 +95,7 @@ export interface TableActionType { | ||
95 | setPagination: (info: Partial<PaginationProps>) => void; | 95 | setPagination: (info: Partial<PaginationProps>) => void; |
96 | setTableData: <T = Recordable>(values: T[]) => void; | 96 | setTableData: <T = Recordable>(values: T[]) => void; |
97 | updateTableDataRecord: (rowKey: string | number, record: Recordable) => Recordable | void; | 97 | updateTableDataRecord: (rowKey: string | number, record: Recordable) => Recordable | void; |
98 | + findTableDataRecord: (rowKey: string | number) => Recordable | void; | ||
98 | getColumns: (opt?: GetColumnsParams) => BasicColumn[]; | 99 | getColumns: (opt?: GetColumnsParams) => BasicColumn[]; |
99 | setColumns: (columns: BasicColumn[] | string[]) => void; | 100 | setColumns: (columns: BasicColumn[] | string[]) => void; |
100 | getDataSource: <T = Recordable>() => T[]; | 101 | getDataSource: <T = Recordable>() => T[]; |