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

解决方法

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

文章目录

前言

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

  1. 项目目录

一、页面table使用

  1. 添加:max-height,和id
html 复制代码
<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()
javascript 复制代码
<script setup name="Activity">
	import useTableHeightStore from '@/store/modules/table'
	const tableHeightStore = useTableHeightStore()
	onActivated(() => {
	  useTableHeightStore().setTableHeight()
	})
	onMounted(() => {
	  useTableHeightStore().setTableHeight()
	})
</script>

二、store状态库

1.引入库

代码如下(table.js):

javascript 复制代码
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=this
        function 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()
// })

效果

相关推荐
码农黛兮_4628 分钟前
CSS3 基础知识、原理及与CSS的区别
前端·css·css3
水银嘻嘻1 小时前
web 自动化之 Unittest 四大组件
运维·前端·自动化
(((φ(◎ロ◎;)φ)))牵丝戏安1 小时前
根据输入的数据渲染柱形图
前端·css·css3·js
wuyijysx1 小时前
JavaScript grammar
前端·javascript
溪饱鱼1 小时前
第6章: SEO与交互指标
服务器·前端·microsoft
咔_2 小时前
LinkedList详解(源码分析)
前端
逍遥德2 小时前
CSS可以继承的样式汇总
前端·css·ui
读心悦2 小时前
CSS3 选择器完全指南:从基础到高级的元素定位技术
前端·css·css3
大鱼前端3 小时前
Vue 3.5 :新特性全解析与开发实践指南
vue.js
学渣y3 小时前
React状态管理-对state进行保留和重置
javascript·react.js·ecmascript