您好,登錄后才能下訂單哦!
這篇文章主要介紹小程序下拉框組件的使用示例,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
適用場景
1、省市三級聯(lián)動
2、出生日期選擇
3、性別選擇
4、一般性的下拉選擇等
一、省市三級聯(lián)動使用
注意mode = region,以及value = “一維數(shù)組”
//.wxml <picker mode="region" bindchange="bindViewEvent" data-model="component" data-method="bindSelect" data-name="region" value="{{region}}" custom-item="{{customItem}}"> <view class="picker"> 當(dāng)前選擇:{{region[0]}},{{region[1]}},{{region[2]}} </view> </picker>
二、出生日期選擇
注意mode = date,以及value = “日期字符串”
<picker mode="date" value="{{date}}" start="2015-09-01" end="2017-09-01" bindchange="bindViewEvent" data-model="component" data-method="bindSelect" date-mode="date" data-name="date"> <view class="picker"> {{date}} </view>
三、性別選擇
<picker bindchange="bindViewEvent" data-model="component" data-method="bindSelect" data-name="index" value='{{index}}' range="{{gender}}"> <view class="label-right"> {{gender[index]}} </view> </picker>
以上都需要在.js里設(shè)置相關(guān)初始變量!
var app = getApp(); data: { region:['湖南','長沙','岳麓'], date:'2010-10-10', gender:['男','女'], index:0 } bindViewEvent:function(e){ app.process(this,e); }
相關(guān)js類
//component.js const select = require('../component/select.js'); const upload = require('../component/upload.js'); class component{ constructor(com, that) { this.com = com; this.that = that; } //綁定下拉框選擇事件 bindSelect(data){ let self = this; let mode = data.currentTarget.dataset.mode; let name = data.currentTarget.dataset.name; let picker = new select({ that: self.that, mode: mode, name: name }); picker.change(data.detail.value); } //點擊事件,傳遞參數(shù)為e.currentTarget.dataset bindImageChoose(data){ //圖片上傳 this.uploader = new upload({ that: that, name: data.name, mode: data.mode, count: data.count || 9 }); this.uploader.choose(); } bindImageDel(data){ //圖片上傳 this.uploader = new upload({ that: that, name: data.name, mode: data.mode, count: data.count || 9 }); this.uploader.del(data.index); } } module.exports = component; //select.js /* * 下拉框?qū)ο? */ class picker{ constructor(data){ this.that = data.that; this.name = data.name || 'date'; this.mode = data.mode || 'selector'; } show(name,data){ let view = {}; view[name] = data; this.that.setData(view); } change(data){ let self = this; self.show(self.name, data); } } module.exports = picker; //upload.js class picUploader { constructor(data) { this.that = data.that; this.name = data.name; this.mode = data.mode || 1; this.count = this.model == 1 ? 1 : data.count || 9; } /* * 選擇圖片 */ choose() { const self = this; wx.chooseImage({ count: (self.count - self.that.data[self.name].length), sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success: function (res) { var tempFilePaths = res.tempFilePaths; self.append(tempFilePaths); } }) } /* * 顯示圖片 */ show() { let self = this; let view = {}; view[self.name] = self.that.data[self.name]; self.that.setData(view); } /* * 追加圖片 */ append(data) { const self = this; for (let i = 0; i < data.length; i++) { self.that.data[self.name].push(data[i]); } self.show(); } /* * 刪除圖片 */ del(index) { let self = this; self.that.data[self.name].splice(index, 1); self.show(); } } module.exports = picUploader;
以上是“小程序下拉框組件的使用示例”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。