微信小程序動(dòng)態(tài)添加view組件的實(shí)例代碼
在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è)類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í)候,能對(duì)view動(dòng)態(tài)增刪了,那么對(duì)數(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í)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 微信小程序自定義tabbar custom-tab-bar 6s出不來解決方案(cover-view不兼容)
- 微信小程序 scroll-view 水平滾動(dòng)實(shí)現(xiàn)過程解析
- 詳解解決小程序中webview頁面多層history返回問題
- 微信小程序webview與h5通過postMessage實(shí)現(xiàn)實(shí)時(shí)通訊的實(shí)現(xiàn)
- 微信小程序移動(dòng)拖拽視圖-movable-view實(shí)例詳解
- 微信小程序webview組件交互,內(nèi)聯(lián)h5頁面并網(wǎng)頁實(shí)現(xiàn)微信支付實(shí)現(xiàn)解析
- 微信小程序webview 腳手架使用詳解
- 微信小程序與webview交互實(shí)現(xiàn)支付功能
- 微信小程序點(diǎn)擊view動(dòng)態(tài)添加樣式過程解析
相關(guān)文章
原生javascript實(shí)現(xiàn)勻速運(yùn)動(dòng)動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了原生javascript實(shí)現(xiàn)勻速運(yùn)動(dòng)動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02
echarts設(shè)置圖例顏色和地圖底色的方法實(shí)例
最近項(xiàng)目要使用echarts進(jìn)行數(shù)據(jù)可視化,所以下面這篇文章主要給大家介紹了關(guān)于echarts設(shè)置圖例顏色和地圖底色的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
uniapp退出關(guān)閉當(dāng)前小程序或APP的簡(jiǎn)單實(shí)現(xiàn)
最近通過Uniapp開發(fā)APP又一個(gè)非常實(shí)用的功能,這篇文章主要給大家介紹了關(guān)于uniapp退出關(guān)閉當(dāng)前小程序或APP的簡(jiǎn)單實(shí)現(xiàn),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
js控制一個(gè)按鈕是否可點(diǎn)擊(可使用)disabled的實(shí)例
下面小編就為大家?guī)硪黄猨s控制一個(gè)按鈕是否可點(diǎn)擊(可使用)disabled的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
原生JavaScript再網(wǎng)頁實(shí)現(xiàn)文本轉(zhuǎn)語音功能
這篇文章主要為大家詳細(xì)介紹了如何通過原生JavaScript再網(wǎng)頁實(shí)現(xiàn)文本轉(zhuǎn)語音功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2025-03-03
JavaScript使用Broadcast?Channel實(shí)現(xiàn)前端跨標(biāo)簽頁通信
這篇文章主要為大家詳細(xì)介紹了JavaScript如何使用Broadcast?Channel實(shí)現(xiàn)前端跨標(biāo)簽頁通信,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04

