解决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 负责滚动。

相关推荐
m0_502724951 分钟前
vue3生成pdf
前端·javascript·vue.js·pdf
@不误正业3 分钟前
2026-05-16-多Agent协作框架深度实战-从ReAct到Plan-and-Execute全架构演进
前端·react.js·架构
我命由我123453 分钟前
PHP - PHP 简易 Web 服务器、基础接口开发
服务器·开发语言·前端·php·intellij-idea·idea·intellij idea
咖喱o5 分钟前
IPv6
服务器·前端·网络
海上彼尚6 分钟前
Nodejs也能写Agent - 6.基础篇 - Agent
前端·人工智能·后端·node.js
2501_940041749 分钟前
纯前端实战:5个高复杂度业务与交互场景
前端
renke336413 分钟前
写给前端的 CANN-torchtitan-npu:昇腾PyTorch Titan适配到底是啥?
前端·人工智能·pytorch·cann
lihaozecq16 分钟前
Agent 开发的 skills 机制设计 - 渐进式披露
前端·agent·ai编程
安生生申19 分钟前
uni-app 连接 JDY-31 蓝牙串口模块实践
c语言·前端·javascript·stm32·单片机·嵌入式硬件·uni-app
Restart-AHTCM24 分钟前
LangChain学习之模型 I/O 与输出解析器 (Output Parsers)(3/8)
前端·学习·langchain