解决vue2 element-table 自适应高度问题

解决element表格自适应高度问题

在写公司后台项目的时候遇到一个需求,要求表格页面不能有滚动条,所以必须封装一个公共方法来实现表格自适应高度

第一步 实现自定义指令去监听表格元素高度变化

我这边使用封住思想 首先创建在obSize文件夹下创建index.js内容如下

// 复制代码
const map = new WeakMap()
// ob监听
const ob = new ResizeObserver((entries) => {
   for (const entry of entries) {
       // 处理元素对应的回调
       const handler = map.get(entry.target)
       if (handler) {
           const box = entry.borderBoxSize[0]
           // 循环entry.target 累加offsetTop 从而得到距离页面顶部距离
           let setTop = countTop(entry.target, 0)

           handler({
               width: box.inlineSize,
               height: box.blockSize,
               entry: entry,
               bodHeight: window.document.body.clientHeight,
               setTop: setTop,
               tableHeight: (window.document.body.clientHeight - setTop - 50 - box.inlineSize) > 0 ? '' : window.document.body.clientHeight - setTop - 72
           })
       }
   }
})

export function countTop(el, topn) {
   if (el.offsetParent) {
       return countTop(el.offsetParent, topn + el.offsetTop)
   } else {
       return topn
   }

}
export default {
   inserted(el, binding) {
       ob.observe(el)
       // 监听el元素尺度的变化
       map.set(el, binding.value)
   },
   unbind(el) {
       // 取消监听
       ob.unobserve(el)
   },
}

注册vue指令在directive文件夹index.js文件中

javascript 复制代码
import obSize from './obSize'
const install = function (Vue) {
  //这里可以放入多个自定义指令
  Vue.directive('ob-size', obSize)
}

if (window.Vue) {

  Vue.use(install); // eslint-disable-line
}

export default install

在main.js引入directive

javascript 复制代码
import directive from './directive'
Vue.use(directive)

第二步 封装mixins

在mixins文件夹内创建estimateTableHeight.js内容如下 70 代表距离页面底部的高度

javascript 复制代码
import { countTop } from '@/directive/obSize'
export default {
    data() {
        return {
            tableHeight: 0,
        }
    },
    methods: {
        handleSizeChange(e) {
            this.tableHeight = e.tableHeight;
        },
    },
    // 监听showSearch
    watch: {
        showSearch(val) {
            // 获取element table元素
            const dome = document.getElementsByClassName('el-table');
            let setTop = countTop(dome, 0)
            this.tableHeight = window.document.body.clientHeight - setTop - 70
        },
    },
    mounted() {
    //监听页面尺寸变化
        window.addEventListener('resize', () => {
            const dome = document.getElementsByClassName('el-table');
            let setTop = countTop(dome, 0)
            this.tableHeight = window.document.body.clientHeight - setTop - 70
        });
    },
}

第三步 在页面引入

引入 import estimateTableHeight from "@/mixins/estimateTableHeight";

export default { mixins: [estimateTableHeight], }

在el-table上加入 v-ob-size="handleSizeChange" :height="tableHeight"

ini 复制代码
<el-table
        v-ob-size="handleSizeChange"
        :height="tableHeight"
        v-loading="loading"
        :data="List"
>
      

大功告成!!!!!!!!!!!!!!!

相关推荐
学习使我快乐013 小时前
JS进阶 3——深入面向对象、原型
开发语言·前端·javascript
bobostudio19953 小时前
TypeScript 设计模式之【策略模式】
前端·javascript·设计模式·typescript·策略模式
黄尚圈圈4 小时前
Vue 中引入 ECharts 的详细步骤与示例
前端·vue.js·echarts
浮华似水5 小时前
简洁之道 - React Hook Form
前端
正小安7 小时前
如何在微信小程序中实现分包加载和预下载
前端·微信小程序·小程序
_.Switch9 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一路向前的月光9 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   9 小时前
vite学习教程06、vite.config.js配置
前端·vite配置·端口设置·本地开发
长路 ㅤ   9 小时前
vue-live2d看板娘集成方案设计使用教程
前端·javascript·vue.js·live2d