Vue+Element-UI根据浏览器大小动态设置el-table的高度,实现自适应

一、页面渲染

<template>
  <div class="app-container">
    <div style="background-color:green;padding:20px;margin-bottom:30px">
      <el-table
        :data="table1_info.tableData11"
        style="width: 100%;"
        border
        :height="tableHeight"
      >
        <el-table-column
          v-for="(item,index1) in table1_info.tableData11[0]"
          :key="index1"
          align="center"
          :label="index1"
        >
          <template slot-scope="scope">
            {{ scope.row[index1] }}
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

二、定义表格渲染数据及所需变量

data() {
    return {
      tableHeight: window.innerHeight - 260, //表格动态高度
      screenHeight: window.innerHeight, //内容区域高度
      table1_info: {
        title: '表格1',
        tableData11: [
          {
            '值1': '1',
            '值2': '2',
            '值3': '3'
          },
          {
            '值1': '11',
            '值2': '22',
            '值3': '33'
          }
        ]
      },
    }
  },

三、监听内容区域高度 screenHeight

通过监听 screenHeight 来动态设置表格高度 tableHeight

watch: {
    // 监听screenHeight从而改变table的高度
    screenHeight(val) {
      this.screenHeight = val
      this.tableHeight = this.screenHeight - 260
    }
 },

四、浏览器尺寸变化响应事件

只要浏览器尺寸发生变化,则重新给 screenHeight 赋值

mounted: function() {
    // window.onresize:浏览器尺寸变化响应事件
    window.onresize = () => {
      return (() => {
        // window.innerHeight:浏览器的可用高度
        window.screenHeight = window.innerHeight
        this.screenHeight = window.screenHeight
      })()
    }
  },

五、效果展示

浏览器全屏显示时:

在这里插入图片描述

浏览器缩小时:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值