Commit 59a90877287a289f746eec97d12c2d3a1d5476b0
Committed by
GitHub
1 parent
3b6b4f73
feat(table): 添加和支持动态删除和插入数据 (#1152)
Showing
4 changed files
with
39 additions
and
0 deletions
src/components/Table/src/BasicTable.vue
... | ... | @@ -141,6 +141,8 @@ |
141 | 141 | getRawDataSource, |
142 | 142 | setTableData, |
143 | 143 | updateTableDataRecord, |
144 | + deleteTableDataRecord, | |
145 | + insertTableDataRecord, | |
144 | 146 | findTableDataRecord, |
145 | 147 | fetch, |
146 | 148 | getRowKey, |
... | ... | @@ -279,6 +281,8 @@ |
279 | 281 | setPagination, |
280 | 282 | setTableData, |
281 | 283 | updateTableDataRecord, |
284 | + deleteTableDataRecord, | |
285 | + insertTableDataRecord, | |
282 | 286 | findTableDataRecord, |
283 | 287 | redoHeight, |
284 | 288 | setSelectedRowKeys, | ... | ... |
src/components/Table/src/hooks/useDataSource.ts
... | ... | @@ -160,6 +160,31 @@ export function useDataSource( |
160 | 160 | } |
161 | 161 | } |
162 | 162 | |
163 | + function deleteTableDataRecord(record: Recordable | Recordable[]): Recordable | undefined { | |
164 | + if (!dataSourceRef.value || dataSourceRef.value.length == 0) return; | |
165 | + const records = !Array.isArray(record) ? [record] : record; | |
166 | + const recordIndex = records | |
167 | + .map((item) => dataSourceRef.value.findIndex((s) => s.key === item.key)) // 取序号 | |
168 | + .filter((item) => item !== undefined) | |
169 | + .sort((a, b) => b - a); // 从大到小排序 | |
170 | + for (const index of recordIndex) { | |
171 | + unref(dataSourceRef).splice(index, 1); | |
172 | + unref(propsRef).dataSource?.splice(index, 1); | |
173 | + } | |
174 | + setPagination({ | |
175 | + total: unref(propsRef).dataSource?.length, | |
176 | + }); | |
177 | + return unref(propsRef).dataSource; | |
178 | + } | |
179 | + | |
180 | + function insertTableDataRecord(record: Recordable, index: number): Recordable | undefined { | |
181 | + if (!dataSourceRef.value || dataSourceRef.value.length == 0) return; | |
182 | + index = index ?? dataSourceRef.value?.length; | |
183 | + unref(dataSourceRef).splice(index, 0, record); | |
184 | + unref(propsRef).dataSource?.splice(index, 0, record); | |
185 | + return unref(propsRef).dataSource; | |
186 | + } | |
187 | + | |
163 | 188 | function findTableDataRecord(rowKey: string | number) { |
164 | 189 | if (!dataSourceRef.value || dataSourceRef.value.length == 0) return; |
165 | 190 | |
... | ... | @@ -314,6 +339,8 @@ export function useDataSource( |
314 | 339 | reload, |
315 | 340 | updateTableData, |
316 | 341 | updateTableDataRecord, |
342 | + deleteTableDataRecord, | |
343 | + insertTableDataRecord, | |
317 | 344 | findTableDataRecord, |
318 | 345 | handleTableChange, |
319 | 346 | }; | ... | ... |
src/components/Table/src/hooks/useTable.ts
... | ... | @@ -122,6 +122,12 @@ export function useTable(tableProps?: Props): [ |
122 | 122 | updateTableData: (index: number, key: string, value: any) => { |
123 | 123 | return getTableInstance().updateTableData(index, key, value); |
124 | 124 | }, |
125 | + deleteTableDataRecord: (record: Recordable | Recordable[]) => { | |
126 | + return getTableInstance().deleteTableDataRecord(record); | |
127 | + }, | |
128 | + insertTableDataRecord: (record: Recordable | Recordable[], index?: number) => { | |
129 | + return getTableInstance().insertTableDataRecord(record, index); | |
130 | + }, | |
125 | 131 | updateTableDataRecord: (rowKey: string | number, record: Recordable) => { |
126 | 132 | return getTableInstance().updateTableDataRecord(rowKey, record); |
127 | 133 | }, | ... | ... |
src/components/Table/src/types/table.ts
... | ... | @@ -95,6 +95,8 @@ export interface TableActionType { |
95 | 95 | setPagination: (info: Partial<PaginationProps>) => void; |
96 | 96 | setTableData: <T = Recordable>(values: T[]) => void; |
97 | 97 | updateTableDataRecord: (rowKey: string | number, record: Recordable) => Recordable | void; |
98 | + deleteTableDataRecord: (record: Recordable | Recordable[]) => Recordable | void; | |
99 | + insertTableDataRecord: (record: Recordable, index?: number) => Recordable | void; | |
98 | 100 | findTableDataRecord: (rowKey: string | number) => Recordable | void; |
99 | 101 | getColumns: (opt?: GetColumnsParams) => BasicColumn[]; |
100 | 102 | setColumns: (columns: BasicColumn[] | string[]) => void; | ... | ... |