1. 程式人生 > >element table高度自適應

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)
        }
      }
    },