ui-js-components-list.md 6.7 KB
Newer Older
G
ge-yafang 已提交
1
# <list> Development
Z
zengyawen 已提交
2 3


G
ge-yafang 已提交
4 5 6 7 8 9
The <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).


## Creating a <list> Component

Create a <list> component in the .hml file under pages/index.
Z
zengyawen 已提交
10 11 12 13 14


```
<!-- index.hml -->
<div class="container"> 
G
ge-yafang 已提交
15
 <list>    <list-item class="listItem"></list-item>
Z
zengyawen 已提交
16 17 18 19 20 21 22
   <list-item class="listItem"></list-item>
   <list-item class="listItem"></list-item>
   <list-item class="listItem"></list-item>
 </list>
</div>
```

G
ge-yafang 已提交
23

Z
zengyawen 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37
```
/* xxx.css */
.container {
  flex-direction: column;
  align-items: center;
  background-color: #F1F3F5;
}
.listItem{
  height: 20%;
  background-color:#d2e0e0;
  margin-top: 20px;
}
```

G
ge-yafang 已提交
38
![en-us_image_0000001223287680](figures/en-us_image_0000001223287680.png)
Z
zengyawen 已提交
39

G
ge-yafang 已提交
40 41 42 43
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**:
> - &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;.
> 
> - &lt;list-item&gt; is a child component of the &lt;list&gt; component and is used to display items in a list.
Z
zengyawen 已提交
44 45


G
ge-yafang 已提交
46 47 48 49
## Adding a Scrollbar

To 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.

Z
zengyawen 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

```
<!-- index.hml -->
<div class="container">
  <list class="listCss" scrollbar="on" >
    <list-item class="listItem"></list-item>
    <list-item class="listItem"></list-item>
    <list-item class="listItem"></list-item>
    <list-item class="listItem"></list-item>
    <list-item class="listItem"></list-item>
    <list-item class="listItem"></list-item>
 </list>
</div> 
```

G
ge-yafang 已提交
65

Z
zengyawen 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
```
/* index.css */
.container {
  flex-direction: column;
  background-color: #F1F3F5;
}
.listItem{
  height: 20%;
  background-color:#d2e0e0;
  margin-top: 20px;
}
.listCss{
  height: 100%;
  scrollbar-color: #8e8b8b;
  scrollbar-width: 50px;
}
```

G
ge-yafang 已提交
84 85 86 87
![en-us_image_0000001223287684](figures/en-us_image_0000001223287684.gif)


## Adding a Side Index Bar
Z
zengyawen 已提交
88

G
ge-yafang 已提交
89
Set a custom indexer component to add an index bar at the right boundary of a list. By default, an alphabetical indexer is used.
Z
zengyawen 已提交
90 91 92 93 94 95 96 97 98 99 100


```
<!-- index.hml -->
<div class="container">   
  <list class="listCss"  indexer="{{['#','1','2','3','4','5','6','7','8']}}" >  
    <list-item class="listItem"  section="#" ></list-item>   
  </list>
</div>
```

G
ge-yafang 已提交
101

Z
zengyawen 已提交
102 103
```
/* index.css */
G
ge-yafang 已提交
104
.container{
Z
zengyawen 已提交
105 106 107 108 109 110 111 112 113 114
  flex-direction: column;
  background-color: #F1F3F5;
 } 
.listCss{
  height: 100%;    
  flex-direction: column;
  columns: 1
}
```

G
ge-yafang 已提交
115 116 117 118 119 120
![en-us_image_0000001223127716](figures/en-us_image_0000001223127716.png)

> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**:
> - This indexer attribute is valid only when flex-direction is set to column and columns is set to 1.
> 
> - You must include "\#" when using a customized indexer.
Z
zengyawen 已提交
121 122


G
ge-yafang 已提交
123 124 125
## Collapsing or Expanding a List

To allow a &lt;list&gt; component to collapse and expand, add groupcollapse and groupexpand events.
Z
zengyawen 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147


