最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

el-table 表格最大高度max-height的問題解決

 更新時間:2024年07月24日 10:12:36   作者:宋玉的世界  
在工作中遇到了多個滾動條的情況,是因為el-table的max-height設置為固定值導致的,本文主要介紹了el-table 表格最大高度max-height的問題解決,具有一定的參考價值,感興趣的可以了解一下

寫在前面

在工作中遇到了多個滾動條的情況,后來仔細研究了一下是因為el-table的max-height設置為固定值導致的,當窗口區(qū)域過小就會出現(xiàn)這種情況。

計算頁面中 el table 表格最大高度max height 20240722184637857

知道問題了就可以對癥下藥,根據(jù)屏幕的大小動態(tài)設置max-heigth。在網(wǎng)上看了很多的方案都是減去除了表格以外其他元素的寬高來動態(tài)計算高度,顯然這種方法不夠健壯一旦新增或減少元素,我們就必須調整代碼。于是我想到頁面流的特性,可以用 flex: 1 +  offsetHeight 的方式實現(xiàn),flex: 1 始終會撐滿剩下區(qū)域,那我們就可以獲取到flex: 1 的容器高度設置給el-table從而實現(xiàn)動態(tài)高度,無需手動計算,這樣既簡單又健壯。

Vue3寫法

為了增加代碼的可復用性,Vue3中可以將相關的代碼封裝成一個hooks。這段代碼中最主要的功能是當窗口變化時獲取到容器的 offsetHeight 設置給 maxHeight。

function useMaxWidth() {
    const tableRef = ref();
    const maxHeight = ref(0);

    onMounted(() => {
      window.addEventListener("resize", resize);
    });

    onUnmounted(() => {
      window.removeEventListener("resize", resize);
    });

    function resize() {
      maxHeight.value = 0;
      nextTick(() => {
        maxHeight.value = tableRef.value.offsetHeight;
      });
    }
    return { maxHeight, tableRef, resize };
  }

接著,在setup 中使用 useMaxWidth, 在加載完數(shù)據(jù)之后,執(zhí)行一次 resize() 來獲取maxHeight, 并導出tableData、 tableRef 和 maxHeight。

const App = {
    setup() {
      let tableData = ref([]);
      onBeforeMount(async () => {
        tableData.value = await new Promise((resolve) => {
          setTimeout(() => {
            const data = new Array(100).fill({
              date: "2016-05-02",
              name: "王小虎",
              address: "上海市普陀區(qū)金沙江路 1518 弄",
            });
            resolve(data);
          }, 2000);
        });
      });

      const { maxHeight, tableRef, resize } = useMaxWidth();

      resize();

      return {
        tableData,
        tableRef,
        maxHeight,
      };
    },
  };
  const app = createApp(App);
  app.use(ElementPlus);
  app.mount("#app");

最后,在 el-table外定義了一個 flex:1 的容器,將 tableRef 綁定到上面,并將 tableData 和 maxHeight 綁定到 el-table 上。

<main class="HolyGrail-content" ref="tableRef">
      <el-table :data="tableData" :max-height="maxHeight">
        <el-table-column prop="date" label="日期" sortable width="180">
        </el-table-column>
        <el-table-column prop="name" label="姓名" sortable width="180">
        </el-table-column>
        <el-table-column prop="address" label="地址" :formatter="formatter">
        </el-table-column>
      </el-table>
    </main>
.HolyGrail-content {
 flex: 1;
}

Vue2寫法

在 Vue3 中可以通過 hooks 復用代碼,Vue2 則可以將功能封裝成一個 table-container 組件,并將 el-table 插入其中。

<template>
 <div class="table-container" ref="table">
  <slot :maxHeight="maxHeight"></slot>
 </div>
</template>
<script>
 export default {
   name:'',
   data () {
     return {
      maxHeight: 0
     }
   },
   mounted() {
    window.addEventListener('resize', this.resize);
   },
   beforeDestroy() {
    window.removeEventListener('resize', this.resize)
   },
   methods:{
    resize() {
      this.maxHeight = 0;
      this.$nextTick(() => {
        this.maxHeight = this.$refs.table.offsetHeight;
      })
    }
   },
 }
</script>
<style scoped>
  .table-container {
    height: 100%;
  } 
</style>

<table-container ref="tableContainer" v-slot="slotProps">
      <el-table :data="tableList" :max-height="slotProps.maxHeight" >
      </el-table>
  </table-container>

當然,也需要在接口數(shù)據(jù)加載完成的時候執(zhí)行一下 resize() 方法。

this.$refs.tableContainer.resize();

重新設置max-height表格會重新計算, 會導致 scrollTop 會重置為0,如何實現(xiàn)上拉加載,下拉刷新!|下拉加載的滾動條會回到頂部,因此可以只在第一頁計算最大高度。

if(this.listQuery.page === 1) this.$refs.tableContainer.resize();

到此這篇關于el-table 表格最大高度max-height的問題解決的文章就介紹到這了,更多相關el-table max-height內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

剑川县| 武邑县| 贵港市| 呼图壁县| 西昌市| 镇坪县| 军事| 沁阳市| 庐江县| 凤阳县| 宾阳县| 衡东县| 青州市| 胶南市| 滕州市| 青冈县| 闸北区| 屯留县| 吴桥县| 绥滨县| 津南区| 康乐县| 扶绥县| 扎囊县| 年辖:市辖区| 陵水| 松阳县| 静乐县| 姚安县| 嘉鱼县| 外汇| 东安县| 宁河县| 台江县| 嵊泗县| 黄龙县| 依兰县| 尼玛县| 泰兴市| 广元市| 平邑县|