小程序中的遍歷循環類似於angularJS的遍歷。
二級數組遍歷有一個坑。二級遍歷wx:for循環的時候,需要注意。(代碼如下)
JS代碼:
data: { groups: [ [ { title: '狼圖騰', cover: '../../img/mineBG.png' }, { title: '狼圖騰', cover: '../../img/mineBG.png' }, ], [ { title: '狼圖騰', cover: '../../img/mineBG.png' }, ], [ { title: '狼圖騰', cover: '../../img/mineBG.png' }, ] ], },
遍歷出不同的數組,然后渲染不同組的cell
<!--一共三組--> <view class="group" wx:for="{{groups}}" wx:for-index="groupindex"> <!--組頭--> <view class="group-header"> <view class="group-header-left">{{}}</view> <view class="group-header-right">{{}}</view> </view>
MARK:
二級循環的時候,wx:for="item",這種寫法是錯誤的。
<!--cell--> <view class="group-cell" wx:for="{{groups[groupindex]}}" wx:for-item="cell" wx:for-index="cellindex"> <!--<image class='group-cell-image' src="{{item.cover}}"></image>--> <image class='group-cell-image' src="../../img/mineBG.png"></image> <view class='group-cell-title'>title{{cell.title}}</view> </view> <!--footer--> <view class="group-footer">{{group.footer}}</view> </view>