From 598ce5a1bf8978a16d796a12fe850edaf098d47e Mon Sep 17 00:00:00 2001 From: George Tan <406132614@qq.com> Date: Tue, 28 Dec 2021 18:44:51 +0800 Subject: [PATCH] feat(table component): add 'scrollTo' table action (#1538) --- src/components/Table/src/BasicTable.vue | 4 ++ src/components/Table/src/hooks/useScrollTo.ts | 55 +++++++++++++++++++ src/components/Table/src/hooks/useTable.ts | 3 + src/components/Table/src/types/table.ts | 1 + 4 files changed, 63 insertions(+) create mode 100644 src/components/Table/src/hooks/useScrollTo.ts diff --git a/src/components/Table/src/BasicTable.vue b/src/components/Table/src/BasicTable.vue index 52db7544..45111e46 100644 --- a/src/components/Table/src/BasicTable.vue +++ b/src/components/Table/src/BasicTable.vue @@ -52,6 +52,7 @@ import { useLoading } from './hooks/useLoading'; import { useRowSelection } from './hooks/useRowSelection'; import { useTableScroll } from './hooks/useTableScroll'; + import { useTableScrollTo } from './hooks/useTableScrollTo'; import { useCustomRow } from './hooks/useCustomRow'; import { useTableStyle } from './hooks/useTableStyle'; import { useTableHeader } from './hooks/useTableHeader'; @@ -186,6 +187,8 @@ getDataSourceRef, ); + const { scrollTo } = useTableScrollTo(tableElRef, getDataSourceRef); + const { customRow } = useCustomRow(getProps, { setSelectedRowKeys, getSelectRowKeys, @@ -298,6 +301,7 @@ setCacheColumnsByField, expandAll, collapseAll, + scrollTo, getSize: () => { return unref(getBindValues).size as SizeType; }, diff --git a/src/components/Table/src/hooks/useScrollTo.ts b/src/components/Table/src/hooks/useScrollTo.ts new file mode 100644 index 00000000..b368f81b --- /dev/null +++ b/src/components/Table/src/hooks/useScrollTo.ts @@ -0,0 +1,55 @@ +import type { ComputedRef, Ref } from 'vue'; +import { nextTick, unref } from 'vue'; +import { warn } from '/@/utils/log'; + +export function useTableScrollTo( + tableElRef: Ref, + getDataSourceRef: ComputedRef, +) { + let bodyEl: HTMLElement | null; + + async function findTargetRowToScroll(targetRowData: Recordable) { + const { id } = targetRowData; + const targetRowEl: HTMLElement | null | undefined = bodyEl?.querySelector( + `[data-row-key="${id}"]`, + ); + //Add a delay to get new dataSource + await nextTick(); + bodyEl?.scrollTo({ + top: targetRowEl?.offsetTop ?? 0, + behavior: 'smooth', + }); + } + + function scrollTo(pos: string): void { + const table = unref(tableElRef); + if (!table) return; + + const tableEl: Element = table.$el; + if (!tableEl) return; + + if (!bodyEl) { + bodyEl = tableEl.querySelector('.ant-table-body'); + if (!bodyEl) return; + } + + const dataSource = unref(getDataSourceRef); + if (!dataSource) return; + + // judge pos type + if (pos === 'top') { + findTargetRowToScroll(dataSource[0]); + } else if (pos === 'bottom') { + findTargetRowToScroll(dataSource[dataSource.length - 1]); + } else { + const targetRowData = dataSource.find((data) => data.id === pos); + if (targetRowData) { + findTargetRowToScroll(targetRowData); + } else { + warn(`id: ${pos} doesn't exist`); + } + } + } + + return { scrollTo }; +} diff --git a/src/components/Table/src/hooks/useTable.ts b/src/components/Table/src/hooks/useTable.ts index d6058932..7165c26e 100644 --- a/src/components/Table/src/hooks/useTable.ts +++ b/src/components/Table/src/hooks/useTable.ts @@ -155,6 +155,9 @@ export function useTable(tableProps?: Props): [ collapseAll: () => { getTableInstance().collapseAll(); }, + scrollTo: (pos: string) => { + getTableInstance().scrollTo(pos); + }, }; return [register, methods]; diff --git a/src/components/Table/src/types/table.ts b/src/components/Table/src/types/table.ts index 8fbdde26..3328881e 100644 --- a/src/components/Table/src/types/table.ts +++ b/src/components/Table/src/types/table.ts @@ -88,6 +88,7 @@ export interface TableActionType { clearSelectedRowKeys: () => void; expandAll: () => void; collapseAll: () => void; + scrollTo: (pos: string) => void; // pos: id | "top" | "bottom" getSelectRowKeys: () => string[]; deleteSelectRowByKey: (key: string) => void; setPagination: (info: Partial) => void;