# Loop Directives
# List Rendering
To implement list rendering, we use the for directive. The for directive is used to loop through and output an array of data.
TIP
The for directive renders a list based on the source data array. The supported syntax is as follows (where {{}} can be omitted):
for="{{list}}"listis the source data array, and the default array element name is$item;for="{{value in list}}"valueis a custom array element name, and the default array element index name is$idx;for="{{(index, value) in list}}"indexis a custom array element index name, andvalueis a custom array element name.
TIP
The tid attribute of the for directive is used to specify the unique ID of array elements. If not specified, the array index ($idx) is used as the unique ID by default. The purpose of the tid attribute is to enable element node reuse and optimize the redraw efficiency of the for loop.
Example:
<template>
<div class="page">
<!-- Method 1: Default $item represents the array element, $idx represents the array index -->
<div class="row" for="{{list}}" tid="uniqueId">
<text>{{$idx}}.{{$item.name}}</text>
</div>
<!-- Method 2: Custom element variable name -->
<div class="row" for="value in list" tid="uniqueId">
<text>{{$idx}}.{{value.name}}</text>
</div>
<!-- Method 3: Custom element and index variable names -->
<div class="row" for="(personIndex, personItem) in list" tid="uniqueId">
<text>{{personIndex}}.{{personItem.name}}</text>
</div>
</div>
</template>
<style>
.page {
flex-direction: column;
}
.row {
width: 85%;
margin-top: 10px;
margin-bottom: 10px;
}
</style>
<script>
export default {
private: {
list: [
{ name: 'aa', uniqueId: 1 },
{ name: 'bb', uniqueId: 2 },
{ name: 'cc', uniqueId: 3 }
]
},
onInit() {
console.info('Directive for')
}
}
</script>
In the example code, the structure of div.row will be generated multiple times based on the definition of the list data in the script when rendering the page.
tid="uniqueId" refers to a property name of the array element, which does not necessarily have to be called uniqueId. It is similar to React's key={item.uniqueId} or Vue's :key="item.uniqueId", used to optimize rendering speed.
When data is modified, DOM elements whose data has not changed will not be re-rendered; only DOM elements whose data has changed will be re-rendered. Therefore, we must ensure that the uniqueId property value is unique for each array element.
Note
The
fordirective can only loop through arrays, not objects.The behavior of the
fordirective inside ablocktag is different from its behavior in other tags. Theblocktag does not insert additional DOM nodes during rendering but instead loops through its internal DOM structure. Other tags will loop and generate themselves.When the
fordirective and theifdirective coexist on the same tag, theifdirective takes precedence over thefordirective. To help newcomers who haven't read the documentation quickly get started with the project, it is not recommended to have these two directives coexist on the same tag.When custom variables represent the array index and array element of the
fordirective, the variable names cannot start with$or_.When using the
tid attribute, note the following:- The data property specified by the
tid attributemust exist; otherwise, it may cause runtime exceptions. - The data property specified by the
tid attributemust be unique; otherwise, it may cause performance issues. - The
tid attributecurrently does not support expressions.
- The data property specified by the