IndexBar 索引栏
为分组列表提供触摸索引和吸顶标题,支持内部滚动或页面滚动。
引入
uni-app 配置 easycom 后使用 <hui-index-bar>。公开类型和函数从 @nscode/handy-uni 导入;普通 Vue 3 安装 @nscode/handy-uni/web 预览插件。
示例
示例可独立运行;交互结果直接显示在页面。
vue
<template>
<view>
<hui-index-bar
:groups="groups"
:height="280"
item-key="id"
@select="(value) => (lastEvent = value)"
@change="(value) => (lastEvent = value)"
>
<template #item="{ item }"><hui-cell :title="nameOf(item)" /></template> </hui-index-bar
><text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件 / No event')
const nameOf = (item: unknown) =>
item && typeof item === 'object' && 'name' in item ? String(item.name) : ''
const groups = [
{ index: 'A', items: [{ id: 1, name: '阿林 / A' }] },
{ index: 'B', items: [{ id: 2, name: '柏木 / B' }] },
{ index: 'C', items: [{ id: 3, name: '长河 / C' }] }
]
</script>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
groups | IndexBarGroup[] | [] | 分组数据,组索引唯一。 |
indexList | string[] | — | 右侧索引;不传时取各组的 index。索引里有、分组里没有的字母点了不跳 |
itemKey | string | ((item: unknown) => string | number) | — | 组内项的 key;不传用下标 |
scrollMode | IndexBarScrollMode | 'inner' | inner 使用内部滚动容器,page 使用页面滚动。 |
height | number | string | — | inner 时的高度;不传撑满父容器 |
scrollTop | number | — | page 时页面转发的滚动位置;Web 版不传时自己订阅 |
stickyOffsetTop | number | 0 | page 时锚点吸顶的上边距(自定义导航栏高度),px |
sticky | boolean | true | 当前分组标题吸顶 |
hint | boolean | true | 触摸索引时屏幕中间显示当前字母 |
vibrate | boolean | false | 触摸切换字母时轻震(platform.vibrateShort) |
根节点类名和样式使用 customClass、customStyle。尺寸数字按 px,时长除明确说明外按 ms。
Events
| 事件 | 参数 |
|---|---|
select | (index: string) |
change | (index: string) |
受控状态由父级回写更新事件后改变。多词事件使用短横线。禁用操作、取消及过期异步结果不提交成功变更。
Slots
| 插槽 | 作用域 |
|---|---|
anchor | { group, active } |
item | { item, group, index } |
empty | — |
方法
| 方法 | 签名 | 说明 |
|---|---|---|
scrollTo | (index: string): void | 滚动到指定分组 |
refresh | (): Promise<void> | 重新测量并更新状态 |
类型
IndexBarScrollMode、IndexBarGroup、IndexBarProps、IndexBarEmits、IndexBarExpose.
平台说明
inner 模式用 HuixScroll 并测量锚点,page 模式在 uni-app 需转发页面 scrollTop。stickyOffsetTop 仅页面模式抵消顶部固定区域。页面滚动、分组和尺寸变化后可调用 refresh。原生 scroll-view 的 sticky 和尺寸事件仍需设备复核。
设备冒烟清单.