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

相关推荐
阿猫的故乡43 分钟前
Vue过渡动画从入门到装X:淡入淡出、滑动、列表动画、第三方库全搞定
前端·javascript·vue.js
裕波1 小时前
Vue&ViteConf 2026 将于 7 月 18 日在上海举办,尤雨溪将现场发表主题演讲
vue.js·vite
IManiy1 小时前
总结之Vibe Coding前端骨架
前端
JS菌1 小时前
AI Agent 沙箱双层防护体系:从权限过滤到内核隔离的完整实现
前端·人工智能·后端
Aphasia3111 小时前
从输入URL到页面展示全流程
前端·面试
我叫黑大帅2 小时前
前端如何竖屏固定视口背景
前端·javascript·面试
abcy0712132 小时前
python pandas csv异步后台清洗前端优先返回成功信息
前端·python·pandas
IT_陈寒2 小时前
Vite这个坑我帮你踩了,动态导入居然这样才生效
前端·人工智能·后端
swipe2 小时前
Mem0 x Agent 实战系列:分层记忆 + 三路召回,搭建真正可用的长期记忆层
前端·javascript·面试
鹤鸣的日常2 小时前
前端运行时动态环境变量方案
前端·react.js·docker·前端框架·vue·gitlab