Vue2+Element-ui實現(xiàn)el-table表格自適應(yīng)高度的案例
效果圖


新建指令
Vue.directive('height', {
inserted(el, _binding, vnode) {
const paginationRef = vnode.context.$refs.paginationRef
const calculateHeight = () => {
const windowHeight = window.innerHeight
const topOffset = el.getBoundingClientRect().top
const otherElementsHeight = 40
let paginationHeight = 0
if (paginationRef && paginationRef.totalPage) {
paginationHeight = 55
}
el.style.height = `${
windowHeight - topOffset - otherElementsHeight - paginationHeight
}px`
}
const debouncedCalculateHeight = _.debounce(calculateHeight, 500)
debouncedCalculateHeight()
window.addEventListener('resize', debouncedCalculateHeight)
el.__vueHeightDirective__ = debouncedCalculateHeight
},
unbind(el) {
window.removeEventListener('resize', el.__vueHeightDirective__)
delete el.__vueHeightDirective__
}
})頁面使用
1:el-table外層嵌套div,加指令v-height
2:el-table設(shè)置height=100%
<div v-height> <el-table height="100%"> </el-table-column> </el-table> </div>
注意,重點!
1:指令這一行 const paginationRef = vnode.context.$refs.paginationRef
用來判斷是否顯示分頁器,所以如果有分頁器需要在分頁器增加ref,如下:
<el-pagination ref="paginationRef"> </el-pagination>
2:至于這一行 paginationRef.totalPage,是分頁器的顯示與否,比如:
<el-pagination v-if="total > 0" ref="paginationRef"> </el-pagination>
至于我這里為什么叫totalPage,因為這個是經(jīng)過二次封裝的,沒封裝的就直接叫total。
同理 指令paginationRef.totalPage需要修改為paginationRef.total
3:有人問了paginationRef.total這個total是怎么來的,怎么命名的;
這個是官網(wǎng)的文檔的組件,paginationRef這里就會獲取你分頁器的props。
比如:

這個total就指的elementUI的分頁組件的props。一般因為是用來判斷total數(shù)量是不是大于0顯示;
大于0就顯示了分頁器,所以。。。懂了吧
4:又有人問了,為什么指令不直接寫在el-table里。這個你可以自己去試試,會發(fā)現(xiàn)滾動不了。
至于原因:因為這個指令設(shè)置的是單獨style的height高度,你審查元素就會發(fā)現(xiàn)el-table傳參的height 他需要同步設(shè)置為他下級元素樣式el-table__body-wrapper的高度,所以并不是設(shè)置style為height的高度這么簡單。
5:代碼的_.debounce是什么。這里是用的lodash的防抖函數(shù),自己寫一個防抖函數(shù)即可。
6:有的同學就要問了,那我直接封裝一個方法或者其他方法或者mixins來計算行不行
都可以,你想怎么用怎么用,mixins的話比如:
export default {
data() {
return {
tableHeight: 0
}
},
mounted() {
this.calculateAndSetHeight()
},
methods: {
calculateAndSetHeight() {
if (this.$refs.tableRef) {
const el = this.$refs.tableRef.$el
const calculateHeight = () => {
const windowHeight = window.innerHeight
const topOffset = el.getBoundingClientRect().top
const otherElementsHeight = 40
let paginationHeight = 0
const paginationRef = this.$refs.paginationRef
if (paginationRef && paginationRef.totalPage) {
paginationHeight = 55
}
this.tableHeight = `${
windowHeight - topOffset - otherElementsHeight - paginationHeight
}px`
}
const throttledCalculateHeight = _.debounce(calculateHeight, 500)
throttledCalculateHeight()
window.addEventListener('resize', throttledCalculateHeight)
// 在組件銷毀時移除事件監(jiān)聽器
this.$once('hook:destroyed', () => {
window.removeEventListener('resize', throttledCalculateHeight)
})
}
}
}
}頁面使用:
<el-table ref="tableRef" :height="tableHeight">
這樣就不用一個個標簽像v-height div包起來了,但是ref和height就必要。
效果都是一樣的,自己看著來。
ps:至于為什么要寫這個功能,你能點進來看說明就是公司有一些犟貨 覺得滾下去太麻煩了。或者是體驗不是很好的,數(shù)據(jù)太多屏幕太小總要滾下去。
另外其實后臺系統(tǒng)大部分用模板即可,有些模板自帶有這種功能。
到此這篇關(guān)于Vue2+Element-ui實現(xiàn)el-table表格自適應(yīng)高度的文章就介紹到這了,更多相關(guān)Vue2 Element-ui表格自適應(yīng)高度內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue elementui el-form rules動態(tài)驗證的實例代碼詳解
在使用elementUI el-form 中,對于業(yè)務(wù)不同的時候可能會產(chǎn)生不同表單結(jié)構(gòu),但是都是存在同一個表單控件el-form中。這篇文章主要介紹了vue elementui el-form rules動態(tài)驗證的實例代碼,需要的朋友可以參考下2019-05-05
vue中循環(huán)表格數(shù)據(jù)出現(xiàn)數(shù)據(jù)聯(lián)動現(xiàn)象(示例代碼)
在Vue中循環(huán)生成表格數(shù)據(jù)時,可能會遇到數(shù)據(jù)聯(lián)動的現(xiàn)象,即修改一個表格中的數(shù)據(jù)后,其他表格的數(shù)據(jù)也會跟著變化,這種現(xiàn)象通常是因為所有表格的數(shù)據(jù)引用了同一個對象或數(shù)組導(dǎo)致的,本文介紹vue中循環(huán)表格數(shù)據(jù)出現(xiàn)數(shù)據(jù)聯(lián)動現(xiàn)象,感興趣的朋友一起看看吧2024-11-11
vue+iview tabs context-menu 彈出框修改樣式的方法
今天遇到一個需求說頁面頂部的菜單右鍵彈出框離得有點遠,需要我們做調(diào)整,下面小編給大家分享下vue+iview tabs context-menu 彈出框修改樣式的方法,感興趣的朋友跟隨小編一起看看吧2024-06-06
Vue項目字體文件打包后fonts文件夾消失的原因分析與解決方案
在 Vue 項目中,將字體文件放在 src/assets/fonts 目錄下,打包后在瀏覽器中訪問發(fā)現(xiàn)路徑里不見了 fonts文件夾,這通常不是文件丟失,而是 Vue 的構(gòu)建工具對靜態(tài)資源進行了自動化處理,本文給大家介紹了Vue項目字體文件打包后fonts文件夾消失的原因分析與解決方案2026-06-06
Vue結(jié)合leaflet實現(xiàn)克里金插值
本文主要介紹了Vue結(jié)合leaflet實現(xiàn)克里金插值,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06

