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

1.Element的table表高度自适应vue3+js写法

解决方法

在页面table上添加id,动态计算每页table的最大高度 ,将高度保存在store中,每次切换路由时进行计算。

文章目录

  • 解决方法
  • 前言
  • 一、页面table使用
  • 二、store状态库
    • 1.引入库
  • 效果

前言

提示:状态管理使用的是pinia,用法参考简单学习pinia

  1. 项目目录
    在这里插入图片描述

一、页面table使用

  1. 添加:max-height,和id
<el-table :max-height="tableHeightStore.height" id="elTableId" ><el-table-column type="selection" width="30" align="center" /><el-table-column label="标题" fixed align="center" prop="title" width="180" />
</el-table>
  1. js部分,引入高度tableHeightStore.height,计算高度方法useTableHeightStore().setTableHeight()
<script setup name="Activity">import useTableHeightStore from '@/store/modules/table'const tableHeightStore = useTableHeightStore()onActivated(() => {useTableHeightStore().setTableHeight()})onMounted(() => {useTableHeightStore().setTableHeight()})
</script>

二、store状态库

1.引入库

代码如下(table.js):

const useTableHeightStore = defineStore('table-height',{state: () => ({height: 500,//默认高度}),getters:{},actions:{setHeight(height) {this.height=height//高度赋值},setTableHeight() {//计算高度:窗口高度-表格距离顶部距离-固定高度(底部页码高度)this.height= window.innerHeight -document.getElementById("elTableId").getBoundingClientRect().top - 52;//问题:在浏览器窗口缩放90%时,table表的滚动条会消失(max-height丢失),解决:操作dom添加document.querySelector('#elTableId .el-scrollbar__wrap').style.maxHeight = this.height - 40 + "px"console.log("路由切换table高度刷新",this.height);window.onresize = () => {console.log("-----","防抖");debounce(tableHeightFun, 200); //防抖};let that=thisfunction tableHeightFun() {//页面没有table的id时,不执行if(document.getElementById('elTableId')==null)return//问题:在路由切换后,窗口高度变高,table表的max-height不能变大。解决:先赋值变小,再变大。that.height = window.innerHeight - document.getElementById('elTableId').getBoundingClientRect().top - 52 - 1;document.querySelector('#elTableId .el-scrollbar__wrap').style.maxHeight = (that.height - 40 - 1) + "px"setTimeout(() => {that.height=  window.innerHeight -document.getElementById("elTableId").getBoundingClientRect().top -52;document.querySelector('#elTableId .el-scrollbar__wrap').style.maxHeight = (that.height - 40) + "px"console.log("--tableHeightFun---",that.height);}, 200);}var timeout = null; //定义一个定时器function debounce(fn, wait) {if (timeout !== null) clearTimeout(timeout); //清除这个定时器timeout = setTimeout(fn, wait);}}}
})export default useTableHeightStore// table添加
// :max-height="tableHeightStore.height" id="elTableId"// 页面引入
// import useTableHeightStore from '@/store/modules/table'
// const tableHeightStore = useTableHeightStore()// 路由状态-缓存-每次进入页面触发onActivated
// onActivated(() => {
//   useTableHeightStore().setTableHeight()
// })// 路由状态-不缓存-每次进入页面触发onMounted
// onMounted(() => {
//   useTableHeightStore().setTableHeight()
// })

效果

在这里插入图片描述

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

相关文章:

  • 联想电脑电池只能充到80%,就不在充电了,猛一看以为坏了,只是设置了养护模式。
  • Unity接入PS5手柄和Xbox手柄以及Android平台的(以及不同平台分析)
  • vue+java实现简易AI问答组件(基于百度文心大模型)
  • 刷代码随想有感(104):动态规划——01背包问题/二维dp数组
  • Docker-Portainer可视化管理工具
  • SqlSugar 集成
  • MySQL Connector/C++ 和 MySQL Connector/ODBC 的区别
  • Weevil-Optimizer象鼻虫优化算法的matlab仿真实现
  • Web前端项目-交互式3D魔方【附源码】
  • 视频格式转换avi格式怎么弄?分享视频转换方法
  • UniRx 入门
  • 简单游戏制作——飞行棋
  • 等保一体机
  • 什么是寄存器文件(Register File)?
  • 6月15号作业
  • 零基础入门学用Arduino 第三部分(三)
  • Trusty qemu + android环境搭建详细步骤
  • 杀戮尖塔游戏
  • Kubernetes (K8s) 和 Spring Cloud 的区别
  • 定个小目标之刷LeetCode热题(21)
  • Oracle 打开钱包 ORA-28368: cannot auto-create wallet
  • 【麒麟虚拟机】NetworkManager没有运行
  • vue之一键部署的shell脚本和它的点.bat文件、海螺AI、ChatGPT
  • pg和oracle的区别
  • Docker:在DockerHub上创建私有仓库
  • 框架的使用
  • Autosar-DEM诊断事件管理流程
  • LabVIEW输送机动态特性参数监测系统
  • 绿色版DirectoryOpus功能强大且高度可定制的Windows文件管理器
  • Cocos Creator,Youtube 小游戏!