不定高表格固定表头这是工作中很常遇到的需求。
不清楚这些做ui组件库的为什么不内置一套解决方案。。。
还得耗时费力地到网上搜,自己不断尝试去找寻处理办法。。。
虽然不想再用vue2
了,但工作中的老项目,没有办法。
本文便是针对 element-ui
的一个方案。
其他UI的方案请查阅:
- vue3 element-plus: soon-admin-vue3
- react antd: soon-admin-react-nextjs
<template>
<div class="h-screen flex flex-col">
<div ref="tableContainer" class="flex flex-col mt-2 overflow-hidden" :style="tableContainerStyle">
<el-table :data="tableData" height="full" />
</div>
</div>
</template>
<script>
import { debounce } from 'lodash-es'
export default {
data() {
return {
tableData: [],
tableContainerStyle: 'flex-1',
}
},
mounted() {
this.debounceResize = debounce(this.resizeEvent, 300)
window.addEventListener('resize', this.debounceResize)
},
methods: {
resizeEvent() {
const that = this
that.tableContainerStyle = 'flex:1'
this.$nextTick(() => {
const containerHeight = this.$refs.tableContainer.offsetHeight
const wrapper = that.$refs.table.$el.querySelector('.el-table__body-wrapper')
const header = that.$refs.table.$el.querySelector('.el-table__header-wrapper')
const headerHeight = header.offsetHeight
const dataTable = wrapper.children[0]
const tableHeight = dataTable.offsetHeight
// const wrapperHeight = wrapper.offsetHeight
if (tableHeight && tableHeight <= containerHeight - headerHeight) {
that.tableContainerStyle = `height:${tableHeight + headerHeight + 17}px`
// 17为水平滚动条高度,如果没有水平滚动条,可不加
}
})
},
getList() {
this.tableData = [...res.data] //获取远程数据
this.resizeEvent() //重新设置高度
},
},
}
</script>
支持一下
本人正在找工作,如在长三角有合适岗位,请联系我
热情地欢迎各位兄弟姐妹的大力支持!
github: github.com/leafio?tab=…
期待至少一个项目能达成1000个star
期待至少一个npm能达成每周1000次下载
- Soon-Admin TS全栈后台管理系统
- 前端vue3版:soon-admin-vue3
- 前端react版:soon-admin-react-nextjs
- 后端express:soon-admin-express
- soon-mock 一键生成增删查改,有ui的json-server,可js自定义逻辑
- soon-tools 分层级JSON转TS类型,JSON导出多级表头Excel。有网页版、npm、exe三种方式使用
- soon-i18n 类型安全的函数式i18n库,支持vue3、react、solid、svelte
- soon-storage 对于localStorage的类型安全封装
- soon-fetch 不到3K的组合式请求库
- react-vmodel 多层级objcet双向绑定,像vue一样简单