一文解決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)文章
axios發(fā)送post請求,提交圖片類型表單數(shù)據(jù)方法
下面小編就為大家分享一篇axios發(fā)送post請求,提交圖片類型表單數(shù)據(jù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03vxe-table?實現(xiàn)表格數(shù)據(jù)分組功能(按指定字段數(shù)據(jù)分組)
文章介紹了如何使用樹結(jié)構(gòu)實現(xiàn)表格數(shù)據(jù)分組,并提供了官方文檔的鏈接,本文結(jié)合實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-11-11淺談在vue中用webpack打包之后運行文件的問題以及相關(guān)配置方法
下面小編就為大家分享一篇淺談在vue中用webpack打包之后運行文件的問題以及相關(guān)配置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02Vue.js第一天學(xué)習(xí)筆記(數(shù)據(jù)的雙向綁定、常用指令)
這篇文章主要為大家分享了Vue.js第一天的學(xué)習(xí)筆記,包括數(shù)據(jù)的雙向綁定、常用指令學(xué)習(xí),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12