index.js 1.92 KB
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    provinceShow: Boolean,
    provinceList: {
      type: Array,
      observer: function (val) {
        console.log(val, '++++');
        this.setData({
          provinceList: val
        })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    region: [0, 0, 0], // 地址每列的index
    regionCode: [],
    provinceList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // emitHideRegion(){},
    // 省市切换
    bindChange(e) {
      // 用于对比滑动的是哪一列数据
      const val = e.detail.value
      if (this.data.region[0] !== val[0]) {
        // 如果滑动的是第一列数据,让二三列恢复到0
        this.setData({
          region: [val[0], 0, 0]
        })
      } else if (this.data.region[1] !== val[1]) {
        // 滑动的是第二列数据
        this.setData({
          region: [val[0], val[1], 0]
        })
      } else {
        this.setData({
          region: e.detail.value
        })
      }
    },
    // 确定提交
    comfirm() {
      const {provinceList, region} = this.data
      // 省市code
      const provinceCode = provinceList[region[0]].value
      const cityCode = provinceList[region[0]].children[region[1]].value
      const townCode = provinceList[region[0]].children[region[1]].children[region[2]].value
      const codeData = [provinceCode, cityCode, townCode]
      // 省市名称
      const provinceName = provinceList[region[0]].label
      const cityName = provinceList[region[0]].children[region[1]].label
      const townName = provinceList[region[0]].children[region[1]].children[region[2]].label
      const provinceNameData = [provinceName, cityName, townName]
      this.triggerEvent('getProvinceCode', {codeData, provinceNameData})
      this.triggerEvent('cancelProvince')
    },
    // 取消
    cancel() {
      this.triggerEvent('cancelProvince')
    }
  }
})