element table高度自适应

1.自定义一个table的高度tableHeight
<el-table
      @row-click="lookDetail"
      v-loadmore="loadMore"
      v-loading="listLoading"
      :data="tableData"
      :height="tableHeight"
      border
      style="width: 100%">
      <el-table-column
        :key="item.prop"
        v-for="item in tableColumn"
        :prop="item.prop"
        :label="item.name">
      </el-table-column>
    </el-table>

2.data中给tableHeight一个默认的高度
tableHeight: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight - 240,(这里的240是减去表格上面导航和搜索条件的高度)

3.在mounted中初始化数据
mounted() {
      const that = this
      window.onresize = () => {
        return (() => {
          window.tableHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
          that.tableHeight = window.tableHeight - that.$refs.queryHeight.offsetHeight - 150
        })()
      }
    },
4.在watch中监听高度的变化
watch: {
      // 这里的定时器是为了优化,如果频繁调用window.onresize方法会造成页面卡顿,增加定时器会避免频繁调用window.onresize方法
      timer默认值设置为false,这里相当于一个按钮,防止频繁改变时引起卡顿
      tableHeight(val) {
        if (!this.timer) {
          this.tableHeight = val
          this.timer = true
          const that = this
          setTimeout(function() {
            that.timer = false
          }, 400)
        }
      }
    },
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值