• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# <list> Development
2
3
4The **<list>** component provides a list container that presents a series of list items arranged in a column with the same width. Lists can be used for presenting the same type of data in a multiple and coherent row style. For details, see [list](../reference/arkui-js/js-components-container-list.md).
5
6
7## Creating a <list> Component
8
9Create a **<list>** component in the .hml file under **pages/index**.
10
11
12```
13<!-- index.hml -->
14<div class="container">
15 <list>
16   <list-item class="listItem"></list-item>
17   <list-item class="listItem"></list-item>
18   <list-item class="listItem"></list-item>
19   <list-item class="listItem"></list-item>
20 </list>
21</div>
22```
23
24```
25/* xxx.css */
26.container {
27  width:100%;
28  height:100%;
29  flex-direction: column;
30  align-items: center;
31  background-color: #F1F3F5;
32}
33.listItem{
34  height: 20%;
35  background-color:#d2e0e0;
36  margin-top: 20px;
37}
38```
39
40![en-us_image_0000001223287680](figures/en-us_image_0000001223287680.png)
41
42> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
43> - **&lt;list-item-group&gt;** is a child component of the **&lt;list&gt;** component and is used to group items in a list. It can have a **&lt;list-item&gt;** nested inside, but not **&lt;list&gt;**.
44>
45> - **&lt;list-item&gt;** is a child component of the **&lt;list&gt;** component and is used to display items in a list.
46
47
48## Adding a Scrollbar
49
50To display a scrollbar on the right side of the screen, set **scrollbar** to **on**. The side scrollbar can be used to scroll a long list or the screen up or down.
51
52
53```
54<!-- index.hml -->
55<div class="container">
56  <list class="listCss" scrollbar="on">
57    <list-item class="listItem"></list-item>
58    <list-item class="listItem"></list-item>
59    <list-item class="listItem"></list-item>
60    <list-item class="listItem"></list-item>
61    <list-item class="listItem"></list-item>
62    <list-item class="listItem"></list-item>
63 </list>
64</div>
65```
66
67```
68/* index.css */
69.container {
70  flex-direction: column;
71  background-color: #F1F3F5;
72}
73.listItem{
74  height: 20%;
75  background-color:#d2e0e0;
76  margin-top: 20px;
77}
78.listCss{
79  height: 100%;
80  scrollbar-color: #8e8b8b;
81  scrollbar-width: 50px;
82}
83```
84
85![en-us_image_0000001223287684](figures/en-us_image_0000001223287684.gif)
86
87
88## Adding a Side Index Bar
89
90Set a custom **indexer** component to add an index bar at the right boundary of a list. By default, an alphabetical indexer is used.
91
92
93```
94<!-- index.hml -->
95<div class="container">
96  <list class="listCss"  indexer="{{['#','1','2','3','4','5','6','7','8']}}" >
97    <list-item class="listItem"  section="#" ></list-item>
98  </list>
99</div>
100```
101
102
103```
104/* index.css */
105.container{
106  flex-direction: column;
107  background-color: #F1F3F5;
108 }
109.listCss{
110  height: 100%;
111  flex-direction: column;
112  columns: 1
113}
114```
115
116![en-us_image_0000001223127716](figures/en-us_image_0000001223127716.png)
117
118> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
119> - This **indexer** attribute is valid only when **flex-direction** is set to **column** and **columns** is set to **1**.
120>
121> - You must include **"\#"** when using a customized indexer.
122
123
124## Collapsing or Expanding a List
125
126To allow a **&lt;list&gt;** component to collapse and expand, add **groupcollapse** and **groupexpand** events.
127
128
129```
130<!-- index.hml -->
131<div class="doc-page">
132  <list style="width: 100%;" id="mylist">
133    <list-item-group for="listgroup in list" id="{{listgroup.value}}" ongroupcollapse="collapse" ongroupexpand="expand">
134      <list-item type="item" style="background-color:#FFF0F5;height:95px;">
135        <div class="item-group-child">
136          <text>One---{{listgroup.value}}</text>
137        </div>
138      </list-item>
139      <list-item type="item" style="background-color: #87CEFA;height:145px;" primary="true">
140        <div class="item-group-child">
141          <text>Primary---{{listgroup.value}}</text>
142        </div>
143      </list-item>
144    </list-item-group>
145  </list>
146</div>
147```
148
149
150```
151/* index.css */
152.doc-page {
153  flex-direction: column;
154  background-color: #F1F3F5;
155}
156list-item{
157margin-top:30px;
158}
159.top-list-item {
160  width:100%;
161  background-color:#D4F2E7;
162}
163.item-group-child {
164  justify-content: center;
165  align-items: center;
166  width:100%;
167}
168```
169
170
171```
172// xxx.js
173import prompt from '@system.prompt';
174export default {
175  data: {
176    direction: 'column',
177    list: []
178  },
179  onInit() {
180    this.list = []
181    this.listAdd = []
182    for (var i = 1; i <= 2; i++) {
183      var dataItem = {
184        value: 'GROUP' + i,
185      };
186        this.list.push(dataItem);
187    }
188  },
189  collapse(e) {
190    prompt.showToast({
191      message: 'Close ' + e.groupid
192    })
193  },
194  expand(e) {
195    prompt.showToast({
196    message: 'Open ' + e.groupid
197    })
198  }
199}
200```
201
202![en-us_image_0000001267887845](figures/en-us_image_0000001267887845.gif)
203
204> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
205>
206> The **groupcollapse** and **groupexpand** events can be used only by the **list-item-group** component.
207
208
209## Example Scenario
210
211Search for contacts by using an alphabetical indexer.
212
213
214
215```
216<!-- index.hml -->
217<div class="doc-page">
218  <text style="font-size: 35px; font-weight: 500; text-align: center; margin-top: 20px; margin-bottom: 20px;">
219      <span>Contacts</span>
220  </text>
221  <list class="list" indexer="true">
222    <list-item class="item" for="{{namelist}}" type="{{$item.section}}" section="{{$item.section}}">
223      <div class="container">
224        <div class="in-container">
225          <text class="name">{{$item.name}}</text>
226          <text class="number">18888888888</text>
227        </div>
228      </div>
229    </list-item>
230    <list-item type="end" class="item">
231      <div style="align-items:center;justify-content:center;width:750px;">
232        <text style="text-align: center;">Total: 10</text>
233      </div>
234    </list-item>
235  </list>
236</div>
237```
238
239
240
241```
242/* index.css */
243.doc-page {
244  width: 100%;
245  height: 100%;
246  flex-direction: column;
247  background-color: #F1F3F5;
248}
249.list {
250  width: 100%;
251  height: 90%;
252  flex-grow: 1;
253}
254.item {
255  height: 120px;
256  padding-left: 10%;
257  border-top: 1px solid #dcdcdc;
258}
259.name {
260  color: #000000;
261  font-size: 39px;
262}
263.number {
264  color: black;
265  font-size: 25px;
266}
267.container {
268  flex-direction: row;
269  align-items: center;
270}
271.in-container {
272  flex-direction: column;
273  justify-content: space-around;
274}
275```
276
277
278
279```
280// xxx.js
281export default {
282   data: {
283     namelist:[{
284       name: 'Zoey',
285       section:'Z'
286     },{
287       name: 'Quin',
288       section:'Q'
289     },{
290       name:'Sam',
291       section:'S'
292     },{
293       name:'Leo',
294       section:'L'
295     },{
296       name:'Zach',
297       section:'Z'
298     },{
299       name:'Wade',
300       section:'W'
301     },{
302       name:'Zoe',
303       section:'Z'
304     },{
305        name:'Warren',
306        section:'W'
307     },{
308        name:'Kyle',
309        section:'K'
310     },{
311       name:'Zaneta',
312       section:'Z'
313     }]
314   },
315   onInit() {
316   }
317 }
318```
319
320
321![en-us_image_0000001267767861](figures/en-us_image_0000001267767861.gif)