Commit 598ce5a1bf8978a16d796a12fe850edaf098d47e

Authored by George Tan
Committed by GitHub
1 parent 8c607b38

feat(table component): add 'scrollTo' table action (#1538)

src/components/Table/src/BasicTable.vue
@@ -52,6 +52,7 @@ @@ -52,6 +52,7 @@
52 import { useLoading } from './hooks/useLoading'; 52 import { useLoading } from './hooks/useLoading';
53 import { useRowSelection } from './hooks/useRowSelection'; 53 import { useRowSelection } from './hooks/useRowSelection';
54 import { useTableScroll } from './hooks/useTableScroll'; 54 import { useTableScroll } from './hooks/useTableScroll';
  55 + import { useTableScrollTo } from './hooks/useTableScrollTo';
55 import { useCustomRow } from './hooks/useCustomRow'; 56 import { useCustomRow } from './hooks/useCustomRow';
56 import { useTableStyle } from './hooks/useTableStyle'; 57 import { useTableStyle } from './hooks/useTableStyle';
57 import { useTableHeader } from './hooks/useTableHeader'; 58 import { useTableHeader } from './hooks/useTableHeader';
@@ -186,6 +187,8 @@ @@ -186,6 +187,8 @@
186 getDataSourceRef, 187 getDataSourceRef,
187 ); 188 );
188 189
  190 + const { scrollTo } = useTableScrollTo(tableElRef, getDataSourceRef);
  191 +
189 const { customRow } = useCustomRow(getProps, { 192 const { customRow } = useCustomRow(getProps, {
190 setSelectedRowKeys, 193 setSelectedRowKeys,
191 getSelectRowKeys, 194 getSelectRowKeys,
@@ -298,6 +301,7 @@ @@ -298,6 +301,7 @@
298 setCacheColumnsByField, 301 setCacheColumnsByField,
299 expandAll, 302 expandAll,
300 collapseAll, 303 collapseAll,
  304 + scrollTo,
301 getSize: () => { 305 getSize: () => {
302 return unref(getBindValues).size as SizeType; 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,6 +155,9 @@ export function useTable(tableProps?: Props): [
155 collapseAll: () => { 155 collapseAll: () => {
156 getTableInstance().collapseAll(); 156 getTableInstance().collapseAll();
157 }, 157 },
  158 + scrollTo: (pos: string) => {
  159 + getTableInstance().scrollTo(pos);
  160 + },
158 }; 161 };
159 162
160 return [register, methods]; 163 return [register, methods];
src/components/Table/src/types/table.ts
@@ -88,6 +88,7 @@ export interface TableActionType { @@ -88,6 +88,7 @@ export interface TableActionType {
88 clearSelectedRowKeys: () => void; 88 clearSelectedRowKeys: () => void;
89 expandAll: () => void; 89 expandAll: () => void;
90 collapseAll: () => void; 90 collapseAll: () => void;
  91 + scrollTo: (pos: string) => void; // pos: id | "top" | "bottom"
91 getSelectRowKeys: () => string[]; 92 getSelectRowKeys: () => string[];
92 deleteSelectRowByKey: (key: string) => void; 93 deleteSelectRowByKey: (key: string) => void;
93 setPagination: (info: Partial<PaginationProps>) => void; 94 setPagination: (info: Partial<PaginationProps>) => void;