Skip to content

清單渲染

wx:for

在組件上使用wx:for控制内容綁定一個數組,即可使用數組中各項的數據重複渲染該組件。

默認數組的當前項的下標變量名默認為index,數組當前項的變量名默認為item。

js
<view wx:for="{{array}}">
  {{index}}: {{item.message}}
</view>
js
Page({
  data: {
    array: [{
      message: 'foo',
    }, {
      message: 'bar'
    }]
  }
})

使用wx:for-item可以指定數組當前元素的變量名,

使用wx:for-index可以指定數組當前下標的變量名:

js
<view wx:for="{{array}}" wx:for-index="idx" wx:for-item="itemName">
  {{idx}}: {{itemName.message}}
</view>

wx:for也可以嵌套,下邊是一個九九乘法表。

js
<view wx:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" wx:for-item="i">
  <view wx:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" wx:for-item="j">
    <view wx:if="{{i <= j}}">
      {{i}} * {{j}} = {{i * j}}
    </view>
  </view>
</view>

block wx:for

類似block wx: if, 也可以將wx:for用在<block/>標籤上,以渲染一個包含多節點的結構塊。 例如:

js
<block wx:for="{{[1, 2, 3]}}">
  <view> {{index}}: </view>
  <view> {{item}} </view>
</block>

wx:key

如果清單中項目的位置會動態改變或者有新的項目添加到清單中,並且希望清單中的項目保持自己的特徵和狀態(如<input/>中的輸入內容,<switch/>的選中狀態),需要使用wx:key來指定清單中項目的唯一的識別字。

wx:key的值以兩種形式提供:

  • 字串,代表在for迴圈的array中item的某個property,該property的值需要是清單中唯一的字串或數位,且不能動態改變。

  • 保留關鍵字*this代表在for迴圈中的item本身,這種表示需要item本身是一個唯一的字串或者數位。

當數據改變觸發渲染層重新渲染的時候,會校正帶有key的組件,框架會確保他們被重新排序,而不是重新創建,以確保使組件保持自身的狀態,並且提高清單渲染時的效率,如下代碼所示:

如不提供wx: key, 會報一個warning,如果明確知道該清單是靜態,或者不必關注其順序,可以選擇忽略。

示例代碼:

js
<switch wx:for="{{objectArray}}" wx:key="unique" style="display: block;"> {{item.id}} </switch>
<button bindtap="switch"> Switch </button>
<button bindtap="addToFront"> Add to the front </button>

<switch wx:for="{{numberArray}}" wx:key="*this" style="display: block;"> {{item}} </switch>
<button bindtap="addNumberToFront"> Add to the front </button>
js
Page({
  data: {
    objectArray: [
      {id: 5, unique: 'unique_5'},
      {id: 4, unique: 'unique_4'},
      {id: 3, unique: 'unique_3'},
      {id: 2, unique: 'unique_2'},
      {id: 1, unique: 'unique_1'},
      {id: 0, unique: 'unique_0'},
    ],
    numberArray: [1, 2, 3, 4]
  },
  switch: function(e) {
    const length = this.data.objectArray.length
    for (let i = 0; i < length; ++i) {
      const x = Math.floor(Math.random() * length)
      const y = Math.floor(Math.random() * length)
      const temp = this.data.objectArray[x]
      this.data.objectArray[x] = this.data.objectArray[y]
      this.data.objectArray[y] = temp
    }
    this.setData({
      objectArray: this.data.objectArray
    })
  },
  addToFront: function(e) {
    const length = this.data.objectArray.length
    this.data.objectArray = [{id: length, unique: 'unique_' + length}].concat(this.data.objectArray)
    this.setData({
      objectArray: this.data.objectArray
    })
  },
  addNumberToFront: function(e){
    this.data.numberArray = [ this.data.numberArray.length + 1 ].concat(this.data.numberArray)
    this.setData({
      numberArray: this.data.numberArray
    })
  }
})

注意事項

當wx:for的值為字串時,會將字串解析成字串數組。

js
<view wx:for="array">
  {{item}}
</view>

等同於

js
<view wx:for="{{['a','r','r','a','y']}}">
  {{item}}
</view>

TIP

花括弧和引號之間如果有空格,將最終被解析成為字串。

js
<view wx:for="{{[1,2,3]}} ">
  {{item}}
</view>

等同於

js
<view wx:for="{{[1,2,3] + ' '}}" >
  {{item}}
</view>