亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

微信小程序動(dòng)態(tài)添加view組件的實(shí)例代碼

 更新時(shí)間:2019年05月23日 17:16:15   作者:lixin051435  
本文通過(guò)實(shí)例代碼給大家介紹了微信小程序動(dòng)態(tài)添加view組件的方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

在web中,我們動(dòng)態(tài)添加DOM,可以用jQuery的方法,很簡(jiǎn)單。在微信小程序中怎么實(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="請(qǐng)輸入出發(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="請(qǐng)輸入途徑地" />
 <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="請(qǐng)輸入目的地" 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è)類(lèi)Detail來(lái)表示這個(gè)view

/**
 * Detail類(lèi) 構(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í)候,能對(duì)view動(dòng)態(tài)增刪了,那么對(duì)數(shù)據(jù)怎么處理,給每個(gè)生成的view添加id屬性,通過(guò)id屬性來(lái)判斷操作的是哪個(gè)Detail類(lèi)。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í)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論