element地址三级联动

本文介绍了一种在前端应用中集成区域数据选择器的方法,利用element-china-area-data组件实现省市区三级联动选择,同时提供了编码与文本之间的转换功能。
  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]]
    }
    
vue-element 省市区三级json数据, 及文字转换成拼音var PinYin = { "a": "\u554a\u963f\u9515", "ai": "\u57c3\u6328\u54ce\u5509\u54c0\u7691\u764c\u853c\u77ee\u827e\u788d\u7231\u9698\u8bf6\u6371\u55f3\u55cc\u5ad2\u7477\u66a7\u7839\u953f\u972d", "an": "\u978d\u6c28\u5b89\u4ffa\u6309\u6697\u5cb8\u80fa\u6848\u8c19\u57ef\u63de\u72b4\u5eb5\u6849\u94f5\u9e4c\u9878\u9eef", "ang": "\u80ae\u6602\u76ce", "ao": "\u51f9\u6556\u71ac\u7ff1\u8884\u50b2\u5965\u61ca\u6fb3\u5773\u62d7\u55f7\u5662\u5c99\u5ed2\u9068\u5aaa\u9a9c\u8071\u87af\u93ca\u9ccc\u93d6", "ba": "\u82ad\u634c\u6252\u53ed\u5427\u7b06\u516b\u75a4\u5df4\u62d4\u8dcb\u9776\u628a\u8019\u575d\u9738\u7f62\u7238\u8307\u83dd\u8406\u636d\u5c9c\u705e\u6777\u94af\u7c91\u9c85\u9b43", "bai": "\u767d\u67cf\u767e\u6446\u4f70\u8d25\u62dc\u7a17\u859c\u63b0\u97b4", "ban": "\u6591\u73ed\u642c\u6273\u822c\u9881\u677f\u7248\u626e\u62cc\u4f34\u74e3\u534a\u529e\u7eca\u962a\u5742\u8c73\u94a3\u7622\u764d\u8228", "bang": "\u90a6\u5e2e\u6886\u699c\u8180\u7ed1\u68d2\u78c5\u868c\u9551\u508d\u8c24\u84a1\u8783", "bao": "\u82de\u80de\u5305\u8912\u96f9\u4fdd\u5821\u9971\u5b9d\u62b1\u62a5\u66b4\u8c79\u9c8d\u7206\u52f9\u8446\u5b80\u5b62\u7172\u9e28\u8913\u8db5\u9f85", "bo": "\u5265\u8584\u73bb\u83e0\u64ad\u62e8\u94b5\u6ce2\u535a\u52c3\u640f\u94c2\u7b94\u4f2f\u5e1b\u8236\u8116\u818a\u6e24\u6cca\u9a73\u4eb3\u8543\u5575\u997d\u6a97\u64d8\u7934\u94b9\u9e41\u7c38\u8ddb", "bei": "\u676f\u7891\u60b2\u5351\u5317\u8f88\u80cc\u8d1d\u94a1\u500d\u72c8\u5907\u60eb\u7119\u88ab\u5b5b\u9642\u90b6\u57e4\u84d3\u5457\u602b\u6096\u789a\u9e4e\u8919\u943e",
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值