解决elementui-plus使用el-table的合计功能时横向滚动条显示在了合计上方

需求:如何在el-table使用合计功能时让横向滚动条显示在最下方

分析:其实el-table一共分为三部分:el-table__header-wrapperel-table__body-wrapperel-table__footer-wrapper滚动条是一直显示在el-table__body-wrapper上的我们只需要把放在el-table__body-wrapper上的滚动条显示在el-table__footer-wrapper上再开启滚动监听就可以了

1、先隐藏原本的滚动条并将合计的滚动条打开:

:deep(.el-table__body-wrapper .el-scrollbar__bar) {

display: none !important;

}

:deep(.el-table__footer-wrapper) {

overflow: auto;

}

2、监听el-table__footer-wrapper的滚动

在onMounted中绑定监听事件

java 复制代码
<el-table v-loading="loading"
  :data="tableData"
  :row-key="rowKey"
  show-summary
  border
  ref="multipleTableRef"></el-table>

const multipleTableRef: any = ref<HTMLElement | null>(null);

let tableFooterBody: HTMLElement | null = null;
const syncScroll = () => {
  if (!tableFooterBody) return;
  const scrollLeft = tableFooterBody.scrollLeft;
  const tableHeaderBody = multipleTableRef.value?.$el.querySelector('.el-table__header-wrapper') as HTMLElement;
  const tableBodyScrollbar = multipleTableRef.value?.$el.querySelector('.el-table__body-wrapper .el-scrollbar__wrap') as HTMLElement;

  if (tableHeaderBody) tableHeaderBody.scrollLeft = scrollLeft;
  if (tableBodyScrollbar) tableBodyScrollbar.scrollLeft = scrollLeft;
};

onMounted(() => {
  tableFooterBody = multipleTableRef.value?.$el.querySelector('.el-table__footer-wrapper') as HTMLElement;
  if (tableFooterBody) {
    tableFooterBody.addEventListener('scroll', syncScroll);
  }
});

// 卸载时移除事件防止内存泄漏
onUnmounted(() => {
  if (tableFooterBody) {
    tableFooterBody.removeEventListener('scroll', syncScroll);
  }
});

这里边注意因为el-table__footer-wrapper用的是el-scrollbar组件,所以 tableBody 实际上不是直接可滚动的元素,而是 el-scrollbar 里面的 .el-scrollbar__wrap 负责滚动。

相关推荐
胡萝卜术4 小时前
当大模型遇上浏览器:用 React + WebGPU 在前端跑通 DeepSeek-R1 的实战笔记
前端·javascript·面试
不好听6134 小时前
Tailwind CSS 原子化 CSS 完全入门:为什么现代前端开发都在用?
前端·css
触底反弹4 小时前
🔥 React 零基础入门(上):环境搭建 + JSX 深度解析
前端·react.js·typescript
why技术4 小时前
分享一套我一直在使用的 AICoding 组合拳,小而美的典范。
前端·后端·ai编程
朦胧之5 小时前
AI应用-消费流式输出
前端·javascript·ai编程
小林ixn5 小时前
在浏览器跑通 15 亿参数大模型:我用 React + WebGPU 复刻了 DeepSeek-R1
前端·react.js·前端框架
Csvn5 小时前
容器查询 @container 实战:告别无休止的媒体查询
前端
稚南城才子,乌衣巷风流7 小时前
函数:编程中的核心概念
开发语言·前端·javascript
IT_陈寒8 小时前
为什么我的JavaScript异步代码总是不按顺序执行?
前端·人工智能·后端
索西引擎8 小时前
【React】useState 状态更新机制:批量更新策略与“异步“错觉的深层解析
前端·react.js·前端框架