微信小程序 動(dòng)態(tài)綁定數(shù)據(jù)及動(dòng)態(tài)事件處理
微信小程序 動(dòng)態(tài)綁定數(shù)據(jù)及動(dòng)態(tài)事件處理
關(guān)鍵核心代碼
<image class="midimage" data-Type="{{item.Type}}" data-BillCode="{{item.BillCode}}" data-src="{{item.imgurl}}" src="{{item.imgurl}}" mode="scaleToFill" bindtap="addtaskimg" bindlongtap="imglongtap" ></image>
var objurl= JSON.parse(res.data); //重置圖片參數(shù) var temppostionlist=that.data.postionlist; for (var i=0;i<temppostionlist.length;i++) { if (temppostionlist[i]["Type"]==e.currentTarget.dataset.type){ temppostionlist[i]["imgurl"]=temppaths; temppostionlist[i]["serverimgurl"]=objurl.Body.Data; break; } } that.setData( { postionlist:temppostionlist })
利用image的data-Type,可以在js后臺(tái)中知道是點(diǎn)擊了哪個(gè)image,上傳圖片后再循環(huán)得出相等data-type的數(shù)據(jù)項(xiàng)進(jìn)行賦值,這樣就能動(dòng)態(tài)給相應(yīng)的標(biāo)簽賦值
addtaskimg 為統(tǒng)一的動(dòng)態(tài)事件
主要代碼如下
.wxml
<scroll-view class="center" scroll-y="true"> <view class="midcenter" wx:for="{{postionlist}}"> <view class="mid_top" > <image class="smallimage" src="../images/my/XXH/line_title.png" mode="aspectFit" ></image> <text>{{item.KeyValue}}</text> </view> <view class="mid_center"> <text>{{item.Remark}}</text> </view> <view class="mid_bottom"> <image class="midimage" data-Type="{{item.Type}}" data-BillCode="{{item.BillCode}}" data-src="{{item.imgurl}}" src="{{item.imgurl}}" mode="scaleToFill" bindtap="addtaskimg" bindlongtap="imglongtap" ></image> </view> </view> </scroll-view>
.js
addtaskimg:function(e){ //添加選擇圖片 if (this.data.blongtap) { //處理如果是長(zhǎng)按,則不再執(zhí)行下面的 this.setData({ blongtap:false }); return; } var that = this; wx.chooseImage({ count:1, //默認(rèn)1張 success:function(res){ //先上傳至服務(wù)器,再返回路徑供保存 var temppaths=res.tempFilePaths[0];//+".jpg"; wx.uploadFile({ url:app.globalData.ghost+"YWTask/PutUploadFile?BillCode="+that.data.detail.BillCode+"&ID="+that.data.id, filePath:temppaths, name:'image', formData:{}, success:function(res){ //res.data返回的是一個(gè)字符串,需進(jìn)行轉(zhuǎn)換成objcet,wx不識(shí)別eval函數(shù) console.log(res.Data); var objurl= JSON.parse(res.data); //重置圖片參數(shù) var temppostionlist=that.data.postionlist; for (var i=0;i<temppostionlist.length;i++) { if (temppostionlist[i]["Type"]==e.currentTarget.dataset.type){ temppostionlist[i]["imgurl"]=temppaths; temppostionlist[i]["serverimgurl"]=objurl.Body.Data; break; } } that.setData( { postionlist:temppostionlist }) } }) } }) }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
JS實(shí)現(xiàn)的透明度漸變動(dòng)畫(huà)效果示例
這篇文章主要介紹了JS實(shí)現(xiàn)的透明度漸變動(dòng)畫(huà)效果,涉及javascript響應(yīng)鼠標(biāo)事件針對(duì)頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-04-04分享8個(gè)JavaScript庫(kù)可更好地處理本地存儲(chǔ)
這篇文章主要介紹了分享8個(gè)JavaScript庫(kù)可更好地處理本地存儲(chǔ),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10再談javascript常見(jiàn)錯(cuò)誤及解決方法
下面小編就為大家?guī)?lái)一篇再談javascript常見(jiàn)錯(cuò)誤及解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09layui 阻止圖片上傳的實(shí)例(before方法)
今天小編就為大家分享一篇layui 阻止圖片上傳的實(shí)例(before方法),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09JavaScript實(shí)現(xiàn)星星等級(jí)評(píng)價(jià)功能
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)星星等級(jí)評(píng)價(jià)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Jvascript學(xué)習(xí)實(shí)踐案例(開(kāi)發(fā)常用)
一些在Jvascript學(xué)習(xí)實(shí)踐的實(shí)例代碼,需要的朋友可以參考下2012-06-06