溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

微信小程序中三級(jí)聯(lián)動(dòng)地址選擇器的示例分析

發(fā)布時(shí)間:2021-07-28 13:48:32 來源:億速云 閱讀:128 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章給大家分享的是有關(guān)微信小程序中三級(jí)聯(lián)動(dòng)地址選擇器的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

效果圖

微信小程序中三級(jí)聯(lián)動(dòng)地址選擇器的示例分析

picker和picker-view組件

在正式介紹實(shí)現(xiàn)之前,我們需要先來介紹下這兩個(gè)組件,picker這個(gè)組件在前面的文章有簡單介紹過,它是從底部彈起的滾動(dòng)選擇器,可以通過設(shè)置type的值來實(shí)現(xiàn)日期選擇,時(shí)間選擇以及普通的選擇器,如果我們想實(shí)現(xiàn)上圖三級(jí)聯(lián)動(dòng)地址選擇效果,發(fā)現(xiàn)實(shí)現(xiàn)起來很困難,應(yīng)該說是不能實(shí)現(xiàn),因?yàn)閜icker普通選擇器是只能有一列,如果想這實(shí)現(xiàn)三列的效果就需要另辟蹊徑了。

既然是另辟蹊徑,自然就會(huì)想到picker-view,該組件是一個(gè)嵌入頁面的滾動(dòng)選擇器,該組件中可以放置多個(gè)picker-view-column,并且只能放置picker-view-column,其它組件是不會(huì)顯示的,每一個(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)我們滾動(dòng)item時(shí)選擇的item數(shù)據(jù)發(fā)生變化就會(huì)觸發(fā)這個(gè)函數(shù),并且可以通過event.detai.vaule(和上面介紹vaule含義相同)獲取當(dāng)前選擇的是第幾項(xiàng)(下標(biāo)從 0 開始)。而對(duì)于picker-view-column高度會(huì)自動(dòng)設(shè)置成與picker-view的選中框的高度一致。

省市縣數(shù)據(jù)文件 存儲(chǔ)了地址選擇所需要用到的數(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組件,分別對(duì)應(yīng)顯示省市區(qū),provinces,citys,areas是對(duì)應(yīng)的數(shù)據(jù),animation是選擇控件可見或者不可見時(shí)的過渡動(dòng)畫。

樣式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 選擇省對(duì)應(yīng)的所有市,
  * areas選擇市對(duì)應(yīng)的所有區(qū)
  * areaInfo:點(diǎn)擊確定時(shí)選擇的省市縣字符拼接
  * animationAddressMenu:動(dòng)畫
  * 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)動(dòng)顯示北京
  var id = address.provinces[0].id
  this.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í)行顯示動(dòng)畫
  if (that.data.addressMenuIsShow) {
   return
  }
  // 執(zhí)行顯示動(dòng)畫
  that.startAddressAnimation(true)
 },
 // 執(zhí)行動(dòng)畫
 startAddressAnimation: function (isShow) {
  console.log(isShow)
  var that = this
  if (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 = this
  var city = that.data.city
  var 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)動(dòng)邏輯
 cityChange: function (e) {
  console.log(e)
  var value = e.detail.value
  var provinces = this.data.provinces
  var citys = this.data.citys
  var areas = this.data.areas
  var provinceNum = value[0]
  var cityNum = value[1]
  var countyNum = value[2]
  // 如果省份選擇項(xiàng)和之前不一樣,表示滑動(dò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) {
   // 滑動(dòng)選擇了第二項(xiàng)數(shù)據(jù),即市,此時(shí)區(qū)顯示省市對(duì)應(yīng)的第一組數(shù)據(jù)
   var id = citys[cityNum].id
   this.setData({
    value: [provinceNum, cityNum, 0],
    areas: address.areas[citys[cityNum].id],
   })
  } else {
   // 滑動(dòng)選擇了區(qū)
   this.setData({
    value: [provinceNum, cityNum, countyNum]
   })
  }
  console.log(this.data)
 },

對(duì)于事件處理,聯(lián)動(dòng)的處理邏輯,需要解釋的我已在代碼實(shí)現(xiàn)中做了解釋,也沒有什么比較難以理解的內(nèi)容,就不在過多介紹,到這里微信小程序省市縣三級(jí)聯(lián)動(dòng)效果已經(jīng)實(shí)現(xiàn)了。

感謝各位的閱讀!關(guān)于“微信小程序中三級(jí)聯(lián)動(dòng)地址選擇器的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI