FxVirtualList 虚拟列表
大数据量列表 / 网格的虚拟滚动组件,只渲染可见窗口 + 预渲染项,避免万级 DOM 节点卡顿。采用 builder 工厂 + render 函数模式,4 个组件共享滚动/渲染逻辑,仅注入不同的尺寸计算函数。
| 组件 | 维度 | 尺寸 | 适用 |
|---|---|---|---|
| FxFixedSizeList | 一维 | 行高固定 | 长列表,项高度一致 |
| FxDynamicSizeList | 一维 | 行高按索引变化 | 长列表,项高度不等 |
| FxFixedSizeGrid | 二维 | 行高/列宽固定 | 表格 / 矩阵 |
| FxDynamicSizeGrid | 二维 | 行高/列宽按索引变化 | 不规则表格 |
FxVirtualList 属于 @fx/components。
何时使用
- 列表 / 表格项数 > 200,全量渲染出现卡顿
- 选择器 / 树形 / 表格内部需要长列表渲染
固定行高列表(FxFixedSizeList)
itemSize 为数字,total 指定总项数,容器需给固定 height。
动态行高列表(FxDynamicSizeList)
itemSize 为函数 (idx) => number,estimatedItemSize 为未测量项的预估行高。内部用二分 + 指数搜索定位。
横向列表(layout="horizontal")
layout="horizontal" 时切换为横向虚拟滚动:itemSize 表示每项宽度,width 是可见宽,纵向滚动条自动切换为底部横向滚动条。
固定网格(FxFixedSizeGrid)
total-row × total-column 网格,row-height / column-width 为数字。Grid 容器 overflow: hidden,滚动靠 wheel / 触摸 / scrollTo,配套横向 + 纵向两条自定义滚动条。
动态网格(FxDynamicSizeGrid)
row-height / column-width 为函数,按索引返回尺寸;estimated-row-height / estimated-column-width 为未测量项的预估值。
API
List props(FxFixedSizeList / FxDynamicSizeList)
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| total | 总项数 | number | (必填) |
| itemSize | 行高。FixedSize 传数字;DynamicSize 传函数 | number | ((idx) => number) | (必填) |
| height | 容器高 | string | number | (必填) |
| width | 容器宽(横向 layout 时必填) | string | number | — |
| layout | 滚动方向 | 'vertical' | 'horizontal' | vertical |
| cache | 上下预渲染项数 | number | 2 |
| init-scroll-offset | 初始滚动偏移 | number | 0 |
| estimated-item-size | 预估行高(仅 DynamicSize) | number | 50 |
| use-is-scrolling | 向 slot 提供 isScrolling | boolean | false |
| direction | 书写方向 | 'ltr' | 'rtl' | ltr |
| inner-width | 内层宽(横向 layout) | string | number | — |
| scrollbar-always-on | 自定义滚动条常驻 | boolean | false |
Grid props(FxFixedSizeGrid / FxDynamicSizeGrid)
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| total-row | 总行数 | number | (必填) |
| total-column | 总列数 | number | (必填) |
| row-height | 行高。FixedSize 数字;DynamicSize 函数 | number | ((idx) => number) | (必填) |
| column-width | 列宽。FixedSize 数字;DynamicSize 函数 | number | ((idx) => number) | (必填) |
| height | 容器高 | string | number | (必填) |
| width | 容器宽 | string | number | (必填) |
| row-cache | 行预渲染数 | number | 2 |
| column-cache | 列预渲染数 | number | 2 |
| init-scroll-left | 初始横向偏移 | number | 0 |
| init-scroll-top | 初始纵向偏移 | number | 0 |
| estimated-row-height | 预估行高(仅 DynamicSize) | number | 50 |
| estimated-column-width | 预估列宽(仅 DynamicSize) | number | 50 |
| item-key | 单元格 key 生成函数 | (args) => string | number | ${row}:${col} |
| use-is-scrolling | 向 slot 提供 isScrolling | boolean | false |
| direction | 书写方向 | 'ltr' | 'rtl' | ltr |
| h-scrollbar-size | 横向滚动条尺寸(px) | number | 6 |
| v-scrollbar-size | 纵向滚动条尺寸(px) | number | 6 |
| scrollbar-start-gap | 滚动条起始间距 | number | 0 |
| scrollbar-end-gap | 滚动条末尾间距 | number | 2 |
| role | aria role | string | — |
Slots
List default:{ index, style, data?, isScrolling? }Grid default:{ rowIndex, columnIndex, style, data?, isScrolling? }
style必须绑定到项根元素(组件靠 absolute 定位摆放每项)。
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| item-rendered | 可见区渲染变化 | List: (cacheStart, cacheEnd, visibleStart, visibleEnd);Grid: (params) 对象含行列的 cacheStart/End + visibleStart/End |
| scroll | 滚动时 | List: (scrollDir, scrollOffset, updateRequested);Grid: (params) 对象含 xAxisScrollDir / yAxisScrollDir / scrollLeft / scrollTop / updateRequested |
Exposes
List:scrollTo(offset) / scrollToItem(idx, alignment?) / states / windowRef / innerRef / getItemStyleCache / resetScrollTop
Grid:scrollTo({ scrollLeft?, scrollTop? }) / scrollToItem(rowIdx?, colIdx?, alignment?) / states / windowRef / innerRef / getItemStyleCache / touchStartX / touchStartY / handleTouchStart / handleTouchMove
alignment 取值:'auto' | 'smart' | 'start' | 'center' | 'end'
说明
- List 项
style必须绑定:slot 根元素:style="style",组件靠 absolute 定位摆放每项。 - List 隐藏原生滚动条:List 用自定义滚动条,原生滚动条需自行隐藏(
:deep(.fx-vl__window) { scrollbar-width: none })。 - Grid 容器
overflow: hidden:不显示原生滚动条,滚动靠 wheel / 触摸 /scrollTo。
