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