List Expansion
Using wx:for, an array can be expanded into a set of nodes. For example:
<view wx:for="{{ arr }}">
The {{ index }}th item in the array is {{ item }}
</view>
In the above example, if the arr array has three items, a total of 3 <view> elements will be generated.
If the structure is relatively complex, a <block> virtual block can be used. For example:
<block wx:for="{{ arr }}">
<view>The {{ index }}th item in the array is:</view>
<view>{{ item }}</view>
</block>
List Temporary Variables
Inside a list expanded by wx:for, two temporary variables, item and index, are automatically generated. Here, item represents the value of the current item in the list, and index represents the index position.
The names of these temporary variables can be customized using wx:for-item and wx:for-index. For example:
<view wx:for="{{ arr }}" wx:for-item="t" wx:for-index="i">
The {{ i }}th item in the array is {{ t }}
</view>
The key Field of a List
When list data is updated, the component framework attempts to compare the old and new list data to infer which nodes should be added, removed, or moved.
If insertions or deletions are required at the beginning or middle of the list, the framework's inference may not be accurate or efficient enough. In such cases, you can provide a key field to assist the inference.
For example, given the following data:
// page.js
Page({
data: {
students: [
{ id: 'A0001', name: 'Zhang San' },
{ id: 'A0002', name: 'Li Si' },
{ id: 'A0003', name: 'Wang Wu' },
]
}
})
If the id of each item in the list is unique and representative, it can be used as the key. In WXML:
<!-- page.wxml -->
<view wx:for="{{ students }}" wx:key="id">
Name: {{ item.name }}
</view>
Note:
wx:keycannot use data binding; you should directly specify a field name string;- If
itemitself is a string or a number (rather than an object), you can specifywx:key="*this"to indicate using the value ofitemitself as the key.
Using the key Field Appropriately
Using wx:key does not necessarily improve list rendering performance (it can sometimes even have a negative impact). Follow these guidelines for whether to use wx:key:
- If the list is fixed and does not change, do not use
wx:key; - If items are only added or removed at the end of the list, do not use
wx:key; - If you cannot find a representative key, do not use
wx:key.
When selecting a representative key, follow these rules:
- Key field values should be unique whenever possible. Duplicates will reduce the effectiveness of using the key for inference;
- Key field values are converted to strings and used as identifiers. Therefore, key field values should be strings or numbers. Otherwise, uniqueness will be lost.
Expanding Other Data Types with wx:for
In addition to arrays, objects, numbers, and strings can also be expanded using wx:for. However, this often leads to unexpected results and is therefore not recommended.
- For objects,
wx:forexpands them as key-value pairs; - For numbers,
wx:fortreats the number as the expansion count; - For strings,
wx:fordecomposes the string into individual characters for expansion.