el-cascader逐级获取省市区街道数据

el-cascader逐级获取数据

代码如下(示例):

<el-cascader
                v-if="cascaderData.length > 0"
                v-model="form.shqAddress"
                placeholder="请选择省市区"
                :options="cascaderData"
                @active-item-change="handleItemChange"
                :props="{
                  value: 'id',
                  label: 'name',
                  children: 'cities',
                  checkStrictly: true,
                }"
                style="width: 100%"
                :key="cascaderKey"
              ></el-cascader>

```html
    handleItemChange(val) {
      this.getNodes(val)
    },

```html

```html
 getNodes(val) {
      let idArea
      let sizeArea
      if (!val) {
        idArea = null
        sizeArea = 0
        this.$http({
          url:("xxx"),
          method: "post",
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.cascaderData = data.data.map((value, i) => {
              return {
                id: value.provinceCode,
                name: value.provinceName,
                cities: []
              }
            })
          } else {
            this.$message.error(data.msg);
          }
        });
      } else if (val.length === 1) {
        idArea = val[0]
        sizeArea = val.length // 3:一级 4:二级 6:三级
        this.$http({
           url:("xxx"),
          method: "post",
          params: { provinceCode: idArea }
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.cascaderData.map((value, i) => {
              if (value.id === val[0]) {
                if (!value.cities.length) {
                  value.cities = data.data.map((value, i) => {
                    return {
                      id: value.cityCode,
                      name: value.cityName,
                      cities: []
                    }
                  })
                }
              }
            })
          } else {
            this.$message.error(data.msg);
          }
        })
      } else if (val.length === 2) {
        idArea = val[1]
        sizeArea = val.length // 3:一级 4:二级 6:三级
        this.$http({
           url:("xxx"),
          method: "post",
          params: { cityCode: idArea }
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.cascaderData.map((value, i) => {
              if (value.id === val[0]) {
                value.cities.map((value, i) => {
                  if (value.id === val[1]) {
                    if (!value.cities.length) {
                      value.cities = data.data.map((value, i) => {
                        return {
                          id: value.countyCode,
                          name: value.countyName,
                          cities: []
                        }
                      })
                    }
                  }
                })
              }
            })
          }
        })
      } else if (val.length === 3) {
        idArea = val[2]
        sizeArea = val.length // 3:一级 4:二级 6:三级
        this.$http({
           url:("xxx"),
          method: "post",
          params: { countyCode: idArea }
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.cascaderData.map((value, i) => {
              if (value.id === val[0]) {
                value.cities.map((value, i) => {
                  if (value.id === val[1]) {
                    value.cities.map((value, i) => {
                      if (value.id === val[2]) {
                        if (!value.cities.length) {
                          value.cities = data.data.map((value, i) => {
                            return {
                              id: value.townCode,
                              name: value.townName,
                            }
                          })
                        }
                      }
                    })
                  }
                })
              }
            })
          } else {
            this.$message.error(data.msg);
          }
        })
      }
    },

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值