一文解決vue2 element el-table自適應(yīng)高度問題
解決element表格自適應(yīng)高度問題
在寫公司后臺項目的時候遇到一個需求,要求表格頁面不能有滾動條,所以必須封裝一個公共方法來實現(xiàn)表格自適應(yīng)高度
第一步 實現(xiàn)自定義指令去監(jiān)聽表格元素高度變化
我這邊使用封住思想 首先創(chuàng)建在obSize文件夾下創(chuàng)建index.js內(nèi)容如下
const map = new WeakMap()
// ob監(jiān)聽
const ob = new ResizeObserver((entries) => {
for (const entry of entries) {
// 處理元素對應(yīng)的回調(diào)
const handler = map.get(entry.target)
if (handler) {
const box = entry.borderBoxSize[0]
// 循環(huán)entry.target 累加offsetTop 從而得到距離頁面頂部距離
let setTop = countTop(entry.target, 0)
handler({
width: box.inlineSize,
height: box.blockSize,
entry: entry,
bodHeight: window.document.body.clientHeight,
setTop: setTop,
tableHeight: (window.document.body.clientHeight - setTop - 50 - box.inlineSize) > 0 ? '' : window.document.body.clientHeight - setTop - 72
})
}
}
})
export function countTop(el, topn) {
if (el.offsetParent) {
return countTop(el.offsetParent, topn + el.offsetTop)
} else {
return topn
}
}
export default {
inserted(el, binding) {
ob.observe(el)
// 監(jiān)聽el元素尺度的變化
map.set(el, binding.value)
},
unbind(el) {
// 取消監(jiān)聽
ob.unobserve(el)
},
}
注冊vue指令在directive文件夾index.js文件中
import obSize from './obSize'
const install = function (Vue) {
//這里可以放入多個自定義指令
Vue.directive('ob-size', obSize)
}
if (window.Vue) {
Vue.use(install); // eslint-disable-line
}
export default install
在main.js引入directive
import directive from './directive' Vue.use(directive)
第二步 封裝mixins
在mixins文件夾內(nèi)創(chuàng)建estimateTableHeight.js內(nèi)容如下 70 代表距離頁面底部的高度
import { countTop } from '@/directive/obSize'
export default {
data() {
return {
tableHeight: 0,
}
},
methods: {
handleSizeChange(e) {
this.tableHeight = e.tableHeight;
},
},
// 監(jiān)聽showSearch
watch: {
showSearch(val) {
// 獲取element table元素
const dome = document.getElementsByClassName('el-table');
let setTop = countTop(dome, 0)
this.tableHeight = window.document.body.clientHeight - setTop - 70
},
},
mounted() {
//監(jiān)聽頁面尺寸變化
window.addEventListener('resize', () => {
const dome = document.getElementsByClassName('el-table');
let setTop = countTop(dome, 0)
this.tableHeight = window.document.body.clientHeight - setTop - 70
});
},
}
第三步 在頁面引入
引入 import estimateTableHeight from "@/mixins/estimateTableHeight";
export default { mixins: [estimateTableHeight], }
在el-table上加入 v-ob-size="handleSizeChange" :height="tableHeight"
<el-table
v-ob-size="handleSizeChange"
:height="tableHeight"
v-loading="loading"
:data="List"
>
大功告成!
以上就是一文解決vue2 element el-table自適應(yīng)高度問題的詳細內(nèi)容,更多關(guān)于vue2 element el-table自適應(yīng)高度的資料請關(guān)注腳本之家其它相關(guān)文章!
- vue3+elementplus基于el-table-v2封裝公用table組件詳細代碼
- Vue3+Element?Plus實現(xiàn)el-table跨行顯示(非腳手架)
- vue3使用element-plus中el-table組件報錯關(guān)鍵字'emitsOptions'與'insertBefore'分析
- Vue2+Element-ui實現(xiàn)el-table表格自適應(yīng)高度的案例
- VUE2.0 ElementUI2.0表格el-table自適應(yīng)高度的實現(xiàn)方法
- Vue 3 中使用 Element Plus 的 `el-table` 組件實現(xiàn)自適應(yīng)高度
相關(guān)文章
使用Vue.js報錯:ReferenceError: “Vue is not d
在前端開發(fā)中,ReferenceError: "Vue is not defined" 是一個常見的錯誤,該錯誤通常發(fā)生在項目中未正確引入 Vue.js 框架或代碼配置存在問題時,本篇文章將詳細分析該錯誤的成因,并提供多種解決方案,幫助開發(fā)者快速排查問題,需要的朋友可以參考下2024-12-12
vue實現(xiàn)登錄頁面的驗證碼以及驗證過程解析(面向新手)
這篇文章主要介紹了vue實現(xiàn)登錄頁面的驗證碼以及驗證過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08
vue使用百度地圖報錯BMap?is?not?defined問題及解決
這篇文章主要介紹了vue使用百度地圖報錯BMap?is?not?defined問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

