table表格页面高度自适应铺满

js写法

function computeHeight(){
      const element = document.querySelector('.my-table');// 获取table表格
      const rect = element.getBoundingClientRect();
      const tablebody = document.querySelector('.el-table__body-wrapper') // 需要设置自适应高度的元素
      const h = window.innerHeight - rect.top - 42 - 44 - 20;
      tablebody.style.minHeight = h+'px';
      tablebody.style.maxHeight = h+'px';
      tablebody.style.overflow = "auto";
 }
    computeHeight();
    window.onresize = function(){
      computeHeight();
    }

vue2指令写法

/**
 * 
 * @param {*} el 
 * @param {*} binding.value.h  除了 table__body 以外的高度
 */
function changeTableHeight(el, binding) {
  const rect = el.getBoundingClientRect();
  let tablebody = '';
  Array.from(el.children).forEach(item => {
    if (item.className.includes('el-table__body-wrapper')) {
      tablebody = item;
    }
  })
  // 42  表格标题 - 44  分页器和上边距 - 20 页面整体padding
  let h = window.innerHeight - rect.top - 42 - 44 - 20;
  if (binding.value?.h) {
    h = window.innerHeight - rect.top - binding.value.h;
  }
  tablebody.style.minHeight = h + 'px';
  tablebody.style.maxHeight = h + 'px';
  tablebody.style.overflow = "auto";
}
export default {
  bind(el, binding, vnode) {
    vnode.context.$nextTick(() => {
      changeTableHeight(el, binding);
    })
    // 定义 resizeHandler 函数
    const resizeHandler = () => {
      vnode.context.$forceUpdate();
    };
    // 监听窗口大小改变事件
    window.addEventListener('resize', resizeHandler);
    // 在解绑时移除事件监听器
    vnode.context.$on('hook:unbind', () => {
      window.removeEventListener('resize', resizeHandler);
    });
  },
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值