解决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"
>
      

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

相关推荐
日光倾20 分钟前
TypeScript 随手记 —— 1
前端·javascript·typescript
自学it的攻城狮33 分钟前
elpis-core ,koa实现的系统底座, 沉淀80% 的通用能力,剩下20% 用于定制化开发
前端
CappuccinoRose1 小时前
模块化体系
前端·import·export·es modules
AIDANHANG2 小时前
放开靠时间戳对日志前先核请求ID跨服务传播与采样关联
前端·人工智能
半生过往3 小时前
前端学 Java 课程笔记
java·前端·笔记
广州华水科技4 小时前
2026年单北斗GNSS变形监测系统推荐,破解水库安全监测难题
前端
独爱香菜4 小时前
Next.js 15 + Cloudflare Workers 实战:零中间件多语言 SEO 站点架构
前端
计算机魔术师4 小时前
DeepSeek、Qwen3比肩GPT-5,本地跑大模型的时代来了
前端
xcyxiner4 小时前
flutter wsl2安装记录(使用宿主机虚拟机)
前端·flutter
前端炒粉5 小时前
长会话虚拟滚动与渲染优化
前端·javascript·vue.js