1.自定义一个table的高度tableHeight
<el-table
@row-click="lookDetail"
v-loadmore="loadMore"
v-loading="listLoading"
:data="tableData"
:height="tableHeight"
border
style="width: 100%">
<el-table-column
:key="item.prop"
v-for="item in tableColumn"
:prop="item.prop"
:label="item.name">
</el-table-column>
</el-table>
2.data中给tableHeight一个默认的高度
tableHeight: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight - 240,(这里的240是减去表格上面导航和搜索条件的高度)
3.在mounted中初始化数据
mounted() {
const that = this
window.onresize = () => {
return (() => {
window.tableHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
that.tableHeight = window.tableHeight - that.$refs.queryHeight.offsetHeight - 150
})()
}
},
4.在watch中监听高度的变化
watch: {
// 这里的定时器是为了优化,如果频繁调用window.onresize方法会造成页面卡顿,增加定时器会避免频繁调用window.onresize方法
timer默认值设置为false,这里相当于一个按钮,防止频繁改变时引起卡顿
tableHeight(val) {
if (!this.timer) {
this.tableHeight = val
this.timer = true
const that = this
setTimeout(function() {
that.timer = false
}, 400)
}
}
},
2024-06-05 element table高度自适应
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 这个问题困扰了很久,查了很多资料,各种解决办法,但是都不咋地 终于找到一个合适的方法,话不多说,上代码 1.把高度...
- 解决element的Table表格组件的高度问题( height只能是数字或者字符串 ),实现height: ca...
- 项目中表格需要自适应高度,否则表格会撑开浏览器页面出现2个滚动条。不好看,做一个自定义的高度 首先在 src目录下...