Skip to content

xtf-virtual-list

组件说明

xtf-virtual-list 是虚拟列表组件,适用于大量数据渲染;仅挂载可视区条目,并支持动态高度和吸顶项。 核心能力以当前 xtf-virtual-list.vue 源码为准。


基础用法

1. 基础用法

最小配置下即可完成常见业务交互:

vue
<template>
  <xtf-virtual-list :items="items" style="height: 400rpx">
    <template #default="{ item }"><xtf-cell :title="item.title" /></template>
  </xtf-virtual-list>
</template>

<script>
export default {
  data() {
    return {
      value: '',
      items: Array.from({ length: 80 }, (_, index) => ({
        id: index,
        title: '列表项 ' + (index + 1)
      }))
    }
  },
  methods: {
    onEvent(payload) {
      console.log('组件事件', payload)
    }
  }
}
</script>

2. 核心交互

监听组件事件并维护页面状态:

vue
<template>
  <xtf-virtual-list
    :items="items"
    dynamic
    auto-measure
    style="height: 400rpx"
    @visible-range-change="onEvent"
    ><template #default="{ item, measure }"
      ><xtf-cell :title="item.title" @tap="measure(88)"
    /></template>
  </xtf-virtual-list>
</template>

<script>
export default {
  data() {
    return {
      value: '',
      items: Array.from({ length: 80 }, (_, index) => ({
        id: index,
        title: '列表项 ' + (index + 1)
      }))
    }
  },
  methods: {
    onEvent(payload) {
      console.log('组件事件', payload)
    }
  }
}
</script>

3. 进阶配置

组合布局、样式或插槽能力:

vue
<template>
  <xtf-virtual-list
    ref="list"
    :items="items"
    :item-height="88"
    style="height: 400rpx"
    @scroll-to-lower="onEvent"
  >
    <template #default="{ item, isEven, itemHeight, isSticky }">
      <view
        :style="{
          height: itemHeight + 'px',
          padding: '24rpx 32rpx',
          boxSizing: 'border-box',
          background: isSticky ? '#e6f4ff' : isEven ? '#f7f8fa' : '#ffffff'
        }"
      >
        <text>{{ item.title }}</text>
      </view>
    </template>
  </xtf-virtual-list>
</template>

<script>
export default {
  data() {
    return {
      value: '',
      items: Array.from({ length: 80 }, (_, index) => ({
        id: index,
        title: '列表项 ' + (index + 1)
      }))
    }
  },
  methods: {
    onEvent(payload) {
      console.log('组件事件', payload)
    }
  }
}
</script>

全部属性

属性类型默认值作用描述适用范围
itemsArrayfunction() {控制组件的 items 行为。按业务需要设置。
stickyItemsArrayfunction() {控制组件的 stickyItems 行为。按业务需要设置。
stickyFieldString''控制组件的 stickyField 行为。按业务需要设置。
stickyOffsetNumber0控制组件的 stickyOffset 行为。按业务需要设置。
itemHeight[String, Number]88控制组件的 itemHeight 行为。按业务需要设置。
dynamicBooleanfalse控制组件的 dynamic 行为。按业务需要设置。
minItemHeight[String, Number]88控制组件的 minItemHeight 行为。按业务需要设置。
getItemHeightFunctionnull控制组件的 getItemHeight 行为。按业务需要设置。
autoMeasureBooleanfalse控制组件的 autoMeasure 行为。按业务需要设置。
bufferSize[String, Number]4控制组件的 bufferSize 行为。按业务需要设置。
keyFieldString'id'控制组件的 keyField 行为。按业务需要设置。
visibleCount[String, Number]0控制组件的 visibleCount 行为。按业务需要设置。
overscan[String, Number]2控制组件的 overscan 行为。按业务需要设置。
showScrollbarBooleantrue控制组件的 showScrollbar 行为。按业务需要设置。
bouncesBooleantrue控制组件的 bounces 行为。按业务需要设置。
loadingMoreBooleanfalse控制组件的 loadingMore 行为。按业务需要设置。
finishedBooleanfalse控制组件的 finished 行为。按业务需要设置。
loadingTextString''控制组件的 loadingText 行为。按业务需要设置。
finishedTextString''控制组件的 finishedText 行为。按业务需要设置。
customClassString''控制组件的 customClass 行为。按业务需要设置。
customStyle[String, Object]''控制组件的 customStyle 行为。按业务需要设置。

数据项结构

字段类型默认值说明
id/keyString | Numberindex稳定键;列表、轮播和瀑布流项目建议提供。
title/label/nameString''展示名称,具体读取字段见组件对应 prop。
disabledBooleanfalse禁用可交互项目。

事件

事件名称触发时机回调参数参数说明
scroll对应交互或状态变化时触发。payload参数形状以源码 $emit('scroll', ...) 为准。
scroll-to-lower对应交互或状态变化时触发。payload参数形状以源码 $emit('scroll-to-lower', ...) 为准。
height-change对应交互或状态变化时触发。payload参数形状以源码 $emit('height-change', ...) 为准。
visible-range-change对应交互或状态变化时触发。payload参数形状以源码 $emit('visible-range-change', ...) 为准。

事件使用示例

vue
<template>
  <xtf-virtual-list @scroll="onEvent" />
</template>
<script>
export default {
  methods: {
    onEvent(payload) {
      console.log('收到事件', payload)
    }
  }
}
</script>

插槽

插槽名说明作用域参数
default自定义可视区列表项。itemindexisEvenitemHeightisStickymeasure

方法

scrollToIndex(index)scrollToTop()scrollToKey(key)refresh()updateItemHeight(index, height)

使用示例:

js
this.$refs.list.refresh()

主题说明

  • var(--xtf-color-surface) 参与组件的颜色、背景、边框或动效呈现。
  • var(--xtf-color-text-secondary) 参与组件的颜色、背景、边框或动效呈现。
  • 视觉变体的可选值以 variantthemelayout 等属性在源码中的分支为准。

如需全局调整主题,请在 xtf-config-provider 或主题配置中覆盖上述 CSS 变量。

MIT Licensed