el-cascader 后端接口实现省市区联动

效果图:

在这里插入图片描述

核心代码(后期优化代码)

<el-cascader
          ref="cascaderMallCatergory"
          v-model="address"
          clearable
          placeholder="请选择省市区"
          :options="provinceList"
          :props="props"
          @active-item-change="handleItemChange"
          @change="handleMallCatergoryChange"
        ></el-cascader>

handleItemChange(val) {
      let map = new Map()
      map.set('北京市', '110000')
      map.set('天津市', '120000')
      map.set('重庆市', '500000')
      map.set('上海市', '310000')
      console.log(val)
      if (val.length === 1) {
        // 查询市区
        this.provinceList.map((item) => {
          if (item.value === val[0]) {
            // 四个直辖市特殊处理
            if( map.has(item.label)){
              return item.cities = [{value:item.label,label: item.label, cities:[]}]
            }
            getCityList({ provinceCode: val[0] }).then(res => {
              item.cities = res.data.map((e) => {
                return { value: e.code, label: e.name, cities: [] }
              })
            })
          }
        })
      } else if (val.length === 2) {
        //加载3级   查询该省市下县级数据
        this.provinceList.map((item) => {
          if (item.value === val[0]) {
            item.cities.map((value) => {
              // 天津市区
              if (value.value === val[1]) {
                if (map.has(value.label)) {
                  console.log('直辖市', value.label)
                  let code = map.get(value.label)
                  getDistrictList({ cityCode: code }).then(res => {
                    value.cities = res.data.map((e) => {
                      return {
                        value: e.code, label: e.name
                      }
                    })
                  })
                  return
                }

                getDistrictList({ cityCode: val[1] }).then(res => {
                  value.cities = res.data.map((e) => {
                    return {
                      value: e.code, label: e.name
                    }
                  })
                })
              }
            })
          }
        })
        console.log('=========', this.provinceList)
      }
    }



handleMallCatergoryChange() {
      if (this.address.length !== 0) {
        let arr = this.$refs['cascaderMallCatergory'].getCheckedNodes()[0].pathLabels
        console.log('mallCatergoryCode', this.address)
        console.log('arr', arr)
        // this.queryParams.installationName = arr.join('')
      }
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值