el-table表头自适应宽度(Vue)
html部分
<el-table>
<el-table-column label="根据标题字数/大小自适应宽度" :render-header="linefeed"></el-table-column>
</el-table>
js部分
methods: {
linefeed (h,{column,index}) { // column,index都是el-table中自带的
let number = column.label.length // 表头字数
let size = 16 // 字体尺寸
column.minWidth = number*size // 计算宽度
return h('div',{class:'table-head',style:{width:'100%'}},[column.label])
}
}
效果展示(下图为其他项目截图,但与上述代码效果一致)

语录分享

本文介绍了一种在Vue框架中实现el-table表头自适应宽度的方法。通过计算表头文字长度与字体尺寸,动态设置列的最小宽度,使得表头宽度能够根据标题的字数自动调整,提供更佳的视觉体验。





