Skip to content

IndexList 索引列表

IndexList 是一个索引列表组件,用于展示按首字母分组的数据,支持快速导航。

导入

js
import IndexList from '@/components/list/IndexList.vue';
import IndexBar from '@/components/nav/IndexBar.vue';

使用方法

基础用法

以下是一个基础的索引列表示例,支持按首字母分组和快速导航:

vue
<template>
  <IndexList 
    :data="data"
    :groupDataBy="(item) => item.charAt(0)"
    :sortGroup="arr => arr.sort()"
    :innerStyle="{
      height: '80vh',
    }"
    @itemClick="handleItemClick"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import IndexList from '@/components/list/IndexList.vue';
import Toast from '@/components/feedback/Toast.vue';

const toast = ref();
const data = [
  'Robert Davis',
  'Sandra Allen',
  'Charles Davis',
  'Michael Brown',
  'Joseph Miller',
  'Carol Allen',
  // ...更多数据
];

function handleItemClick(item) {
  toast.info('点击了:' + item);
}
</script>

自定义渲染

IndexList 支持自定义列表项和分组标题的渲染,需要注意小程序和非小程序环境的差异处理:

vue
<template>
  <IndexList 
    :data="data"
    :groupDataBy="(item) => item.charAt(0)"
    :sortGroup="arr => arr.sort()"
    :innerStyle="{
      height: '80vh',
    }"
  >
    <!-- #ifdef MP -->
    <template #list="{ visibleItems, className, itemStyle, onItemPress }">
      <view 
        v-for="(row, index) in visibleItems" 
        :key="row.key"
        :style="itemStyle"
        :class="className"
        @click="onItemPress(row.item, index)"
      > 
        <text :id="row.item.id" v-if="row.item.isHeader" style="height: 40rpx;background-color:#a71">
          Header {{ row.item.header }}
        </text>
        <view :id="row.item.id" v-else>
          <text :style="{ color: index % 2 === 0 ? 'red' : 'green' }">{{ row.item.data }}</text>
        </view>
      </view>
    </template>
    <!-- #endif -->

    <!-- #ifndef MP -->
    <template #header="{ header, id }">
      <text :id="id" style="height: 40rpx;background-color:#a71">
        Header {{ header }}
      </text>
    </template>
    <template #itemContent="{ item, index, id }">
      <view :id="id">
        <text>Index: {{index}}</text>
        <text :style="{ color: index % 2 === 0 ? 'red' : 'green' }">{{ item }}</text>
      </view>
    </template>
    <!-- #endif -->
  </IndexList>
</template>

单独使用 IndexBar

IndexBar 是 IndexList 的侧边滑动组件,也可以单独使用:

vue
<template>
  <view>
    <text :style="{ fontSize: '40rpx' }">当前选中索引 {{ activeIndex }}</text>
    <IndexBar
      v-model:activeIndex="activeIndex"
      :data="dataIndex"
    />
  </view>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import IndexBar from '@/components/nav/IndexBar.vue';

const activeIndex = ref(-1);
const dataIndex = [
  'A', 'B', 'C', 'D', 'E', 'F', 'G', 
  'H', 'I', 'J', 'K', 'L', 'M'
];
</script>

API 参考

IndexList

索引列表组件。

Props

名称说明类型必填默认值
data源数据数组T[][]
dataKey数据项的唯一键名,用于vue循环优化stringundefined
dataDisplayProp显示数据的属性名,为空则直接显示数据stringundefined
colorProp控制数据条目颜色的字段名称stringundefined
disabledProp控制是否禁用数据条目的字段名称stringundefined
itemHeight每个条目的高度(px)number40
headerHeight分组标题的高度(px)number30
groupDataBy数据分组的回调函数(item: T) => string-
sortGroup分组排序的回调函数(headers: string[]) => string[]undefined
mode列表选择模式:select(点击选择)、single-check(单选)、mulit-check(多选)'select' | 'single-check' | 'mulit-check''select'
checkProps选择框的自定义属性CheckBoxDefaultButtonProps{}
defaultSelect默认选中的条目any[][]
innerStyle内部容器的自定义样式ViewStyle{}
groupStyle分组标题的自定义样式ViewStyle{}
groupTextStyle分组标题文字的自定义样式TextStyle{}
itemStyle条目的自定义样式ViewStyle{}
textStyle条目文字的自定义样式TextStyle{}
checkedItemStyle选中的条目的自定义样式ViewStyle{}
checkedTextStyle选中的条目文字的自定义样式TextStyle{}
emptyText空数据时显示的文字string'暂无数据'

Slots

名称说明参数
list自定义列表渲染(主要用于小程序环境){ visibleItems, className, itemStyle, onItemPress }
header分组标题插槽(非小程序环境){ header, id }
item列表项插槽(非小程序环境){ item, index, id }
itemContent列表项内容插槽(非小程序环境){ item, index, id }
empty空数据时的插槽
inner内部插槽,用于放置额外内容-

Events

名称说明参数
itemClick点击列表项时触发item: T, index: number
selectedItemChanged选中项目改变时触发(仅在check模式下)selectedItems: T[]

主题变量

名称类型默认值说明
IndexedListBackgroundColorstringwhite索引列表背景色
IndexedListHeaderBackgroundColorstringbackground.page分组标题背景色
IndexedListHeaderPaddingVerticalnumber0分组标题垂直内边距
IndexedListHeaderPaddingHorizontalnumber30分组标题水平内边距
IndexedListHeaderFontSizenumber24分组标题字体大小
IndexedListHeaderColorstringtext.second分组标题文字颜色
IndexedListItemPaddingHorizontalnumber30列表项水平内边距
IndexedListItemFontSizenumber28列表项字体大小
IndexedListItemBorderTopWidthnumber0列表项顶部边框宽度
IndexedListItemBorderBottomWidthnumber2列表项底部边框宽度
IndexedListItemBorderColorstringborder.cell列表项边框颜色
IndexedListItemColorstringtext.content列表项文字颜色
IndexedListItemBackgroundColorstringwhite列表项背景色
IndexedListItemCheckedTextFontSizenumber28选中项文字字体大小
IndexedListItemCheckedTextFontWeightstringbold选中项文字字体粗细
IndexedListItemCheckedTextColorstringprimary选中项文字颜色