element-ui 级联选择Cascader选择省市区

// 设置prop属性
   props:{
    label: 'areaName',
    value: 'areaId',
    //children: 'children',
   },


// 获取省市
getProvence() {
  post(url, {parentId: 0}).then((res) => {
    this.addressObject.list = res && res.data || []
    this.addressObject.list.forEach((item,index)=>{
      this.$set(this.addressObject.list[index], 'children', [])
    })

  })
},
// 级联选择器
handleAddressChange(val) {
  if(val.length <= 0 ) return;
  let provinceList = this.addressObject.list;
  let provinceLength = provinceList.length;
  let provinceAreaId = val[0];

  if(val.length === 1)  {
     // 省areaId, 获取省下面所有的市
    post(url, {parentId: provinceAreaId}).then((res) => {
      for(let i = 0; i < provinceLength; i++) {
        if(provinceList[i].areaId === provinceAreaId) {
          this.$set(provinceList[i], 'children', res && res.data  || []);
          provinceList[i].children.forEach((item,index)=>{
             this.$set(provinceList[i].children[index], 'children', [])
          })
          break;
        }
      }
    })
  }
  if(val.length === 2)  {
    let cityId = val[1]; // 市areaId,获取市下面所有的区
    post(url, {parentId: cityId}).then((res) => {
      for(let i = 0; i < provinceLength; i++) {
        if(this.addressObject.list[i].areaId === provinceAreaId) {
          let findProvince = this.addressObject.list[i];
          let cityList = findProvince.children;
          let cityLength = cityList.length;
          for(let j = 0; j < cityLength; j++){
              if(cityList[j].areaId === cityId) {
                this.$set(cityList[j], 'children', res && res.data  || []);
                break;
              }//if
          }//for
          break;
        }//if
      }//for
    })
  }
},

你可能感兴趣的:(vue,js)