您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)微信小程序開發(fā)中如何實(shí)現(xiàn)三級聯(lián)動地址選擇器,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
在正式介紹實(shí)現(xiàn)之前,我們需要先來介紹下這兩個(gè)組件,picker這個(gè)組件在前面的文章有簡單介紹過,它是從底部彈起的滾動選擇器,可以通過設(shè)置type的值來實(shí)現(xiàn)日期選擇,時(shí)間選擇以及普通的選擇器,如果我們想實(shí)現(xiàn)上圖三級聯(lián)動地址選擇效果,發(fā)現(xiàn)實(shí)現(xiàn)起來很困難,應(yīng)該說是不能實(shí)現(xiàn),因?yàn)閜icker普通選擇器是只能有一列,如果想這實(shí)現(xiàn)三列的效果就需要另辟蹊徑了。
既然是另辟蹊徑,自然就會想到picker-view,該組件是一個(gè)嵌入頁面的滾動選擇器,該組件中可以放置多個(gè)picker-view-column,并且只能放置picker-view-column,其它組件是不會顯示的,每一個(gè)picker-view-column就是一列。
picker-view有幾個(gè)重要的屬性,value是一個(gè)數(shù)組類型,數(shù)組中的數(shù)字依次表示 picker-view 內(nèi)的 picker-view-colume 選擇的第幾項(xiàng)(下標(biāo)從 0 開始),數(shù)字大于 picker-view-column 可選項(xiàng)長度時(shí),選擇最后一項(xiàng)。indicator-style和indicator-class可以設(shè)置選擇器中間選中框的樣式,他有一個(gè)事件bindchange,當(dāng)我們滾動item時(shí)選擇的item數(shù)據(jù)發(fā)生變化就會觸發(fā)這個(gè)函數(shù),并且可以通過event.detai.vaule(和上面介紹vaule含義相同)獲取當(dāng)前選擇的是第幾項(xiàng)(下標(biāo)從 0 開始)。而對于picker-view-column高度會自動設(shè)置成與picker-view的選中框的高度一致。
省市縣數(shù)據(jù)文件 存儲了地址選擇所需要用到的數(shù)據(jù),主要是區(qū)域碼和名字,然后通過下面代碼將數(shù)據(jù)暴露出去,以供使用
module.exports = { citys, provinces, areas }
wxml文件實(shí)現(xiàn)
<view class="picker-view" animation="{{animationAddressMenu}}" style="visibility:{{addressMenuIsShow ? 'visible':'hidden'}}"> <view style="height:10% ;width:95%;margin-top:10rpx"><text catchtap="cityCancel">取消text><text style="float: right" catchtap="citySure">確定text> view> <picker-view style="width: 100%; height: 300px;" bindchange="cityChange" value="{{value}}" wx:key=""><picker-view-column> <view wx:for="{{provinces}}" class="picker-item">{{item.name}}view>picker-view-column><picker-view-column> <view wx:for="{{citys}}" class="picker-item" wx:key="">{{item.name}}view>picker-view-column><picker-view-column> <view wx:for="{{areas}}" class="picker-item" wx:key="">{{item.name}}view>picker-view-column> picker-view>view>
主要就是上面有一個(gè)取消和確定供用戶點(diǎn)擊確認(rèn)選擇,以及picker-view 中包含三個(gè)picker-view-column組件,分別對應(yīng)顯示省市區(qū),provinces,citys,areas是對應(yīng)的數(shù)據(jù),animation是選擇控件可見或者不可見時(shí)的過渡動畫。
樣式wxss文件
.picker-view { width: 100%; display: flex; z-index:12; background-color: #fff; flex-direction: column; justify-content: center; align-items: center; position: fixed; bottom: 0rpx; left: 0rpx; height: 40vh; }.picker-item { line-height: 70rpx; margin-left: 5rpx; margin-right: 5rpx; text-align: center; }
在js中我們在data中加入數(shù)據(jù)
/** * 控件當(dāng)前顯示的數(shù)據(jù) * provinces:所有省份 * citys 選擇省對應(yīng)的所有市, * areas選擇市對應(yīng)的所有區(qū) * areaInfo:點(diǎn)擊確定時(shí)選擇的省市縣字符拼接 * animationAddressMenu:動畫 * addressMenuIsShow:是否可見 */ data: {animationAddressMenu: {},addressMenuIsShow: false,value: [0, 0, 0],provinces: [],citys: [],areas: [],areaInfo:'' },
最重要的是在js文件開始處引入數(shù)據(jù)文件
var address = require('../../utils/city.js')
onLoad中初始化數(shù)據(jù),默認(rèn)顯示北京
// 默認(rèn)聯(lián)動顯示北京var id = address.provinces[0].idthis.setData({ provinces: address.provinces, citys: address.citys[id], areas: address.areas[address.citys[id][0].id], })
事件的處理邏輯:
// 點(diǎn)擊所在地區(qū)彈出選擇框 selectDistrict: function (e) {var that = this// 如果已經(jīng)顯示,不在執(zhí)行顯示動畫if (that.data.addressMenuIsShow) { return}// 執(zhí)行顯示動畫that.startAddressAnimation(true) }, // 執(zhí)行動畫 startAddressAnimation: function (isShow) { console.log(isShow)var that = thisif (isShow) { // vh是用來表示尺寸的單位,高度全屏是100vh that.animation.translateY(0 + 'vh').step() } else { that.animation.translateY(40 + 'vh').step() } that.setData({ animationAddressMenu: that.animation.export(), addressMenuIsShow: isShow, }) }, // 點(diǎn)擊地區(qū)選擇取消按鈕 cityCancel: function (e) {this.startAddressAnimation(false) }, // 點(diǎn)擊地區(qū)選擇確定按鈕 citySure: function (e) {var that = thisvar city = that.data.cityvar value = that.data.value that.startAddressAnimation(false)// 將選擇的城市信息顯示到輸入框var areaInfo = that.data.provinces[value[0]].name + ',' + that.data.citys[value[1]].name + ',' + that.data.areas[value[2]].name that.setData({ areaInfo: areaInfo, }) }, // 點(diǎn)擊蒙版時(shí)取消組件的顯示 hideCitySelected: function (e) { console.log(e)this.startAddressAnimation(false) }, // 處理省市縣聯(lián)動邏輯 cityChange: function (e) { console.log(e)var value = e.detail.valuevar provinces = this.data.provincesvar citys = this.data.citysvar areas = this.data.areasvar provinceNum = value[0]var cityNum = value[1]var countyNum = value[2]// 如果省份選擇項(xiàng)和之前不一樣,表示滑動了省份,此時(shí)市默認(rèn)是省的第一組數(shù)據(jù),if (this.data.value[0] != provinceNum) { var id = provinces[provinceNum].id this.setData({ value: [provinceNum, 0, 0], citys: address.citys[id], areas: address.areas[address.citys[id][0].id], }) } else if (this.data.value[1] != cityNum) { // 滑動選擇了第二項(xiàng)數(shù)據(jù),即市,此時(shí)區(qū)顯示省市對應(yīng)的第一組數(shù)據(jù) var id = citys[cityNum].id this.setData({ value: [provinceNum, cityNum, 0], areas: address.areas[citys[cityNum].id], }) } else { // 滑動選擇了區(qū) this.setData({ value: [provinceNum, cityNum, countyNum] }) } console.log(this.data) },
關(guān)于“微信小程序開發(fā)中如何實(shí)現(xiàn)三級聯(lián)動地址選擇器”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。
免責(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)容。