您好,登錄后才能下訂單哦!
在web中,我們動(dòng)態(tài)添加DOM,可以用jQuery的方法,很簡單。在微信小程序中怎么實(shí)現(xiàn)下面這么需求。
其中,里程數(shù)代表上一行到這一行地方的距離(這個(gè)不重要);要實(shí)現(xiàn)的就是點(diǎn)擊增加途徑地,就多一行,刪除途徑地,就少一行。
分析:添加的和刪除的是同樣的結(jié)構(gòu),只是數(shù)量不一樣,所以考慮循環(huán),用列表表示,增加就往這個(gè)列表push一個(gè),刪除就從列表pop一個(gè)。
主要代碼如下:
<view class="weui-cell weui-cell_input"> <view class="weui-cell__hd"> <view class="weui-label rui-justify"> <text space="ensp">出 發(fā) 地</text> </view> </view> <view class="weui-cell__bd"> <input class="weui-input" name="beginAddress" bindinput="inputedit" data-obj="info" data-item="beginAddress" value="{{info.beginAddress}}" placeholder="請輸入出發(fā)地" disabled="{{info.isPreDetail}}" /> </view> </view> <view wx:for="{{info.details}}" wx:key="key" class="forItemBorder"> <view class="weui-cell weui-cell_input "> <view class="weui-cell__hd"> <view class="weui-label rui-justify"> <text space="ensp">途 徑 地</text> </view> </view> <input class="weui-input" id="place-{{index}}" bindinput="setPlace" placeholder="請輸入途徑地" /> <input class="weui-input lcs" id="number-{{index}}" bindinput="setNumber" placeholder="里程數(shù)(km)" /> </view> </view> <view class="weui-cell weui-cell_input"> <view class="weui-cell__hd"> <view class="weui-label rui-justify"> <text space="ensp">目 的 地</text> </view> </view> <input class="weui-input" name="destination" bindinput="inputedit" data-obj="info" data-item="destination" value="{{info.destination}}" placeholder="請輸入目的地" disabled="{{info.isPreDetail}}" /> <input class="weui-input lcs" name="endLength" bindinput="inputedit" data-obj="info" data-item="endLength" value="{{info.endLength}}" placeholder="里程數(shù)(km)" disabled="{{info.isPreDetail}}" /> </view> <view class="weui-cell" hidden="{{info.isPreDetail}}"> <button type='primary' bindtap='addItem' style='width:50%;'>增加途徑地</button> <button type='primary' bindtap='removeItem' style='width:50%;margin-left:5rpx;'> 刪除途徑地 </button> </view>
因?yàn)樘砑觿h除的是相同的結(jié)構(gòu),所以我構(gòu)造了一個(gè)類Detail來表示這個(gè)view
/** * Detail類 構(gòu)造函數(shù) * @param {string} placeName 途徑地 * @param {string} number 里程數(shù) */ function Detail(placeName, number) { this.placeName = placeName; this.number = number; } function Info() { this.details = []; } Page({ data: { info: {} }, init: function () { let that = this; this.setData({ info: new Info(), }); }, onLoad: function (options) { this.init(); }, addItem: function (e) { let info = this.data.info; info.details.push(new Detail()); this.setData({ info: info }); }, removeItem: function (e) { let info = this.data.info; info.details.pop(); this.setData({ info: info }); }, })
這時(shí)候,能對view動(dòng)態(tài)增刪了,那么對數(shù)據(jù)怎么處理,給每個(gè)生成的view添加id屬性,通過id屬性來判斷操作的是哪個(gè)Detail類。js部分代碼如下:
setPlace: function (e) { let index = parseInt(e.currentTarget.id.replace("place-", "")); let place = e.detail.value; let info = this.data.info; info.details[index].placeName = place; this.setData({ info: info }); }, setNumber: function (e) { let index = parseInt(e.currentTarget.id.replace("number-", "")); let number = e.detail.value; let info = this.data.info; info.details[index].number = number; this.setData({ info: info }); },
總結(jié)
以上所述是小編給大家介紹的微信小程序動(dòng)態(tài)添加view組件的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。