element地址三级联动

  1. 首先引入相关组件
    import {regionData, CodeToText, TextToCode} from ‘element-china-area-data’

  2. 使用组件

    <el-cascader
    size=“large”
    :options=“sendaddresss”
    v-model=“sendOptions”
    @change=“handleChanges” style=“width:217px;” clearable>

    data() {
      return {
     		 datas:[],
     	 	sendaddresss: regionData,
       		CodeToText,
        	TextToCode,
      		sendareas: '',//详细地址
          	sendOptions: [],
          	sendreturnprovince:'',
          	sendreturncity:'',
          	sendreturnregion:'',
      }
    },
    methods: {
    	//通过下拉列表选择获取省市区
    	handleChanges(value) {
           	 this.sendreturnprovince = CodeToText[this.sendOptions[0]]
       		 if (CodeToText[this.sendOptions[1]] == '市辖区') {
         			 this.sendreturncity = this.sendreturnprovince
        	} else {
          		this.sendreturncity = CodeToText[this.sendOptions[1]]
       		 }
        	this.sendreturnregion = CodeToText[this.sendOptions[2]]
     	 },
    	//通过省市区获取对应编码
     	 convertTextToCode(provinceText, cityText, regionText) {
    			let code = ''
    			if (provinceText && this.TextToCode[provinceText]) {
      				const province = this.TextToCode[provinceText]
      				code += province.code + ', '
     				 if (cityText && province[cityText]) {
        				const city = province[cityText]
        				code += city.code + ', '
       					 if (regionText && city[regionText]) {
          						code += city[regionText].code
        				}
     				 }
    			}
    			return code
    	},
    
    },
    mounted(){
    	//通过this.datas获取地址赋值到页面
    	this.sendOptions[0] = (this.TextToCode[this.datas.AddresserProvince].code)
    	if (this.datas.AddresserProvince != this.datas.AddresserCity) {
    		this.datas.AddresserCity = this.datas.AddresserCity
    	} else {
    	 this.datas.AddresserCity = '市辖区'
    	}
    	this.sendOptions[1] = (this.TextToCode[this.datas.AddresserProvince][this.datas.AddresserCity].code)
    
    this.sendOptions[2] = (this.TextToCode[this.datas.AddresserProvince][this.datas.AddresserCity][this.datas.AddresserRegion].code)
    
    
    this.sendOptions = [this.ruleForm.sendOptions[0], this.ruleForm.sendOptions[1], this.ruleForm.sendOptions[2]]
    }
    
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值