IndexBar
Adds touch indexes and sticky headings to grouped lists, using inner or page scrolling.
Import
Use <hui-index-bar> after configuring uni-app easycom. Import public types and functions from @nscode/handy-uni. Ordinary Vue applications install the @nscode/handy-uni/web preview plugin.
Examples
Each example is standalone. Controls show the current value or event result on the page.
<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
| Prop | Type | Default | Details |
|---|---|---|---|
groups | IndexBarGroup[] | [] | Grouped data; each group index must be unique. |
indexList | string[] | — | Explicit sidebar indexes; otherwise derived from groups. |
itemKey | string | ((item: unknown) => string | number) | — | Stable object field or key getter; falls back to id, then index. |
scrollMode | IndexBarScrollMode | 'inner' | Inner scroll container or page scrolling. |
height | number | string | — | Height; numbers use px and strings preserve their unit. |
scrollTop | number | — | Page scroll position forwarded by the application. |
stickyOffsetTop | number | 0 | Page heading offset in px. |
sticky | boolean | true | Enables sticky group headings. |
hint | boolean | true | Displays the selected-index hint. |
vibrate | boolean | false | Requests brief haptic feedback. |
Root classes and styles use customClass and customStyle. Numbers representing dimensions use px; durations use ms unless explicitly documented.
Events
| Event | Arguments |
|---|---|
select | (index: string) |
change | (index: string) |
Controlled state only changes when the parent accepts its update event. Events with multiple words use kebab-case. Disabled or stale asynchronous actions do not emit a successful state change.
Slots
| Slot | Scope |
|---|---|
anchor | { group, active } |
item | { item, group, index } |
empty | — |
Methods
| Method | Signature | Description |
|---|---|---|
scrollTo | (index: string): void | Scroll to a group. |
refresh | (): Promise<void> | Measure again and update state. |
Types
IndexBarScrollMode、IndexBarGroup、IndexBarProps、IndexBarEmits、IndexBarExpose.
Platform notes
Inner mode uses HuixScroll and measured anchors. uni-app page mode requires forwarded scrollTop. stickyOffsetTop offsets fixed page headers. Call refresh after page or content geometry changes. Native scroll-view sticky behavior and dimension events require device checks.
Device smoke checklist.