【wx:for】小程序列表渲染的使用说明
wx:for 控制属性绑定一个数组,即可使用数组中各项的数据重复渲染该组件。
默认数组的当前项的下标变量名默认为 index,数组当前项的变量名默认为 item,即: {{index}} 、 {{item}}
. 标签原型(以标签block为例):
<block wx:for="{{array}}" wx:key="[String | *this]" wx:for-index="[String]" wx:for-item="[String]">
</block>
wx:for 【必须 Array】数组
wx:for-index 【可选 String】用新的变量名代替{{index}},特别在多个wx:for套嵌时使用,用于区分不同的{{index}}
wx:for-item 【可选 String】用新的变量名代替{{item}},特别在多个wx:for套嵌时使用,用于区分不同的{{item}}
wx:key 【可选 String | *this】动态添加列表、编辑列表时,希望列表项目保持自身特征及状态时需要此属性。
wx:for 示例:
1 <view wx:for="{{array}}"> 2 {{index}}: {{item.message}} 3 </view>
1 Page({ 2 data: { 3 array: [{ 4 message: 'foo', 5 }, { 6 message: 'bar' 7 }] 8 } 9 })
标签中的{{index}} 表示数组的下标值,index从0起始。
标签中{{item.message}}表示数组项目下message键值.
结果示例:
0:foo
1:bar
wx:for-index 与 wx:for-item 使用说明:
以下是九九乘法表代码:
1 <view wx:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" wx:for-item="i"> 2 <view wx:for="{{[1, 2, 3, 4, 5, 6, 7, 8, 9]}}" wx:for-item="j"> 3 <view wx:if="{{i <= j}}"> 4 {{i}} * {{j}} = {{i * j}} 5 </view> 6 </view> 7 </view>
在上例中,将最外层的循环的{{item}}重定义为 i ,将内层的{{item}}重定义为j,这样解决了名称相重问题。
wx:key的使用说明(以下引用官网内容,后面加自身的解释):
如果列表中项目的位置会动态改变或者有新的项目添加到列表中,并且希望列表中的项目保持自己的特征和状态(如 <input/> 中的输入内容,<switch/> 的选中状态),需要使用 wx:key 来指定列表中项目的唯一的标识符。
wx:key 的值以两种形式提供
1. 字符串,代表在 for 循环的 array 中 item 的某个 property,该 property 的值需要是列表中唯一的字符串或数字,且不能动态改变。
2. 保留关键字 *this 代表在 for 循环中的 item 本身,这种表示需要 item 本身是一个唯一的字符串或者数字,如:
当数据改变触发渲染层重新渲染的时候,会校正带有 key 的组件,框架会确保他们被重新排序,而不是重新创建,以确保使组件保持自身的状态,并且提高列表渲染时的效率。
1 <switch wx:for="{{objectArray}}" wx:key="unique" style="display: block;"> {{item.id}} </switch> 2 <button bindtap="switch"> Switch </button> 3 <button bindtap="addToFront"> Add to the front </button> 4 5 <switch wx:for="{{numberArray}}" wx:key="*this" style="display: block;"> {{item}} </switch> 6 <button bindtap="addNumberToFront"> Add to the front </button>
1 Page({ 2 data: { 3 objectArray: [ 4 {id: 5, unique: 'unique_5'}, 5 {id: 4, unique: 'unique_4'}, 6 {id: 3, unique: 'unique_3'}, 7 {id: 2, unique: 'unique_2'}, 8 {id: 1, unique: 'unique_1'}, 9 {id: 0, unique: 'unique_0'}, 10 ], 11 numberArray: [1, 2, 3, 4] 12 }, 13 switch: function(e) { 14 const length = this.data.objectArray.length 15 for (let i = 0; i < length; ++i) { 16 const x = Math.floor(Math.random() * length) 17 const y = Math.floor(Math.random() * length) 18 const temp = this.data.objectArray[x] 19 this.data.objectArray[x] = this.data.objectArray[y] 20 this.data.objectArray[y] = temp 21 } 22 this.setData({ 23 objectArray: this.data.objectArray 24 }) 25 }, 26 addToFront: function(e) { 27 const length = this.data.objectArray.length 28 this.data.objectArray = [{id: length, unique: 'unique_' + length}].concat(this.data.objectArray) 29 this.setData({ 30 objectArray: this.data.objectArray 31 }) 32 }, 33 addNumberToFront: function(e){ 34 this.data.numberArray = [ this.data.numberArray.length + 1 ].concat(this.data.numberArray) 35 this.setData({ 36 numberArray: this.data.numberArray 37 }) 38 } 39 })
代码是最好的解释方式,以上程序运行时,你可以选中其中的一项switch,使其为开状态,然后点击“Switch”或“Add to the front“按钮时,已选中的switch依然保持选中状态。如果代码中没加 wx:key 时,选中的状态将不保留。