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

el-table添加固定高度height后高度自适应

0 效果

在这里插入图片描述

1 添加自定义指令

新建目录src/directive/el-table
在el-table目录下新建文件adaptive.js

import { addResizeListener, removeResizeListener } from 'element-ui/src/utils/resize-event'// 设置表格高度const doResize = async(el, binding, vnode) => {// 获取表格Dom对象const { componentInstance: $table } = await vnode// 获取调用传递过来的数据 const { value } = bindingif (!$table.height) {throw new Error(`el-$table must set the height. Such as height='100px'`)}// 获取距底部距离(用于展示页码等信息)const bottomOffset = (value && value.bottomOffset) || 90if (!$table) return// 计算列表高度并设置const height = window.innerHeight - el.getBoundingClientRect().top - bottomOffset$table.layout.setHeight(height)$table.doLayout()
}export default {  // 初始化设置bind(el, binding, vnode) { // 设置resize监听方法el.resizeListener = async() => { await doResize(el, binding, vnode)}    // 绑定监听方法到addResizeListeneraddResizeListener(window.document.body, el.resizeListener)  },  // 绑定默认高度async inserted(el, binding, vnode) { await doResize(el, binding, vnode)  },  // 销毁时设置unbind(el) { // 移除resize监听removeResizeListener(el, el.resizeListener)  }
}

在el-table目录下新建index.js

import adaptive from './adaptive'const install = function(Vue) {   // 绑定v-adaptive指令Vue.directive('adaptive', adaptive)
}if (window.Vue) {window['adaptive'] = adaptive  // eslint-disable-next-line no-undef Vue.use(install)
}adaptive.install = installexport default adaptive

2 引入

2.1 单个vue文件引入

在所需使用的vue页面中引入
import adaptive from ‘…/…/…/src/directive/el-table’
在这里插入图片描述

2.2 全局引入

在main.js中引入
在这里插入图片描述

3 使用

在el-table标签中添加属性
v-adaptive:自定义指令,bottomOffset 代表距离底部的距离
height:高度属性,100无具体意义,仅为初始值,不可省略
在这里插入图片描述

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

相关文章:

  • Python分享之多进程探索 (multiprocessing包)
  • Boris FX Mocha Pro 2023:Mac/win全能影像处理神器
  • elementUI 特定分辨率(如1920*1080)下el-row未超出一行却换行
  • mac电脑视频处理推荐:达芬奇DaVinci Resolve Studio 18 中文最新
  • OKLink携手CertiK在港举办Web3生态安全主题论坛
  • 王道p40 1.设计一个递归算法,删除不带头结点的单链表L中的所有值为x的结点(c语言代码实现)图解递归
  • 深入浅出排序算法之希尔排序
  • close excel by keyword 根据关键字关闭 excel 窗口 xlwings 方式实现
  • LIO-SAM算法解析
  • vscode 提升小程序开发效率的必备插件与工具
  • 第五章单元测试
  • 【JAVA基础】多线程与线程池
  • HCIA数据通信——交换机(Vlan间的通信与安全)
  • Linux shell编程学习笔记16:bash中的关联数组
  • 浏览器是怎么执行JS的?——消息队列与事件循环
  • IMU预积分的过程详解
  • TypeScript中的类型运算符
  • 【蓝桥杯选拔赛真题03】C++输出字母Y 青少年组蓝桥杯C++选拔赛真题 STEMA比赛真题解析
  • redis搭建集群-多实例快速搭建
  • 为什么进行压力测试? 有哪些方法?
  • Java开发者必备:支付宝沙箱环境支付远程调试指南
  • 基于STM32温湿度传感器采集报警系统设计
  • 檢測項目簡體字
  • 适用于嵌入式arm的ffmpeg编解码
  • nlp与知识图谱代码解读_词嵌入
  • HarmonyOS 音频通话开发指导
  • LeetCode讲解篇之面试题 01.08. 零矩阵
  • 安装python虚拟环境
  • 【App 抓包提示网络异常怎么破?】
  • 【开发篇】一、处理函数:定时器与定时服务