微信小程序的开发之城市三级联动选择器

时间:2022-10-06 05:37:05
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) {
      // vh是用来表示尺寸的单位,高度全屏是100vh
      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],
      })
    }
  }
效果图

微信小程序的开发之城市三级联动选择器