计算页面中 el-table 表格最大高度

methods: {
  /**
   * ele-table 的 maxHeight 需要根据页面内容进行计算,
   * 【因为】列表没有数据时没有分页容器,获取到的高度为0
   * 【所以】不能再页面初始化的时候计算 ele-table 最大高度,只能在获取到分页数据后进行计算
   * 【注意】在接口获取分页数据成功之后计算 ele-table 最大高度
   */
  getTableHeight() {
    const AppMain = document.querySelector('.AppMain') // 内容区域容器
    const searchForm = document.querySelector('.searchForm') // 搜索区域容器
    const tableTopBtns = document.querySelector('.ele-table-button-group') // 表格顶部按钮容器
    const tablePagination = document.querySelector('.ele-table-pagination') // 分页容器
    const AppMainHeight = (AppMain && AppMain.offsetHeight) || 0
    const searchFormHeight = (searchForm && searchForm.offsetHeight) || 0
    const tableTopBtnsHeight = (tableTopBtns && tableTopBtns.offsetHeight) || 0
    const tablePaginationHeight = (tablePagination && tablePagination.offsetHeight) || 0
    return AppMainHeight - searchFormHeight - tableTopBtnsHeight - tablePaginationHeight
  },
  // 在接口获取分页数据成功之后调用 setTableHeight 方法,计算 ele-table 最大高度
  setTableHeight() {
    this.$nextTick(() => {
      this.tableMaxHeight = this.getTableHeight()
    })
  }
}

优化

/** ** ele-table 最大高度计算 ****/
export const EleTableMixin = {
  data() {
    return {
      tableData: [],
      tableMaxHeight: 400,
      total: 0
    }
  },
  computed: {
    options() {
      return {
        data: this.tableData,
        maxHeight: this.tableMaxHeight
      }
    }
  },
  mounted() {
    window.addEventListener('resize', () => {
      this.setTableHeight()
    })
  },
  methods: {
    /**
     * ele-table 的 maxHeight 需要根据页面内容进行计算,
     * 【因为】列表没有数据时没有分页容器,获取到的高度为0
     * 【所以】不能再页面初始化的时候计算 ele-table 最大高度,只能在获取到分页数据后进行计算
     * 【注意】在接口获取分页数据成功之后计算 ele-table 最大高度
     */
    getTableHeight() {
      const AppMain = document.querySelector('.AppMain')
      const searchForm = document.querySelector('.searchForm')
      const tableTopBtns = document.querySelector('.ele-table-button-group')
      const tablePagination = document.querySelector('.ele-table-pagination')
      const AppMainHeight = (AppMain && AppMain.offsetHeight) || 0
      const searchFormHeight = (searchForm && searchForm.offsetHeight) || 0
      const tableTopBtnsHeight = (tableTopBtns && tableTopBtns.offsetHeight) || 0
      const tablePaginationHeight = (tablePagination && tablePagination.offsetHeight) || 0
      return AppMainHeight - searchFormHeight - tableTopBtnsHeight - tablePaginationHeight
    },
    // 在接口获取分页数据成功之后调用 setTableHeight 方法,计算 ele-table 最大高度
    setTableHeight() {
      this.$nextTick(() => {
        this.tableMaxHeight = this.getTableHeight()
      })
    },
    // 序号处理
    indexMethod(index) {
      return (this.searchForm.page - 1) * this.searchForm.rows + index + 1
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值