课程表

微信小程序框架

微信小程序组件

微信小程序API

微信小程序开放接口

微信小程序工具

微信小程序设计规范

工具箱
速查手册

小程序 列表渲染 wx:for

当前位置:免费教程 » 移动开发 » 微信小程序

在组件上使用wx:for控制属性绑定一个数组,即可使用数组中各项的数据重复渲染该组件。

默认数组的当前项的下标变量名默认为index,数组当前项的变量名默认为item

  1. <view wx:for="{{items}}">
  2. {{index}}: {{item.message}}
  3. </view>
  1. Page({    data:{
  2.       items: [{
  3.         message: 'foo',
  4.       },{
  5.         message: 'bar'
  6.       }]
  7.     }
  8. })

使用wx:for-item可以指定数组当前元素的变量名

使用wx:for-index可以指定数组当前下标的变量名:

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

wx:for也可以嵌套,下边是一个九九乘法表

  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>

block wx:for


类似block wx:if,也可以将wx:for用在<block/>标签上,以渲染一个包含多节点的结构块。例如:

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

wx:key


如果列表中项目的位置会动态改变或者有新的项目添加到列表中,并且希望列表中的项目保持自己的特征和状态(如 <input/> 中的输入内容,<switch/> 的选中状态),需要使用 wx:key 来指定列表中项目的唯一的标识符。

wx:key 的值以两种形式提供

  1. 字符串,代表在 for 循环的 array 中 item 的某个 property,该 property 的值需要是列表中唯一的字符串或数字,且不能动态改变。
  2. 保留关键字 *this 代表在 for 循环中的 item 本身,这种表示需要 item 本身是一个唯一的字符串或者数字,如:

当数据改变触发渲染层重新渲染的时候,会校正带有 key 的组件,框架会确保他们被重新排序,而不是重新创建,以确保使组件保持自身的状态,并且提高列表渲染时的效率。

如不提供 wx:key,会报一个 warning, 如果明确知道该列表是静态,或者不必关注其顺序,可以选择忽略。

示例代码:

  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. <switch wx:for="{{numberArray}}" wx:key="*this" style="display: block;"> {{item}} </switch>
  5. <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. })
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号