Skip to content

xtf-index-list

组件说明

xtf-index-list 是索引列表组件,基于 xtf-index-bar 封装。通过数据驱动自动生成索引分组列表,支持自定义字段映射和单元格样式,适用于城市选择、联系人列表和分类导航等场景。


基础用法

1. 基础用法

通过 dataSource 传入分组数据:

vue
<template>
  <xtf-index-list :data-source="cityList" height="100vh" clickable @item-click="onItemClick" />
</template>

<script>
export default {
  data() {
    return {
      cityList: [
        { name: 'A', items: [{ name: '安阳' }, { name: '鞍山' }] },
        { name: 'B', items: [{ name: '北京' }, { name: '保定' }] },
        { name: 'C', items: [{ name: '成都' }, { name: '重庆' }] }
      ]
    }
  },
  methods: {
    onItemClick(data) {
      console.log('选择城市:', data.item.name)
    }
  }
}
</script>

2. 自定义字段

通过 fieldNames 映射数据字段:

vue
<template>
  <xtf-index-list
    :data-source="contactList"
    :field-names="{ title: 'label', desc: 'phone' }"
    height="100vh"
  />
</template>

<script>
export default {
  data() {
    return {
      contactList: [
        { name: 'A', items: [{ label: '安琪', phone: '138****1234' }] },
        { name: 'B', items: [{ label: '白雪', phone: '139****5678' }] }
      ]
    }
  }
}
</script>

3. 自定义列表项与索引事件

使用 item 插槽渲染业务字段,并监听列表项和侧栏索引事件:

vue
<template>
  <xtf-index-list
    :data-source="groups"
    height="500rpx"
    clickable
    @item-click="onItem"
    @index-select="onIndex"
  >
    <template #item="{ item }">
      <view style="padding: 20rpx 24rpx">
        <text>{{ item.name }}</text>
        <text style="margin-left: 16rpx">{{ item.phone }}</text>
      </view>
    </template>
  </xtf-index-list>
</template>

<script>
export default {
  data() {
    return {
      groups: [
        { name: 'A', items: [{ name: '安琪', phone: '13800000000' }] },
        { name: 'B', items: [{ name: '白雪', phone: '13900000000' }] }
      ]
    }
  },
  methods: {
    onItem({ item }) {
      uni.showToast({ title: item.name, icon: 'none' })
    },
    onIndex({ name }) {
      console.log('切换索引:', name)
    }
  }
}
</script>

全部属性

属性类型默认值作用描述适用范围
dataSourceArray[]分组数据数据
indexListArray[]索引字符列表索引
heightString | Number'100%'滚动区域高度布局
highlightColorString''高亮颜色样式
anchorTextColorString'secondary'锚点文字颜色样式
stickyBooleantrue是否吸顶吸顶
clickableBooleanfalse是否可点击交互
fieldNamesObject{ title: 'name', desc: '', icon: '', thumb: '' }字段映射数据
customClassString''自定义类名样式覆盖
customStyleString | Object''自定义样式动态样式覆盖
sidebarClassString''侧栏自定义类名侧栏
sidebarStyleString | Object''侧栏自定义样式侧栏
sidebarPaddingString | Number''侧栏内边距侧栏
sidebarRadiusString | Number''侧栏圆角侧栏
sidebarBackgroundString''侧栏背景色侧栏
sidebarBorderString''侧栏边框侧栏
sidebarShadowString''侧栏阴影侧栏
sidebarRightString | Number''侧栏距右侧距离侧栏
sidebarItemClassString | Function''侧栏索引项类名;函数接收索引栏组件的参数侧栏项
sidebarItemStyleString | Object | Function''侧栏索引项样式;函数接收索引栏组件的参数侧栏项
sidebarItemSizeString | Number''侧栏索引项尺寸侧栏项
sidebarItemRadiusString | Number''侧栏索引项圆角侧栏项
sidebarItemColorString''侧栏索引项文字颜色侧栏项
sidebarItemActiveColorString''激活侧栏索引项文字颜色侧栏项
sidebarItemActiveBackgroundString''激活侧栏索引项背景色侧栏项
sidebarItemActiveScaleString | Number''激活侧栏索引项缩放比例侧栏项
toastClassString''索引提示层类名提示层
toastStyleString | Object''索引提示层样式提示层
toastSizeString | Number''索引提示层尺寸提示层
toastRadiusString | Number''索引提示层圆角提示层
toastBackgroundString''索引提示层背景色提示层
toastColorString''索引提示层文字颜色提示层
groupClassString | Function''分组容器类名;函数接收 { group, groupIndex }分组
groupGapString | Number8分组底部间距分组
groupPaddingString | Number | Object''分组内边距分组
groupStyleString | Object | Function''分组容器样式;函数接收 { group, groupIndex }分组
anchorClassString | Function''锚点类名;函数接收 { group, groupIndex }锚点
anchorPaddingString | Number | Object''锚点内边距锚点
anchorMarginString | Number | Object''锚点外边距锚点
anchorRadiusString | Number''锚点圆角锚点
anchorBackgroundString''锚点背景色锚点
anchorBorderString''锚点边框锚点
anchorShadowString''锚点阴影锚点
anchorStyleString | Object | Function''锚点样式;函数接收 { group, groupIndex }锚点
cellClassString | Function''单元格类名;函数接收 { group, item, index, groupIndex }单元格
cellStyleString | Object | Function''单元格样式;函数接收 { group, item, index, groupIndex }单元格
itemClassString | Function''列表项附加类名;与 cellClass 合并列表项
itemStyleString | Object | Function''列表项附加样式;与 cellStyle 合并列表项
cellVariantString'soft'单元格变体,可选 surfacesoftcardglass单元格
cellThemeString'primary'单元格主题,可选 primarysuccesswarningdangerinfo单元格
cellSizeString'md'单元格尺寸,可选 smmdlg单元格
cellRadiusString | Number''单元格圆角单元格
cellPaddingXString | Number''单元格水平内边距单元格
cellPaddingYString | Number''单元格垂直内边距单元格

事件

事件名称触发时机回调参数参数说明
item-click点击列表项时触发({ group, item, index })group 为分组名 String,另含项数据和索引
select点击列表项时触发({ group, item, index })item-click 同时触发;group 为分组名 String
index-select触摸侧栏索引时触发({ index, name })索引位置和字符

插槽

插槽名称说明
item自定义列表项内容

插槽示例

vue
<template>
  <xtf-index-list :data-source="groups" height="500rpx" clickable>
    <template #item="{ item }">
      <view>
        <text>{{ item.name }}</text>
<text>{{ item.phone }}</text></view>
</template>

  </xtf-index-list>
</template>
<script>
export default {
  data() {
    return { groups: [{ name: 'A', items: [{ name: '安琪', phone: '13800000000' }] }] }
  }
}
</script>

方法

方法名参数返回值说明
scrollTo(index: String | Number)-委托 xtf-index-bar 滚动到指定索引

侧栏与提示层属性直接透传至 xtf-index-bargroup*anchor* 的函数型值接收 { group, groupIndex }cell*item* 的函数型值接收 { group, item, index, groupIndex }


主题说明

  • 锚点使用 xtf-text 组件
  • 列表项使用 xtf-cell 组件
  • 底层使用 xtf-index-bar 组件

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

MIT Licensed