当前位置: 首页 > news >正文

Element 页面滚动表头置顶

在开发后台管理系统时,表格是最常用的一个组件,为了看数据方便,时常需要固定表头。

如果页面基本只有一个表格区域,我们可以根据屏幕的高度动态的计算出一个值,给表格设定一个固定高度,这样表头就可以固定了。

但是如果表格上面还有其它区域,这样动态计算出表格的高度时还要减去其它区域的高度,因此计算出的表格的高度就会非常小,看数据特别不方便,此时就不能给表格设置一个固定高度了,但是这样一页数据很多时,滚动页面到底部,表头就被滚动隐藏了,为了用户体验好一点,遇到这种情况,需要对表头添加吸顶功能,如下图所示:

下面直接上代码:

<template><div><div class="app-container"><!-- 其它区域 --><div class="table-total"></div><!-- 表格主体 --><div class="table-container"><el-table :data="tableData" style="width:100%;"><el-table-column v-for="item in tableColumn" :key="item.prop" :prop="item.prop" :label="item.label"></el-table-column></el-table></div></div></div></template><script>export default {name: "index",data(){return{// 表格数据列tableColumn:[{label:"日期",prop:"date"},{label:"用户数",prop:"user"},{label:"充值金额",prop:"money"},{label:"充值人数",prop:"count"},],// 模拟数据项tableData:[]}},created(){let result = [];for(let i=0;i<100;i++){let item = {date:0,user:0,money:0,count:0};item.id=i+1;result.push(item);}this.tableData = result;},mounted(){window.addEventListener('scroll', this.handleScroll, true)},beforeDestroy() { window.removeEventListener('scroll', this.handleScroll, true)},methods: {handleScroll(e) {let scrollTop = document.getElementsByClassName('app-container')[0].scrollTop;let offsetWidth = document.getElementsByClassName('app-container')[0].offsetWidth - 43; // 43=>右侧滚动条加上外边距的宽度let headerWrapper = document.getElementsByClassName('el-table__header-wrapper')[0];let fixedWrapper = document.getElementsByClassName('el-table__fixed-header-wrapper');// 300=>为滚动区域内,除了表格以外,其它的区域高度if (scrollTop >= 300) { // 93=>为表头在吸顶时,距离屏幕顶部的位置headerWrapper.style.top = '93px';headerWrapper.style.zIndex = '2';headerWrapper.style.position = 'fixed';headerWrapper.style.width = offsetWidth+'px';// 表格有固定列时还会多出一个表头if(fixedWrapper.length){for (let i=0;i<fixedWrapper.length;i++) {fixedWrapper[i].style.top = '93px';fixedWrapper[i].style.zIndex = '2';fixedWrapper[i].style.position = 'fixed';headerWrapper.style.width = offsetWidth+'px';}}} else {headerWrapper.style.top = '';headerWrapper.style.zIndex = '';headerWrapper.style.position = 'inherit';headerWrapper.style.width = '';if(fixedWrapper.length){for (let i=0;i<fixedWrapper.length;i++) {fixedWrapper[i].style.top = '';fixedWrapper[i].style.zIndex = '';fixedWrapper[i].style.width = '';}}}}},
};
</script><style lang="scss" scoped>.app-container {height: calc(100vh - 108px);overflow-y: scroll;.table-total{height:300px;border:1px solid #eaedf1;}.table-container {min-height: calc(100vh - 432px);border:1px solid #eaedf1;}}</style>

以上代码中涉及到的几个数值,请参考注释根据实际情况进行修改。

http://www.lryc.cn/news/387241.html

相关文章:

  • 对于CDA一级考试该咋准备??!
  • 如何使用PHP和Selenium快速构建自己的网络爬虫系统
  • intellij idea安装R包ggplot2报错问题求解
  • 【C++】初识C++(一)
  • 【智能算法】目标检测算法
  • python 中 json.load json.loadd json.dump json.dumps 详解
  • 【UE 网络】专用服务器和多个客户端加入游戏会话的过程,以及GameMode、PlayerController、Pawn的创建流程
  • 磁盘分区工具(fdisk 和 parted)区别及操作笔记
  • VisualStudio2019受支持的.NET Core
  • Java——IO流(二)-(1/7):字符流-FileReader、FileWriter、字符输出流的注意事项(构造器及常用方法、小结)
  • Spring循环依赖问题——从源码画流程图
  • Android SurfaceFlinger——动画播放准备(十五)
  • Zynq7000系列FPGA中的DMA控制器简介(二)
  • 获取 url 地址栏 ? 后面的查询字符串,并以键值对形式放到对象里面
  • List接口, ArrayList Vector LinkedList
  • 探讨数字化背景下VSM(价值流程图)的挑战和机遇
  • Conda跨平台环境迁移
  • 全面掌握 Jackson 序列化工具:原理、使用与高级配置详解
  • mathtype7.4永久激活码密钥及2024最新破解版注册码附安装教程
  • 【SQL】优化慢 SQL的简单思路
  • 禁止浏览器对input的自动填充和填充提示(适用于谷歌、火狐、Edge(原IE浏览器)等常见浏览器)
  • 鸿蒙项目实战-月木学途:1.编写首页,包括搜索栏、轮播图、宫格
  • 深入浅出:npm常用命令详解和实践
  • 山东大学-科技文献阅读与翻译(期末复习)(选择题+翻译)
  • 二分查找:自定义 upper_bound、lower_bound
  • Java 搭建个人博客基本框架
  • 停车场智能化管理:车位引导系统实现车位资源优化与数据分析
  • 梯度下降法
  • 【高考志愿】光学工程
  • Golang | Leetcode Golang题解之第205题同构字符串