微信小程序 wx:for遍歷循環(huán)使用實(shí)例解析
這篇文章主要介紹了微信小程序 wx:for遍歷循環(huán)使用實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
效果圖如下:

實(shí)現(xiàn)代碼如下:
type.js:
// pages/type/type.js
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
types: ""
},
editType: function (e) {
var typeId = e.currentTarget.dataset['id'];
console.log("edit:"+typeId);
wx.navigateTo({
url: '../type_edit/type_edit?typeId=' + typeId
})
},
delType:function(e){
var typeId = e.currentTarget.dataset['id'];
console.log("delete:"+typeId)
wx.showModal({
title: '提示',
content: '確認(rèn)要?jiǎng)h除該支出類型?',
success: function (res) {
if (res.confirm) {
console.log('用戶點(diǎn)擊確定')
wx.request({
url: getApp().globalData.urlPath + "spendingType/delete",
method: "POST",
data: {
typeId: typeId
},
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
console.log(res.data.code);
if (res.statusCode == 200) {
//訪問正常
if (res.data.code == "000000") {
wx.showToast({
title: "刪除成功,返回支出類型列表",
icon: 'success',
duration: 3000,
success: function () {
wx.navigateTo({
url: '../type/type'
})
}
})
}
} else {
wx.showLoading({
title: '系統(tǒng)異常',
fail
})
setTimeout(function () {
wx.hideLoading()
}, 2000)
}
}
})
} else if (res.cancel) {
console.log('用戶點(diǎn)擊取消')
}
}
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁(yè)面加載
*/
onLoad: function (options) {
wx.setNavigationBarTitle({
title: "支出類型列表"
})
var userCode = wx.getStorageSync('userId');
var self = this
wx.request({
url: getApp().globalData.urlPath + "spendingType/types",//json數(shù)據(jù)地址
data: {
userCode: userCode
},
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
success: function (res) {
self.setData({
types: res.data.data
});//等同于
}
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁(yè)面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁(yè)面顯示
*/
onShow: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁(yè)面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁(yè)面卸載
*/
onUnload: function () {
},
/**
* 頁(yè)面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動(dòng)作
*/
onPullDownRefresh: function () {
},
/**
* 頁(yè)面上拉觸底事件的處理函數(shù)
*/
onReachBottom: function () {
},
/**
* 用戶點(diǎn)擊右上角分享
*/
onShareAppMessage: function () {
}
})
type.js沒什么好說的,如果要說,只能說這個(gè)onLoad這里的onLoad就相當(dāng)于js中的onload方法,當(dāng)進(jìn)入該視圖時(shí),默認(rèn)全局加載一次。
type.wxml:
<view>
<view>
<navigator url="/pages/type_add/type_add" hover-class="navigator-hover">添加支出類型信息</navigator>
</view>
<view>
<text>\n</text>
</view>
<view>
<view>
<text>列表數(shù)據(jù)</text>
<text>\n</text>
</view>
<view class="table">
<view class="tr thead">
<view class="td">類型名</view>
<view class="td">創(chuàng)建時(shí)間</view>
<view class="td ">修改時(shí)間</view>
<view class="td">備注</view>
<view class="td ">操作</view>
</view>
<block>
<view class="tr" wx:for="{{types}}" wx:for-item="item">
<view class="td">{{item.typeName}}</view>
<view class="td">{{item.createDate}}</view>
<view class="td">{{item.modifyDate}}</view>
<view class="td">{{item.remark}}</view>
<view class="td">
<text bindtap='editType' data-id="{{item.typeId}}">編輯</text>
<text>\n</text>
<text>\n</text>
<text bindtap='delType' data-id="{{item.typeId}}">刪除</text>
</view>
</view>
</block>
</view>
</view>
</view>
遍歷循環(huán)主要使用的是wx:for。如果要類比的話,我覺得jstl跟這個(gè)神似。先來看看jstl,代碼如下:
<c:forEach var="u" items="${user}">
<tr>
<td>${u.cid}</td>
<td>${u.cname}</td>
<td>${u.age }</td>
</tr>
</c:forEach>
var相當(dāng)于我可以任意定義一個(gè)簡(jiǎn)要字母來調(diào)用item(item相當(dāng)于type.js中的data或self.setData存儲(chǔ)的數(shù)據(jù))。
如果你還不明白的話,可以聯(lián)系到$.each,代碼如下:
$.each(classroom_list, function(i, c) {
rows = rows + "<tr>";
rows = rows + "<td>" + c.id + "</td>";
rows = rows + "<td>" + c.nickname + "</td>";
rows = rows + "<td><a href='student_submit_info.html?userId="+c.id+"'>查看詳情</a></td>";
rows = rows + "</tr>"
});
type.wxss:
.table {
border: 1px solid #ccc;
font-size: 28rpx;
background: #fff;
border-right: none;
}
.tr{
display: flex;
justify-content: space-between;
}
.td {
text-align: center;
border: 1px solid #ccc;
display: inline-block;
border-left: none;
border-bottom: none;
padding: 10rpx 1%;
width: 12%;
}
.thead .td{
border-top: none;
height: 140rpx;
line-height: 50rpx;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript獲取一個(gè)范圍內(nèi)日期的方法
這篇文章主要介紹了JavaScript獲取一個(gè)范圍內(nèi)日期的方法,涉及javascript操作日期的相關(guān)技巧,需要的朋友可以參考下2015-04-04
JavaScript實(shí)現(xiàn)數(shù)組對(duì)象轉(zhuǎn)換為鍵值對(duì)的四種方式
本文探討了將包含 {icon: "abc", url: "123"} 形式對(duì)象的數(shù)組轉(zhuǎn)換為鍵值對(duì)形式的四種方法,并從實(shí)現(xiàn)方式的簡(jiǎn)潔性、可讀性和性能角度進(jìn)行了分析比較,感興趣的朋友可以參考下2024-02-02
JavaScript基于遍歷操作實(shí)現(xiàn)對(duì)象深拷貝功能示例
這篇文章主要介紹了JavaScript基于遍歷操作實(shí)現(xiàn)對(duì)象深拷貝功能,涉及javascript元素遍歷與屬性操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-03-03
JavaScript中forEach遍歷數(shù)組舉例詳解
許多小伙伴對(duì)forEach語句應(yīng)該再熟悉不過了,無論是前端遍歷渲染還是js中的邏輯處理,這篇文章主要介紹了JavaScript中forEach方法的使用方式、優(yōu)缺點(diǎn)以及實(shí)際應(yīng)用場(chǎng)景,幫助開發(fā)者更好地掌握這一常見的數(shù)組遍歷技術(shù),需要的朋友可以參考下2025-03-03
解決npm安裝Electron緩慢網(wǎng)絡(luò)超時(shí)導(dǎo)致失敗的問題
下面小編就為大家分享一篇解決npm安裝Electron緩慢網(wǎng)絡(luò)超時(shí)導(dǎo)致失敗的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02

