el-table 表格最大高度max-height的問題解決
寫在前面
在工作中遇到了多個滾動條的情況,后來仔細研究了一下是因為el-table的max-height設(shè)置為固定值導(dǎo)致的,當窗口區(qū)域過小就會出現(xiàn)這種情況。
知道問題了就可以對癥下藥,根據(jù)屏幕的大小動態(tài)設(shè)置max-heigth。在網(wǎng)上看了很多的方案都是減去除了表格以外其他元素的寬高來動態(tài)計算高度,顯然這種方法不夠健壯一旦新增或減少元素,我們就必須調(diào)整代碼。于是我想到頁面流的特性,可以用 flex: 1 + offsetHeight 的方式實現(xiàn),flex: 1 始終會撐滿剩下區(qū)域,那我們就可以獲取到flex: 1 的容器高度設(shè)置給el-table從而實現(xiàn)動態(tài)高度,無需手動計算,這樣既簡單又健壯。
Vue3寫法
為了增加代碼的可復(fù)用性,Vue3中可以將相關(guān)的代碼封裝成一個hooks。這段代碼中最主要的功能是當窗口變化時獲取到容器的 offsetHeight 設(shè)置給 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, 并導(dǎo)出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 復(fù)用代碼,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();
重新設(shè)置max-height表格會重新計算, 會導(dǎo)致 scrollTop 會重置為0,如何實現(xiàn)上拉加載,下拉刷新!|下拉加載的滾動條會回到頂部,因此可以只在第一頁計算最大高度。
if(this.listQuery.page === 1) this.$refs.tableContainer.resize();
到此這篇關(guān)于el-table 表格最大高度max-height的問題解決的文章就介紹到這了,更多相關(guān)el-table max-height內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+element實現(xiàn)動態(tài)換膚的示例代碼
本文主要介紹了vue+element實現(xiàn)動態(tài)換膚的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09詳解vue-cli + webpack 多頁面實例應(yīng)用
本篇文章主要介紹了詳解vue-cli + webpack 多頁面實例應(yīng)用,具有一定的參考價值,有興趣的可以了解一下2017-04-04Vue3中引入、封裝和使用svg矢量圖的實現(xiàn)示例
SVG全稱Scalable Vector Graphics,它是網(wǎng)絡(luò)上使用最廣泛的矢量圖格式,在項目開發(fā)過程中,我們經(jīng)常會用到svg矢量圖,而且我們使用svg以后,頁面上加載的不再是圖片資源,本文將給大家介紹Vue3中引入、封裝和使用svg矢量圖的實現(xiàn)示例,需要的朋友可以參考下2024-07-07ant-design-vue Table pagination分頁實現(xiàn)全過程
這篇文章主要介紹了ant-design-vue Table pagination分頁實現(xiàn)全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04vue-treeselect及el-tree點擊節(jié)點獲取上級節(jié)點的數(shù)據(jù)方式
這篇文章主要介紹了vue-treeselect及el-tree點擊節(jié)點獲取上級節(jié)點的數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Vue文件如何轉(zhuǎn)換成base64并去除多余的文件類型前綴
這篇文章主要介紹了Vue文件如何轉(zhuǎn)換成base64并去除多余的文件類型前綴問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03