小程序省市区三级联动

小程序从数据库中读取省市区用多列选择器显示

wxml代码

<picker mode="multiSelector" bindchange="bindMultiPickerChange" bindcolumnchange="bindMultiPickerColumnChange" value="{{areaIndex}}" range="{{areaList}}" range-key="name" class="search-picker">
          <block wx:if="{{!regionChoose}}">
          <view class="picker">
            <text>请选择公司地址</text>
          </view>
          </block>
          <block wx:else>
          <view class="picker">
            {{areaList[0][areaIndex[0]].name}},{{areaList[1][areaIndex[1]].name}},{{areaList[2][areaIndex[2]].name}}
          </view>
          </block>
 </picker>

js代码

data: {
    areaList: [],
    areaIndex: [0, 0, 0],
    regionChoose:false,//判断地区是否选择
    },
/**
     * 获取省
     */
  getProvince: function () {
    var that = this;
    wx.request({
      url: util.baseurl + 'Company/getProvince',
      data: {},
      method: 'post',
      dataType: 'json',
      success: function (res) {
        console.log(res.data);
        that.setData({
          areaList: [res.data, [], []]
        }, function () {
          var provinceid = that.data.areaList[0];
          that.getCity(provinceid[0].id);
        })
      }
    })
  },
  /**
   * 获取市
   */
  getCity: function (id) {
    var that = this;
    wx.request({
      url: util.baseurl + 'Company/getProvince',
      data: { id: id },
      method: 'post',
      dataType: 'json',
      success: function (res) {
        that.setData({
          areaList: [that.data.areaList[0], res.data, []]
        }, function (res) {
          var cityid = that.data.areaList[1];
          that.getArea(cityid[0].id);
        })
      }
    })
  },
  /**
   * 获取区县
   */
  getArea: function (id) {
    var that = this;
    wx.request({
      url: util.baseurl + 'Company/getProvince',
      data: { id: id },
      method: 'post',
      success: function (res) {
        that.setData({
          areaList: [that.data.areaList[0], that.data.areaList[1], res.data]
        })
      }
    })
  },
  //值改变
  bindMultiPickerChange: function (e) {
    this.setData({
      areaIndex: e.detail.value,
      regionChoose:true,
    })
  },

  //获取列的改变
  bindMultiPickerColumnChange: function (e) {
    console.log('列改变--', e);
    switch (e.detail.column) {
      case 0:
        var provinceid = this.data.areaList[0][e.detail.value].id;
        console.log(provinceid);
        this.getCity(provinceid);
        break;
      case 1:
        var cityid = this.data.areaList[1][e.detail.value].id;
        this.getArea(cityid);
        break;
    }
  },
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值