```
<!-- index.hml -->
<div class="doc-page">
  <list style="width: 100%;" id="mylist">
    <list-item-group for="listgroup in list" id="{{listgroup.value}}" ongroupcollapse="collapse" ongroupexpand="expand">
      <list-item type="item" style="background-color:#FFF0F5;height:95px;">
        <div class="item-group-child">
          <text>One---{{listgroup.value}}</text>
        </div>
      </list-item>
      <list-item type="item" style="background-color: #87CEFA;height:145px;" primary="true">
        <div class="item-group-child">
          <text>Primary---{{listgroup.value}}</text>
        </div>
      </list-item>
    </list-item-group>
  </list>
</div>
```

G
ge-yafang 已提交
148

Z
zengyawen 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
```
/* index.css */
.doc-page {
  flex-direction: column;
  background-color: #F1F3F5;
}
list-item{
margin-top:30px;
}
.top-list-item {
  width:100%;
  background-color:#D4F2E7;
}
.item-group-child {
  justify-content: center;
  align-items: center;
  width:100%;
}
```

G
ge-yafang 已提交
169

Z
zengyawen 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
```
// xxx.js
import prompt from '@system.prompt';
export default {
  data: {
    direction: 'column',
    list: []
  },
  onInit() {
    this.list = []
    this.listAdd = []
    for (var i = 1; i <= 2; i++) {
      var dataItem = {
        value: 'GROUP' + i,
      };
        this.list.push(dataItem);
    }
  },
  collapse(e) {
    prompt.showToast({
      message: 'Close ' + e.groupid
    })
  },
  expand(e) {
    prompt.showToast({
    message: 'Open ' + e.groupid
    })
  }
}
```

G
ge-yafang 已提交
201 202 203 204
![en-us_image_0000001267887845](figures/en-us_image_0000001267887845.gif)

> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**:
> - The groupcollapse and groupexpand events can be used only by the list-item-group component.
Z
zengyawen 已提交
205 206


G
ge-yafang 已提交
207
## Example Scenario
Z
zengyawen 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

Search for contacts by using an alphabetical indexer.

```
<!-- index.hml -->
<div class="doc-page"> 
  <text style="font-size: 35px; font-weight: 500; text-align: center; margin-top: 20px; margin-bottom: 20px;"> 
      <span>Contacts</span> 
  </text> 
  <list class="list" indexer="true"> 
    <list-item class="item" for="{{namelist}}" type="{{$item.section}}" section="{{$item.section}}"> 
      <div class="container"> 
        <div class="in-container"> 
          <text class="name">{{$item.name}}</text> 
          <text class="phone">18888888888</text> 
        </div> 
      </div> 
    </list-item> 
    <list-item type="end" class="item"> 
      <div style="align-items:center;justify-content:center;width:750px;"> 
        <text style="text-align: center;">Total: 10</text> 
      </div> 
    </list-item> 
  </list> 
</div>
```

```
/* index.css */
.doc-page {
  flex-direction: column;
  background-color: #F1F3F5;
}
.list {
  width: 100%;
  height: 100%;
}
.item {
  height: 120px;
  padding-left: 10%;
  border-top: 1px solid #dcdcdc;
}
.name {
  color: #000000;
  font-size: 39px;
}
.phone {
  color: black;
  font-size: 25px;
}
.container {
  flex-direction: row;
  align-items: center;
}
.in-container {
  flex-direction: column;
  justify-content: space-around;
}
```

```
// xxx.js
export default { 
   data: { 
     namelist:[{ 
       name: 'Zoey', 
       section:'Z' 
     },{ 
       name: 'Quin', 
       section:'Q' 
     },{ 
       name:'Sam', 
       section:'S' 
     },{ 
       name:'Leo', 
       section:'L' 
     },{ 
       name:'Zach', 
       section:'Z' 
     },{ 
       name:'Wade', 
       section:'W' 
     },{ 
       name:'Zoe', 
       section:'Z' 
     },{ 
        name:'Warren', 
        section:'W' 
     },{ 
        name:'Kyle', 
        section:'K' 
     },{ 
       name:'Zaneta', 
       section:'Z' 
     }] 
   }, 
   onInit() { 
   } 
 }
```


G
ge-yafang 已提交
310
![en-us_image_0000001267767861](figures/en-us_image_0000001267767861.gif)