Commit 598ce5a1bf8978a16d796a12fe850edaf098d47e
Committed by
GitHub
1 parent
8c607b38
feat(table component): add 'scrollTo' table action (#1538)
Showing
4 changed files
with
63 additions
and
0 deletions
src/components/Table/src/BasicTable.vue
... | ... | @@ -52,6 +52,7 @@ |
52 | 52 | import { useLoading } from './hooks/useLoading'; |
53 | 53 | import { useRowSelection } from './hooks/useRowSelection'; |
54 | 54 | import { useTableScroll } from './hooks/useTableScroll'; |
55 | + import { useTableScrollTo } from './hooks/useTableScrollTo'; | |
55 | 56 | import { useCustomRow } from './hooks/useCustomRow'; |
56 | 57 | import { useTableStyle } from './hooks/useTableStyle'; |
57 | 58 | import { useTableHeader } from './hooks/useTableHeader'; |
... | ... | @@ -186,6 +187,8 @@ |
186 | 187 | getDataSourceRef, |
187 | 188 | ); |
188 | 189 | |
190 | + const { scrollTo } = useTableScrollTo(tableElRef, getDataSourceRef); | |
191 | + | |
189 | 192 | const { customRow } = useCustomRow(getProps, { |
190 | 193 | setSelectedRowKeys, |
191 | 194 | getSelectRowKeys, |
... | ... | @@ -298,6 +301,7 @@ |
298 | 301 | setCacheColumnsByField, |
299 | 302 | expandAll, |
300 | 303 | collapseAll, |
304 | + scrollTo, | |
301 | 305 | getSize: () => { |
302 | 306 | return unref(getBindValues).size as SizeType; |
303 | 307 | }, | ... | ... |
src/components/Table/src/hooks/useScrollTo.ts
0 → 100644
1 | +import type { ComputedRef, Ref } from 'vue'; | |
2 | +import { nextTick, unref } from 'vue'; | |
3 | +import { warn } from '/@/utils/log'; | |
4 | + | |
5 | +export function useTableScrollTo( | |
6 | + tableElRef: Ref<ComponentRef>, | |
7 | + getDataSourceRef: ComputedRef<Recordable[]>, | |
8 | +) { | |
9 | + let bodyEl: HTMLElement | null; | |
10 | + | |
11 | + async function findTargetRowToScroll(targetRowData: Recordable) { | |
12 | + const { id } = targetRowData; | |
13 | + const targetRowEl: HTMLElement | null | undefined = bodyEl?.querySelector( | |
14 | + `[data-row-key="${id}"]`, | |
15 | + ); | |
16 | + //Add a delay to get new dataSource | |
17 | + await nextTick(); | |
18 | + bodyEl?.scrollTo({ | |
19 | + top: targetRowEl?.offsetTop ?? 0, | |
20 | + behavior: 'smooth', | |
21 | + }); | |
22 | + } | |
23 | + | |
24 | + function scrollTo(pos: string): void { | |
25 | + const table = unref(tableElRef); | |
26 | + if (!table) return; | |
27 | + | |
28 | + const tableEl: Element = table.$el; | |
29 | + if (!tableEl) return; | |
30 | + | |
31 | + if (!bodyEl) { | |
32 | + bodyEl = tableEl.querySelector('.ant-table-body'); | |
33 | + if (!bodyEl) return; | |
34 | + } | |
35 | + | |
36 | + const dataSource = unref(getDataSourceRef); | |
37 | + if (!dataSource) return; | |
38 | + | |
39 | + // judge pos type | |
40 | + if (pos === 'top') { | |
41 | + findTargetRowToScroll(dataSource[0]); | |
42 | + } else if (pos === 'bottom') { | |
43 | + findTargetRowToScroll(dataSource[dataSource.length - 1]); | |
44 | + } else { | |
45 | + const targetRowData = dataSource.find((data) => data.id === pos); | |
46 | + if (targetRowData) { | |
47 | + findTargetRowToScroll(targetRowData); | |
48 | + } else { | |
49 | + warn(`id: ${pos} doesn't exist`); | |
50 | + } | |
51 | + } | |
52 | + } | |
53 | + | |
54 | + return { scrollTo }; | |
55 | +} | ... | ... |
src/components/Table/src/hooks/useTable.ts
... | ... | @@ -155,6 +155,9 @@ export function useTable(tableProps?: Props): [ |
155 | 155 | collapseAll: () => { |
156 | 156 | getTableInstance().collapseAll(); |
157 | 157 | }, |
158 | + scrollTo: (pos: string) => { | |
159 | + getTableInstance().scrollTo(pos); | |
160 | + }, | |
158 | 161 | }; |
159 | 162 | |
160 | 163 | return [register, methods]; | ... | ... |
src/components/Table/src/types/table.ts
... | ... | @@ -88,6 +88,7 @@ export interface TableActionType { |
88 | 88 | clearSelectedRowKeys: () => void; |
89 | 89 | expandAll: () => void; |
90 | 90 | collapseAll: () => void; |
91 | + scrollTo: (pos: string) => void; // pos: id | "top" | "bottom" | |
91 | 92 | getSelectRowKeys: () => string[]; |
92 | 93 | deleteSelectRowByKey: (key: string) => void; |
93 | 94 | setPagination: (info: Partial<PaginationProps>) => void; | ... | ... |