vue elemengUI 省市区三级联动

(1)template中

          <el-form-item label="公司注册地址">
            <el-select v-model="ruleForm.province_id" style="width:150px;margin: 0px 10px 0 0;" @change="changeCity(1)">
              <el-option v-for="(province,index) in provinceList" :key="index" :label="province.short_name" :value="province.area_id"></el-option>
            </el-select>  省
            <el-select v-model="ruleForm.city_id" style="width:150px;margin: 0px 10px;" @change="changeCity(2)">
              <el-option v-for="(city,index) in cityList" :key="index" :label="city.short_name" :value="city.area_id"></el-option>
            </el-select>  市
            <el-select v-model="ruleForm.country_id" style="width:150px;margin: 0px 10px;">
              <el-option v-for="(country,index) in countryList" :key="index" :label="country.short_name" :value="country.area_id"></el-option>
            </el-select>  区
            <el-input v-model="ruleForm.address" style="width:240px;margin: 0px 10px;" placeholder="请输入详细地址"></el-input>
          </el-form-item>

(2)data中

    ruleForm:{
        province_id: '',
        city_id: '',
        country_id: '',
        address: ''
    },
     // 省
     provinceList: [],
     // 市
     cityList: [],
     // 区县
     countryList: []

(3)methods中

            // 在获取编辑信息接口中:
             const sanObj = {
                sid: this.ruleFormData.province_id,
                shid: this.ruleFormData.city_id,
                cid: this.ruleFormData.country_id
              }
             // console.log(sanObj, 'sanObj')
              this.editShou(sanObj)
              this.ruleForm.address = this.ruleFormData.address
    // 地区选择
    changeCity(type) {
      switch (type) {
        case 1:
          this.getCity(type, this.ruleForm.province_id)
          break
        case 2:
          this.getCity(type, this.ruleForm.city_id)
          break
      }
    },
    // 获取地区列表
    getCity(type, pid) {
      const data = {
        pid: pid
      }
      console.log(data, 'aaa')
      getCities(data)
        .then(res => {
          if (res.data && res.status === 200) {
            // console.log(res)
            switch (type) {
              case 0:
                this.provinceList = res.data
                this.ruleForm.city_id = ''
                this.ruleForm.country_id = ''
                this.cityList = []
                this.countryList = []
                break
              case 1:
                this.cityList = res.data
                this.ruleForm.city_id = ''
                this.ruleForm.country_id = ''
                this.countryList = []
                break
              case 2:
                this.ruleForm.country_id = ''
                this.countryList = res.data
                break
            }
          }
        })
        .catch(err => {
          console.log(err)
        })
    },   
    getData(sid, callback) {
      const data = {
        pid: sid
      }
      getCities(data)
        .then(res => {
          if (res.data && res.status === 200) {
            // this.cityList = res.data
            callback(res)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    async editShou(sanObj) {
      const _this = this
      await _this.getData(100000, function(res) {
        _this.provinceList = res.data
        _this.ruleForm.province_id = sanObj.sid
        // setTimeout(() => {
        // }, 300)
      })
      await _this.getData(sanObj.sid, function(res) {
        _this.cityList = res.data
        _this.ruleForm.city_id = sanObj.shid
      })
      await _this.getData(sanObj.shid, function(res) {
        _this.countryList = res.data
        _this.ruleForm.country_id = sanObj.cid
      })
    },

(4)created中

this.getCity(0, 100000)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值