vue+element-ui表格最大高度适配解决方案
第一步:utils目录下创建一个文件为directive,新建auto-height.js文件,
export default {
componentUpdated(el, binding, vnode) {
const ctx = vnode.context;
if (!ctx || typeof ctx[binding.arg] === 'undefined' || ctx.autoHeightResizeListenner) return;
ctx.autoHeightResizeListenner = () => {
let top = el.offsetTop;
let cur = el.offsetParent;
while(cur !== null) {
top += cur.offsetTop;
cur = cur.offsetParent;
}
const h = (window.innerHeight - top) + binding.value;
ctx[binding.arg] = Math.max(h, 100 );
};
window.addEventListener('resize', ctx.autoHeightResizeListener, false);
setTimeout(ctx.autoHeightResizeListenner, 50);
},
unbind(el, binding, vnode){
const ctx = vnode.context;
if(ctx && ctx.autoHeightResizeListener) {
window.removeEventListener('resize', ctx.autoHeightResizeListener, false);
ctx.autoHeightResizeListener = null;
}
},
};
第二步:全局配置 在main.js中导入 auto-height.js文件
import autoHeight from './utils/directive/auto-height';
Vue.directive('auto-height',autoHeight)
第三步:组件中需要的标签添加两个属性 v-auto-height:maxHeight = ‘-10’ :max-height=”maxHeight”
<el-table v-auto-height:maxHeight = '-10' :max-height="maxHeight"></el-table>