0 效果
1 添加自定義指令
新建目錄src/directive/el-table
在el-table目錄下新建文件adaptive.js
import { addResizeListener, removeResizeListener } from 'element-ui/src/utils/resize-event'
// 設(shè)置表格高度
const doResize = async(el, binding, vnode) => {
// 獲取表格Dom對(duì)象
const { componentInstance: $table } = await vnode
// 獲取調(diào)用傳遞過來的數(shù)據(jù)
const { value } = binding
if (!$table.height) {
throw new Error(`el-$table must set the height. Such as height='100px'`)
}
// 獲取距底部距離(用于展示頁(yè)碼等信息)
const bottomOffset = (value && value.bottomOffset) || 90
if (!$table) return
// 計(jì)算列表高度并設(shè)置
const height = window.innerHeight - el.getBoundingClientRect().top - bottomOffset
$table.layout.setHeight(height)
$table.doLayout()
}
export default {
// 初始化設(shè)置
bind(el, binding, vnode) {
// 設(shè)置resize監(jiān)聽方法
el.resizeListener = async() => {
await doResize(el, binding, vnode)
}
// 綁定監(jiān)聽方法到addResizeListener
addResizeListener(window.document.body, el.resizeListener)
},
// 綁定默認(rèn)高度
async inserted(el, binding, vnode) {
await doResize(el, binding, vnode)
},
// 銷毀時(shí)設(shè)置
unbind(el) {
// 移除resize監(jiān)聽
removeResizeListener(el, el.resizeListener)
}
}
在el-table目錄下新建index.js
import adaptive from './adaptive'
const install = function(Vue) {
// 綁定v-adaptive指令
Vue.directive('adaptive', adaptive)
}
if (window.Vue) {
window['adaptive'] = adaptive
// eslint-disable-next-line no-undef
Vue.use(install)
}
adaptive.install = install
export default adaptive
2 引入
2.1 單個(gè)vue文件引入
在所需使用的vue頁(yè)面中引入
import adaptive from ‘…/…/…/src/directive/el-table’
2.2 全局引入
在main.js中引入文章來源:http://www.zghlxwxcb.cn/news/detail-714628.html
3 使用
在el-table標(biāo)簽中添加屬性
v-adaptive:自定義指令,bottomOffset 代表距離底部的距離
height:高度屬性,100無具體意義,僅為初始值,不可省略文章來源地址http://www.zghlxwxcb.cn/news/detail-714628.html
到了這里,關(guān)于el-table添加固定高度height后高度自適應(yīng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!