ts-container-alphabet-indexer.md 5.2 KB
Newer Older
Z
zengyawen 已提交
1
# AlphabetIndexer
Z
zengyawen 已提交
2

3
The **\<Indexer>** component can create a logically indexed array of items in a container for instant location.
Z
zengyawen 已提交
4

5 6 7
>  **NOTE**
>
>  This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9 10


## Child Components
Z
zengyawen 已提交
11

12
Not supported
Z
zengyawen 已提交
13 14


Z
zengyawen 已提交
15
## APIs
Z
zengyawen 已提交
16

E
ester.zhou 已提交
17
AlphabetIndexer(value: {arrayValue: Array&lt;string&gt;, selected: number})
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19
**Parameters**
Z
zengyawen 已提交
20

E
ester.zhou 已提交
21 22 23 24
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| arrayValue | Array&lt;string&gt; | Yes| Array of strings to be displayed in the alphabetic index bar.|
| selected   | number              | Yes   | Index of the initially selected item.    |
Z
zengyawen 已提交
25

Z
zengyawen 已提交
26
## Attributes
Z
zengyawen 已提交
27

E
ester.zhou 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.

| Name                 | Type    | Description                                                             |
| ----------------------- | --------------- | ----------------------------------------------------------- |
| color                   | [ResourceColor](ts-types.md#resourcecolor)       | Font color.                          |
| selectedColor           | [ResourceColor](ts-types.md#resourcecolor)     | Font color of the selected text.                          |
| popupColor              | [ResourceColor](ts-types.md#resourcecolor)        | Font color of the pop-up text.                        |
| selectedBackgroundColor | [ResourceColor](ts-types.md#resourcecolor)       | Background color of the selected item.                          |
| popupBackground         | [ResourceColor](ts-types.md#resourcecolor)        | Background color of the pop-up text.                           |
| usingPopup              | boolean                                  | Whether to use pop-up text.                        |
| selectedFont            | [Font](ts-types.md#font) | Font style of the selected text.                          |
| popupFont               | [Font](ts-types.md#font) | Font style of the pop-up text.                        |
| font                    | [Font](ts-types.md#font) | Default font style of the alphabetic index bar.                     |
| itemSize                | string \| number            | Size of an item in the alphabetic index bar. The item is a square, and the side length needs to be set.      |
| alignStyle              | IndexerAlign                             | Alignment style of the alphabetic index bar. Left alignment and right alignment are supported.<br>Default value: **IndexerAlign.Right**|
| selected | number | Index of the selected item.|
| popupPosition | [Position](ts-types.md#position8) | Position of the pop-up window relative to the center of the indexer bar's top border.|

## IndexerAlign enums
Z
zengyawen 已提交
47

E
ester.zhou 已提交
48 49 50 51
| Name| Description|
| -------- | -------- |
| Left | The pop-up window is displayed on the right of the alphabetic indexer bar.|
| Right | The pop-up window is displayed on the left of the alphabetic indexer bar.|
Z
zengyawen 已提交
52

Z
zengyawen 已提交
53
## Events
Z
zengyawen 已提交
54

E
ester.zhou 已提交
55 56
Only the following events are supported.

57
| Name| Description|
Z
zengyawen 已提交
58
| -------- | -------- |
E
ester.zhou 已提交
59 60 61 62
| onSelected(callback: (index: number) =&gt; void)<sup>(deprecated)</sup> | Invoked when an item in the alphabetic indexer bar is selected. The return value is the index of the selected item.                                |
| onSelect(callback: (index: number) =&gt; void)<sup>8+</sup> | Invoked when an item in the alphabetic indexer bar is selected. The return value is the index of the selected item.                                |
| onRequestPopupData(callback: (index: number) =&gt; Array&lt;string&gt;)<sup>8+</sup> | Invoked when a request for displaying content in the index prompt window is sent after an item in the alphabetic indexer bar is selected.<br>The return value is a string array corresponding to the indexes. The string array is displayed vertically in the pop-up window. It can display up to five strings at a time and allows scrolling.|
| onPopupSelect(callback: (index: number) =&gt; void)<sup>8+</sup> | Invoked when an item in the index pop-up window is selected.                           |
Z
zengyawen 已提交
63 64


Z
zengyawen 已提交
65
## Example
Z
zengyawen 已提交
66

67 68
```ts
// xxx.ets
Z
zengyawen 已提交
69 70
@Entry
@Component
Z
zengyawen 已提交
71
struct AlphabetIndexerSample {
Z
zengyawen 已提交
72 73 74
  private value: string[] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

  build() {
Z
zengyawen 已提交
75
    AlphabetIndexer({ arrayValue: this.value, selected: 0 })
Z
zengyawen 已提交
76 77 78 79 80 81 82 83 84
      .selectedColor(0xffffff) // Font color of the selected text
      .popupColor(0xFFFAF0) // Font color of the pop-up text
      .selectedBackgroundColor(0xCCCCCC) // Background color of the selected text
      .popupBackground(0xD2B48C) // Background color of the pop-up text
      .usingPopup(true) // Whether to use pop-up text
      .selectedFont({ size: 16, weight: FontWeight.Bolder }) // Font style of the selected text
      .popupFont({ size: 30, weight: FontWeight.Bolder }) // Font style of the pop-up text
      .itemSize(28) // Size of each item (square)
      .alignStyle(IndexerAlign.Left) // Left aligned
E
ester.zhou 已提交
85
      .onSelect((index: number) => {
Z
zengyawen 已提交
86 87 88 89 90 91 92
        console.info(this.value[index] + 'Selected') // Event indicating that an item is selected
      })
      .margin({ left: 50 })
  }
}
```

Z
zengyawen 已提交
93
![en-us_image_0000001212378392](figures/en-us_image_0000001212378392.gif)