wxml
<view bindtap='selectDistrict' wx:if='{{areaInfo}}'>{{areaInfo}}</view> <view class='shadow' hidden='{{addressMenuIsShow != true}}'></view> <view class="picker-view" animation="{{animationAddressMenu}}" style="visibility:{{addressMenuIsShow ? 'visible':'hidden'}}"> <view class='sureNo'> <text catchtap="cityCancel" class='cancel'>取消</text> <text style="float: right" catchtap="citySure" class='sure'>确定</text> </view> <picker-view 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> </view>
js
var address = require('../../utils/city.js');
Page({
/** * 页面的初始数据 */
/** * 控件当前显示的数据 * provinces:所有省份 * citys 选择省对应的所有市, * areas选择市对应的所有区 * areaInfo:点击确定时选择的省市县字符拼接 * animationAddressMenu:动画 * addressMenuIsShow:是否可见 */
data: {
animationAddressMenu: {},
addressMenuIsShow: false,
value: [0, 0, 0],
provinces: [],
citys: [],
areas: [],
areaInfo: ''
},
/** * 生命周期函数--监听页面加载 */
onLoad: function (options) {
console.log(options);
var userInfo = wx.getStorageSync("userInfo");
var id = address.provinces[0].id
this.setData({
weixin: userInfo.detail.userInfo.nickName,
provinces: address.provinces,
citys: address.citys[id],
areas: address.areas[address.citys[id][0].id],
});
}
selectDistrict: function (e) {
var that = this;
console.log(that);
if (that.data.addressMenuIsShow) {
return
}
that.startAddressAnimation(true)
},
startAddressAnimation: function (isShow) {
var that = this;
var animation = wx.createAnimation({
transformOrigin: "50% 50%",
duration: 200,
timingFunction: "ease-in",
delay: 0
});
if (isShow) {
animation.translateY(0 + 'vh').step()
} else {
animation.translateY(40 + 'vh').step()
}
that.setData({
animationAddressMenu: animation.export(),
addressMenuIsShow: isShow,
})
},
cityCancel: function (e) {
this.startAddressAnimation(false)
},
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,
})
},
hideCitySelected: function (e) {
this.startAddressAnimation(false)
},
cityChange: function (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]
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) {
var id = citys[cityNum].id
this.setData({
value: [provinceNum, cityNum, 0],
areas: address.areas[citys[cityNum].id],
})
} else {
this.setData({
value: [provinceNum, cityNum, countyNum],
})
}
}
效果图