|
作者:handsomeBoys,來(lái)自原文地址
一:多層嵌套循環(huán),二級(jí)數(shù)組遍歷
小程序中的遍歷循環(huán)類(lèi)似于angularJS的遍歷。
二級(jí)數(shù)組遍歷有一個(gè)坑。二級(jí)遍歷wx:for循環(huán)的時(shí)候,需要注意。(代碼如下)
JS代碼:
-
data: {
-
groups: [
-
[
-
{
-
title: '狼圖騰',
-
cover: '../../img/mineBG.png'
-
},
-
{
-
title: '狼圖騰',
-
cover: '../../img/mineBG.png'
-
},
-
],
-
[
-
{
-
title: '狼圖騰',
-
cover: '../../img/mineBG.png'
-
},
-
-
],
-
[
-
{
-
title: '狼圖騰',
-
cover: '../../img/mineBG.png'
-
},
-
-
]
-
],
-
},
遍歷出不同的數(shù)組,然后渲染不同組的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:二級(jí)循環(huán)的時(shí)候,wx:for="item",這種寫(xiě)法是錯(cuò)誤的。
-
<!--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>
二:設(shè)置data里面的數(shù)據(jù)
關(guān)于設(shè)置 data里面的數(shù)據(jù)
wxml:
-
<view>{{userName}}</view>
-
data: {
-
-
userName:'張三',
-
-
}
有兩種方法:
方法一: 直接使用點(diǎn)關(guān)系符號(hào)賦值,類(lèi)似于普通賦值,但是這樣的話,外面使用綁定的數(shù)據(jù),不會(huì)實(shí)現(xiàn)臟檢查,wxml綁定的數(shù)據(jù)不回及時(shí)更新。
例子:
-
this.data.userName = '李四';
方法二: 使用系統(tǒng)提供的setData()方法,這樣的 話,系統(tǒng)會(huì)執(zhí)行類(lèi)似于angularJS框架的臟檢查,wxml綁定的數(shù)據(jù)會(huì)馬上刷新,實(shí)現(xiàn)數(shù)據(jù)的綁定。
例子:
-
this.setData({
-
-
userName = '李四';
-
-
})
|