Vue 学习随笔系列二十二 —— 表格高度自适应

表格高度自适应

文章目录


1、方法一

根据页面元素计算各自占比

bash 复制代码
<template>
  <div class="main">
    <div class="query-form" ref="Query">
      <QueryForm
        ref="QueryForm"
        @query="query"
      ></QueryForm>
    </div>

    <div class="table-box" ref="Temp">
      <TableModal
        ref="TableModal"
        :maxHeight="tempHeight-200"
        :tableData="tableData"
      ></TableModal>
      <!-- 分页 -->
      <div class="pagination-box flex-h flex-he">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="pageSizes"
          :current-page="pageNum"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          background
          small
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import QueryForm from './QueryForm.vue';
  import TableModal from './TableModal.vue';
  import FileSaver from "file-saver";

  export default {
    components: {
      QueryForm, 
      TableModal,
    },
    data() {
      return {
        tableData: [],
        total: 0,
        pageNum: 1,
        pageSize: 10,
        pageSizes: [10, 20, 50, 100],
        multipleSelection: [],
        maxHeight: 220,
        windowHeight: 0,  // 页面窗口高度
        tempHeight: 0,  // 元素高度
        QueryHeight: 0, // 查询框高度
      }
    },
    mounted() {
      this.query()

      // 自动获取元素高度
      var that = this;
      //刚进入页面时,获取窗口默认宽高度
      this.windowHeight = document.body.clientHeight
      this.QueryHeight = this.$refs.Query.offsetHeight
      //进入页面元素默认宽高
      // this.tempHeight = this.$refs.Temp.offsetHeight
      // this.maxHeight = this.tempHeight - 70
      this.tempHeight = this.windowHeight - this.QueryHeight

      window.onresize = () => {
        return (() => {
          //窗口缩放自动获取页面宽高
          window.fullHeight = document.documentElement.clientHeight;
          this.QueryHeight = this.$refs.Query.offsetHeight
          that.windowHeight = window.fullHeight; //高

          //窗口缩放自动获取元素宽高
          // this.tempHeight = this.$refs.Temp.offsetHeight //高
          // this.maxHeight = this.tempHeight - 70
          this.tempHeight = this.windowHeight - this.QueryHeight
        })()
      }
    },
    methods: {
      // 分页
      handleSizeChange (val) {
        this.pageSize = val
        this.query()
      },
      // 当前页
      handleCurrentChange (val) {
        this.pageNum = val
        this.query()
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      query(){
        const form = this.$refs.QueryForm.getParams()
        const params = { ...form, pageNum: this.pageNum, pageSize: this.pageSize}
       	// ....
      },  
    },
  }
</script>

<style lang="scss" scoped>
.main  {
  padding: 10px;
  background-color: #F2F3F5;
} 

// 分页
.pagination-box {
  margin-top: 20px;
  float: right;
}
:deep .pagination-box .el-select--mini .el-input--mini .el-input__inner {
  height: 22px;
  line-height: 22px;
}

:deep .pagination-box .el-select--mini .el-input--mini .el-input__icon {
  line-height: 22px;
}
</style>

2、方法二

直接计算表格高度

bash 复制代码
<template>
    <div>
        <el-card>
            <QueryForm
                ref="queryForm"
                @query="query"
            ></QueryForm>
        </el-card>
    
        <TableColumn
            :tableData="tableData"
            :tableHeight="tableHeight"
        ></TableColumn>
    </div>

</template>

<script>
import QueryForm from "./queryForm.vue"
import TableColumn from './tableColumn.vue'

export default {
    components: {
        QueryForm,
        TableColumn,
    },
    data() {
        return {
            tableData: [],
            tableHeight: 0,
        }
    },
    created() {
      this.tableHeight = window.innerHeight - 340
    },
    mounted() {
        this.query()
        this.handleTableHeight()
    },
    methods: {
        handleTableHeight: function () {
            var _this = this;
            window.onresize = () => {
                _this.tableHeight = window.innerHeight - 346
            };
    
        },
        query() {
            const params = this.$refs.queryForm.getForm()
            // .......
        },

    }
}
</script>

<style lang="less" scoped >
    .el-card {
        margin: 0 0 10px 0;
    }
</style>
相关推荐
良木林5 分钟前
JavaScript书写基础和基本数据类型
开发语言·前端·javascript
LGGGGGQ2 小时前
嵌入式学习-PyTorch(4)-day21
学习
艾莉丝努力练剑3 小时前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
人生游戏牛马NPC1号4 小时前
学习 Flutter (三):玩安卓项目实战 - 上
android·学习·flutter
工业甲酰苯胺7 小时前
TypeScript枚举类型应用:前后端状态码映射的最简方案
javascript·typescript·状态模式
没有羊的王K8 小时前
SSM框架学习——day1
java·学习
林林要一直努力9 小时前
AOSP Settings模块问题初窥
android·学习·bug·android studio
谢尔登10 小时前
【React Native】ScrollView 和 FlatList 组件
javascript·react native·react.js
然我11 小时前
面试官:如何判断元素是否出现过?我:三种哈希方法任你选
前端·javascript·算法
kk_stoper11 小时前
如何通过API查询实时能源期货价格
java·开发语言·javascript·数据结构·python·能源