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