Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Table): ✨ scrollToElement 支持在非虚拟滚动的情况下使用 #4946

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/table/base-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default defineComponent({
const renderTNode = useTNodeJSX();
const tableRef = ref<HTMLDivElement>();
const tableElmRef = ref<HTMLTableElement>();
const tableBodyRef = ref<HTMLTableElement>();
const tableBodyRef = ref<InstanceType<typeof TBody>>();
const bottomContentRef = ref<HTMLDivElement>();
const tableFootHeight = ref(0);
const { classPrefix, virtualScrollClasses, tableLayoutClasses, tableBaseClass, tableColFixedClasses } =
Expand Down Expand Up @@ -326,7 +326,21 @@ export default defineComponent({
log.error('Table', `${params.key} does not exist in data, check \`rowKey\` or \`data\` please.`);
}
}
virtualConfig.scrollToElement({ ...params, index: index - 1 });
if (virtualConfig.isVirtualScroll.value) {
virtualConfig.scrollToElement({ ...params, index: index + 1 });
} else {
// 执行普通的滚动
// 获取 tbody
const el = tableBodyRef.value?.$el as HTMLElement | undefined;
const row = el?.children?.[index] as HTMLElement;
if (row) {
const { offsetTop } = row;
const scrollTop = tableContentRef.value.scrollTop;
const scrollHeight = offsetTop - scrollTop - (params.top ?? 0);
// 实现偏移量的支持
tableContentRef.value.scrollBy({ top: scrollHeight, behavior: params.behavior ?? 'auto' });
}
}
};

return {
Expand Down Expand Up @@ -665,7 +679,7 @@ export default defineComponent({
{this.showHeader && (
<THead v-slots={this.$slots} {...{ ...headProps, thWidthList: columnResizable ? this.thWidthList : {} }} />
)}
<TBody v-slots={this.$slots} {...tableBodyProps} />
<TBody v-slots={this.$slots} ref="tableBodyRef" {...tableBodyProps} />
<TFoot
v-slots={this.$slots}
rowKey={this.rowKey}
Expand Down
2 changes: 1 addition & 1 deletion src/table/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ scroll-y | `(params: { e: WheelEvent })` | 已废弃。表格内容纵向滚动
-- | -- | -- | --
refreshTable | \- | \- | 必需。全部重新渲染表格
scrollColumnIntoView | `(colKey: string)` | \- | 必需。横向滚动到指定列,呈现在可视范围内
scrollToElement | `(params: ComponentScrollToElementParams)` | \- | 必需。虚拟滚动场景,纵向滚动到指定行。示例:`scrollToElement({ index: 100, top: 80, time: 200, behavior: 'smooth' })`
scrollToElement | `(params: ComponentScrollToElementParams)` | \- | 必需。纵向滚动到指定行。示例:`scrollToElement({ index: 100, top: 80, time: 200, behavior: 'smooth' })`

### BaseTableCol

Expand Down
2 changes: 1 addition & 1 deletion src/table/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export interface BaseTableInstanceFunctions<T extends TableRowData = TableRowDat
*/
scrollColumnIntoView: (colKey: string) => void;
/**
* 虚拟滚动场景,纵向滚动到指定行。示例:`scrollToElement({ index: 100, top: 80, time: 200, behavior: 'smooth' })`
* 纵向滚动到指定行。示例:`scrollToElement({ index: 100, top: 80, time: 200, behavior: 'smooth' })`
*/
scrollToElement: (params: ComponentScrollToElementParams) => void;
}
Expand Down
Loading