自定义原生小程序国家省份城市选择器

  html:

<view class="option">
  <label class="left-label residence-label">居住地</label>
  <picker class="picker" mode="multiSelector" bindchange="residenceChange" 
    bindcolumnchange="residenceColumnChange" range="{{ residencePickerData }}">
    <view class="right-content">
      <input type="text" class="input residence" value="{{ initialize.residence }}" 
        disabled name="residence" placeholder="选择你的居住地" 
        placeholder-style="#9C9C9C"/>
      <icon class="iconfont icon-xiangzuojiantou-"></icon>
    </view>
  </picker>
</view>

js变量:

data () {
    return {
        initialize: {
            residence: ""  // 现居地
        },
        residenceData: [[], [], []],  // 现居地总数据
        residencePickerData: [[], [], []]  // 现居地picker数据
    }
},

js方法:

/**
 * 获取居住地数据
 */
getResidenceData(country_region_id = "", state_id = "") {
  wx.request({
    url: API.service.getLocations,
    method: 'post',
    data: {
      country_region_id,
      state_id
    },
    success: res => {
      if (res.statusCode == 200) {
        let residenceData = [];
        let residencePickerData = [];
        let index = !country_region_id && !state_id ? 0 : (country_region_id ? 1 : 2 );
        res.data.data.forEach(item => {
          residenceData.push(item);
          residencePickerData.push(item.name);
        });
        this.setData({
          [`residenceData[${index}]`]: residenceData,
          [`residencePickerData[${index}]`]: residencePickerData
        });
        // 判断是否到城市 如果没有则递归获取省份和城市
        if (index == 0) {
          this.getResidenceData(res.data.data[0].id);
        } else  if (index == 1) {
          this.getResidenceData("", res.data.data[0].id);
        };
      } else {
        API.ways.restsCode('获取现居地接口失败');
      };
    },
    fail(err) {
      API.ways.networkError();
    }
  });
},
/**
 * 选择居住地确定
 */
residenceChange(e) {
  let residence = "";
  let residenceIndexArray = e.detail.value;
  residenceIndexArray.forEach((item, index) => {
    residence += `${this.data.residenceData[index][item].name} `;
  });
  this.setData({ 
    "initialize.residence": residence
  });
},
/**
 * 选择居住地行列发生改变
 */
residenceColumnChange(e) {
  let { column, value } = e.detail;
  if (column == 0) {
    this.data.residenceData[1] = [];
    this.data.residenceData[2] = [];
    this.data.residencePickerData[1] = [];
    this.data.residencePickerData[2] = [];
    this.setData({
      residenceData: this.data.residenceData,
      residencePickerData: this.data.residencePickerData
    });
    let country_region_id = this.data.residenceData[column][value].id;
    this.getResidenceData(country_region_id);
  } else if (column == 1) {
    this.data.residenceData[2] = [];
    this.data.residencePickerData[2] = [];
    this.setData({
      residenceData: this.data.residenceData,
      residencePickerData: this.data.residencePickerData
    });
    let state_id = this.data.residenceData[column][value].id;
    this.getResidenceData("", state_id);
  };
}

效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值