話不多說,先上效果圖:
實作代碼:
wxml
<view class="address-selecter">
<text class='chooseAddress_text' bindtap='selectDistrict'>{{address}}</text>
<view class="picker-view" animation="{{animationAddressMenu}}"
style="visibility:{{addressMenuIsShow ? 'visible':'hidden'}}">
<view style="height:10% ;width:95%;margin-top:10rpx">
<text catchtap="cityCancel" class='cancel_text'>取消</text>
<text style="float: right" catchtap="citySure" class='sure_text'>确定</text>
</view>
<picker-view style="width: 100%; height: 300px;" bindchange="_cityChange" value="{{value}}" >
<picker-view-column>
<view wx:for="{{provinces}}" class="picker-item" wx:key="*this">
{{item.name}}</view>
</picker-view-column>
<picker-view-column>
<view wx:for="{{citys}}" class="picker-item" wx:key="*this">
{{item.name}}</view>
</picker-view-column>
<picker-view-column>
<view wx:for="{{areas}}" class="picker-item" wx:key="*this">
{{item.name}}</view>
</picker-view-column>
</picker-view>
</view>
</view>
wxss
.chooseAddress_text{
margin-left: 20rpx;
font-size: 30rpx;
text-align: center;
}
.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;
}
.picker-view .cancel_text{
color: green;
}
.picker-view .sure_text{
color: green;
}
json
{
"component": true,
"usingComponents": {}
}
js
var util = require("../../utils/util.js")
var address = require('../../utils/city.js')
Component({
/**
* 元件的屬性清單
*/
properties: {
address:{
type: String,
value: '點選選擇位址'
}
},
/**
* 元件的初始資料
*/
data: {
animation: {},
animationAddressMenu: {},
addressMenuIsShow: false,
value: [0, 0, 0],
provinces: [],
citys: [],
areas: [],
address: '',
},
/**
* 生命周期函數
*/
lifetimes:{
attached: function () {
let id = address.provinces[0].id
this.setData({
provinces: address.provinces,
citys: address.citys[id],
areas: address.areas[address.citys[id][0].id],
})
},
},
/**
* 元件的方法清單
*/
methods: {
/**
* 點選所在地區彈出選擇框
*/
selectDistrict: function (e) {
var that = this
if (that.data.addressMenuIsShow) {
return
}
that._startAddressAnimation(true)
},
/**
* 取消按鈕
*/
cityCancel: function (e) {
this._startAddressAnimation(false)
},
/**
* 确定按鈕
*/
citySure: function (e) {
var that = this
let value = that.data.value
that._startAddressAnimation(false)
// 所選擇的城市資訊
let address_now = that.data.provinces[value[0]].name +
',' + that.data.citys[value[1]].name + ',' + that.data.areas[value[2]].name
console.log(address_now)
that.setData({
address: address_now
})
that.triggerEvent('select', [address_now, that.data.provinces[value[0]].name,
that.data.citys[value[1]].name, that.data.areas[value[2]].name])
},
/**
* 執行動畫
*/
_startAddressAnimation: function (isShow) {
var that = this
if (isShow) {
that.animation = wx.createAnimation({
duration: 1000,
timingFunction: 'ease',
}),
that.animation.translateY(0 + 'vh').step()
} else {
that.animation.translateY(40 + 'vh').step()
}
that.setData({
animationAddressMenu: that.animation.export(),
addressMenuIsShow: isShow,
})
},
/**
* 點選蒙版時取消元件的顯示
*/
_hideCitySelected: function (e) {
this._startAddressAnimation(false)
},
/**
* 處理省市縣關聯邏輯
*/
_cityChange: function (e) {
let value = e.detail.value
let provinces = this.data.provinces
let citys = this.data.citys
let areas = this.data.areas
let provinceNum = value[0]
let cityNum = value[1]
let countyNum = value[2]
// 如果省份選擇項和之前不一樣,表示滑動了省份,此時市預設是省的第一組資料,
if (this.data.value[0] != provinceNum) {
let 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) {
// 滑動選擇了第二項資料,即市,此時區顯示省市對應的第一組資料
let id = citys[cityNum].id
this.setData({
value: [provinceNum, cityNum, 0],
areas: address.areas[citys[cityNum].id],
})
} else {
// 滑動選擇了區
this.setData({
value: [provinceNum, cityNum, countyNum]
})
}
},
}
})
使用方式:
<!--位址-->
<view class="address">
<text class='address_text'>位址:</text>
<address-selecter bind:select="addressSelecter"></address-selecter>
</view>
/**
* 位址選擇
*/
addressSelecter(e){
console.log(e)
},
必要檔案下載下傳位址