支持表格动态行高与虚拟滚动的表格组件
内容长度不一的单元格如果强制单行截断,会丢失信息;而允许换行后行高不一致,又会给虚拟滚动的定位带来挑战。vxe-table 支持动态行高与横向、纵向虚拟滚动同时开启:单元格内容自动撑高行高,行列数量很大时只渲染可视区域,兼顾信息完整与性能。
但这两项能力组合时有一个已知现象:横向虚拟滚动下,未渲染的右侧列滚入可视区时,如果内容比当前行更高,行高会突然变化。本文将说明如何配置、这一现象的原因,以及如何在两者之间取舍。
vxe-table 如何同时实现动态行高与虚拟滚动?
设置 showOverflow: false 允许单元格内容换行并撑高行高;在 virtualXConfig 与 virtualYConfig 中分别设置 enabled: true、gt: 0 开启横向与纵向虚拟滚动;表格设置固定 height,虚拟滚动才能正常计算可视区域。
- showOverflow 必须为 false。默认值通常为 false,会截断超出列宽的内容为单行,行高固定,动态行高不会生效。
- 表格必须设置固定 height,否则纵向虚拟滚动无法计算可视范围。
- 横向虚拟滚动在列总宽度明显超过容器宽度时才有意义;示例配置了 100 列以触发横向滚动。
- 虚拟滚动的 gt 为阈值,设为 0 表示始终启用。
单元格内容较长时自动换行,行高随内容增高;纵向滚动只渲染可视行,横向滚动只渲染可视列;切换数据量(3 行到 30000 行)时加载与滚动保持流畅。
代码

<template>
<div>
<vxe-grid v-bind="gridOptions"></vxe-grid>
</div>
</template>
<script setup>
import { ref, reactive, nextTick } from 'vue'
import { VxeUI } from 'vxe-table'
const rowSize = ref(100)
const imgUrlCellRender = reactive({
name: 'VxeImage',
props: {
width: 36,
height: 36
}
})
const gridOptions = reactive({
border: true,
loading: false,
showOverflow: false,
height: 800,
columnConfig: {
resizable: true
},
virtualXConfig: {
enabled: true,
gt: 0
},
virtualYConfig: {
enabled: true,
gt: 0
},
columns: [
{ type: 'checkbox', width: 60 },
{ title: '列0', field: 'col0', width: 100 },
{ title: '列1', field: 'imgUrl', width: 80, cellRender: imgUrlCellRender },
{ title: '列2', field: 'col2', width: 160 },
{ title: '列3', field: 'col3', width: 200 },
{ title: '列4', field: 'col4', width: 140 },
{ title: '列5', field: 'col5', width: 300 },
{ title: '列6', field: 'col6', width: 160 },
{ title: '列7', field: 'col7', width: 120 },
{ title: '列8', field: 'col8', width: 400 },
{ title: '列9', field: 'col9', width: 160 },
{ title: '列10', field: 'col10', width: 160 },
{ title: '列11', field: 'col11', width: 180 },
{ title: '列12', field: 'col12', width: 160 },
{ title: '列13', field: 'col13', width: 80 },
{ title: '列14', field: 'col14', width: 120 },
{ title: '列15', field: 'col15', width: 360 },
{ title: '列16', field: 'col16', width: 150 },
{ title: '列17', field: 'col17', width: 380 },
{ title: '列18', field: 'col18', width: 100 },
{ title: '列19', field: 'col19', width: 290 },
{ title: '列20', field: 'col20', width: 80 },
{ title: '列21', field: 'col21', width: 100 },
{ title: '列22', field: 'col22', width: 120 },
{ title: '列23', field: 'col23', width: 270 },
{ title: '列24', field: 'col24', width: 330 },
{ title: '列25', field: 'col25', width: 460 },
{ title: '列26', field: 'col26', width: 280 },
{ title: '列27', field: 'col27', width: 220 },
{ title: '列28', field: 'col28', width: 120 },
{ title: '列29', field: 'col29', width: 180 },
{ title: '列30', field: 'col30', width: 500 },
{ title: '列31', field: 'col31', width: 600 },
{ title: '列32', field: 'col32', width: 100 },
{ title: '列33', field: 'col33', width: 490 },
{ title: '列34', field: 'col34', width: 100 },
{ title: '列35', field: 'col35', width: 150 },
{ title: '列36', field: 'col36', width: 800 },
{ title: '列37', field: 'col37', width: 400 },
{ title: '列38', field: 'col38', width: 800 },
{ title: '列39', field: 'col39', width: 360 },
{ title: '列40', field: 'col40', width: 420 },
{ title: '列41', field: 'col41', width: 100 },
{ title: '列42', field: 'col42', width: 120 },
{ title: '列43', field: 'col43', width: 280 },
{ title: '列44', field: 'col44', width: 170 },
{ title: '列45', field: 'col45', width: 370 },
{ title: '列46', field: 'col46', width: 420 },
{ title: '列47', field: 'col47', width: 170 },
{ title: '列48', field: 'col48', width: 400 },
{ title: '列49', field: 'col49', width: 220 },
{ title: '列50', field: 'col50', width: 170 },
{ title: '列51', field: 'col51', width: 160 },
{ title: '列52', field: 'col52', width: 500 },
{ title: '列53', field: 'col53', width: 280 },
{ title: '列54', field: 'col54', width: 170 },
{ title: '列55', field: 'col55', width: 370 },
{ title: '列56', field: 'col56', width: 120 },
{ title: '列57', field: 'col57', width: 170 },
{ title: '列58', field: 'col58', width: 400 },
{ title: '列59', field: 'col59', width: 220 },
{ title: '列60', field: 'col60', width: 650 },
{ title: '列61', field: 'col61', width: 600 },
{ title: '列62', field: 'col62', width: 100 },
{ title: '列63', field: 'col63', width: 490 },
{ title: '列64', field: 'col64', width: 100 },
{ title: '列65', field: 'col65', width: 150 },
{ title: '列66', field: 'col66', width: 800 },
{ title: '列67', field: 'col67', width: 400 },
{ title: '列68', field: 'col68', width: 800 },
{ title: '列69', field: 'col69', width: 360 },
{ title: '列70', field: 'col70', width: 650 },
{ title: '列71', field: 'col71', width: 600 },
{ title: '列72', field: 'col72', width: 100 },
{ title: '列73', field: 'col73', width: 490 },
{ title: '列74', field: 'col74', width: 100 },
{ title: '列75', field: 'col75', width: 150 },
{ title: '列76', field: 'col76', width: 800 },
{ title: '列77', field: 'col77', width: 400 },
{ title: '列78', field: 'col78', width: 800 },
{ title: '列79', field: 'col79', width: 360 },
{ title: '列80', field: 'col80', width: 650 },
{ title: '列81', field: 'col81', width: 600 },
{ title: '列82', field: 'col82', width: 100 },
{ title: '列83', field: 'col83', width: 490 },
{ title: '列84', field: 'col84', width: 100 },
{ title: '列85', field: 'col85', width: 150 },
{ title: '列86', field: 'col86', width: 800 },
{ title: '列87', field: 'col87', width: 400 },
{ title: '列88', field: 'col88', width: 800 },
{ title: '列89', field: 'col89', width: 360 },
{ title: '列90', field: 'col90', width: 650 },
{ title: '列91', field: 'col91', width: 600 },
{ title: '列92', field: 'col92', width: 100 },
{ title: '列93', field: 'col93', width: 490 },
{ title: '列94', field: 'col94', width: 100 },
{ title: '列95', field: 'col95', width: 150 },
{ title: '列96', field: 'col96', width: 800 },
{ title: '列97', field: 'col97', width: 400 },
{ title: '列98', field: 'col98', width: 800 },
{ title: '列99', field: 'col99', width: 360 },
{ title: '列100', field: 'col100', width: 360 }
],
data: []
})
const loadList = () => {
gridOptions.loading = true
setTimeout(() => {
const dataList = []
for (let i = 0; i < rowSize.value; i++) {
const item = {
id: 1e4 + i,
imgUrl: i % 3 === 0 ? 'https://vxeui.com/resource/img/546.gif' : 'https://vxeui.com/resource/img/673.gif'
}
for (let j = 0; j < 100; j++) {
if (i % 9 === 0) {
item[`col${j}`] = `值_${i}_${j} 内容9内容9 内容9内容9内容9 内容9内容9内容9内容9 内容9内容9内容9内容9 内容9内容9内容9 内容9内容9`
} else if (i % 8 === 0) {
item[`col${j}`] = `值_${i}_${j} 内容8内容8内容8内容8`
} else if (i % 7 === 0) {
item[`col${j}`] = `值_${i}_${j} 内容7内容7`
} else if (i % 6 === 0) {
item[`col${j}`] = `值_${i}_${j} 内容6内容6内容6内容6内容6内容6内容6内容6`
} else if (i % 5 === 0) {
item[`col${j}`] = `值_${i}_${j} 内容5内容5内容5内容5内容5`
} else if (i % 4 === 0) {
item[`col${j}`] = `值_${i}_${j} 内容4内容4内容4内容4内容4内容4内容4内容4内容4内容4内容4内容4`
} else {
item[`col${j}`] = `值_${i}_${j}`
}
}
dataList.push(item)
}
const startTime = Date.now()
gridOptions.data = dataList
gridOptions.loading = false
nextTick(() => {
VxeUI.modal.message({
content: `加载时间 ${Date.now() - startTime} 毫秒`,
status: 'success'
})
})
}, 350)
}
loadList()
</script>
参数说明
1. 动态行高的开关
showOverflow: false
- 默认 showOverflow: false 时,超出列宽的内容被截断为单行,行高固定。
- 也可以不设置,当时可能倍全局参数设置为 true,设为 false 后,单元格内容允许换行,行高由内容撑开,形成动态行高。
- 这是动态行高生效的前提,未关闭溢出截断时,后续虚拟滚动配置都不会改变行高。
2. 双向虚拟滚动
virtualXConfig: {
enabled: true,
gt: 0
},
virtualYConfig: {
enabled: true,
gt: 0
}
| 配置 | 作用 | 说明 |
|---|---|---|
| virtualXConfig | 横向虚拟滚动 | 只渲染可视列,适合列数很多(示例 100 列)的场景 |
| virtualYConfig | 纵向虚拟滚动 | 只渲染可视行,适合数据量很大(示例 30000 行)的场景 |
| enabled | 总开关 | 必须为 true |
| gt | 启用阈值 | 数据量超过该值才启用;0 表示始终启用 |
3. 固定表格高度
height: 800
动态行高与横向虚拟滚动的已知现象
同时开启动态行高与横向虚拟滚动时,横向滚动过程中可能出现行高突然变高。
- 横向虚拟滚动只会渲染可视区域内的列,非可视列在滚动到可视区之前不渲染。行高由当前已渲染单元格中内容最高的那个决定:
- 初始状态下,左侧列已渲染,内容较短,行高较矮。
- 向右滚动时,右侧列进入可视区并开始渲染。
- 如果右侧某列的内容比左侧已渲染列更高,该行需要重新计算高度。
- 行高从较矮跳到较高,表现为“单元格突然变高”。
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| showOverflow | Boolean | true | 是否截断溢出内容;设为 false 才能动态行高 |
| height | Number / String | — | 表格高度,虚拟滚动依赖固定高度 |
| virtualXConfig.enabled | Boolean | false | 是否启用横向虚拟滚动 |
| virtualXConfig.gt | Number | — | 横向虚拟滚动启用阈值,0 表示始终启用 |
| virtualYConfig.enabled | Boolean | false | 是否启用纵向虚拟滚动 |
| virtualYConfig.gt | Number | — | 纵向虚拟滚动启用阈值,0 表示始终启用 |
| columnConfig.resizable | Boolean | false | 是否允许拖拽调整列宽 |
| 列 width | Number | — | 列宽,示例中列宽差异较大以触发横向滚动 |
| cellRender | Object | — | 单元格渲染器,示例用于渲染图片 |

浙公网安备 33010602011771号