Summary : Open the console or zoom the browser to affect the height of the el-table, and a scroll bar will appear to facilitate viewing and subsequent development.
Situation 1: Open the console
Before:
After:
Scenario 2: Zooming the browser
Before change:
After change:
Code:
Create a new el-table file in the /assets/js directory
Create a new adaptive.js in the el-table folder
import {
addResizeListener, removeResizeListener } from 'element-ui/src/utils/resize-event'
// 设置表格高度
const doResize = async(el, binding, vnode) => {
// 获取表格Dom对象
const {
componentInstance: $table } = await vnode
// 获取调用传递过来的数据
const {
value } = binding
if (!$table.height) {
throw new Error(`el-$table must set the height. Such as height='100px'`)
}
// 获取距底部距离(用于展示页码等信息)
const bottomOffset = (value && value.bottomOffset) || 30
if (!$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)
}
// 绑定监听方法到addResizeListener
addResizeListener(window.document.body, el.resizeListener)
},
// 绑定默认高度
async inserted(el, binding, vnode) {
await doResize(el, binding, vnode)
},
// 销毁时设置
unbind(el) {
// 移除resize监听
removeResizeListener(el, el.resizeListener)
}
}
Create a new index.js in the el-table folder
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 = install
export default adaptive
Introduced in main.js
import adaptive from './assets/js/el-table'
used in tables
<el-table
:data="tableData"
border
style="width: 100%"
v-adaptive="{bottomOffset: 85}"
height="100px"
>
......
</el-table>
Note : The height=“100px” attribute value is required. You can fill in a certain value at will, otherwise an error will be reported
. Thanks for this article.