AreaSelect.vue 5.46 KB
<template>
	<div>
		<el-select v-model="provinceIndex" @change="changeProvince" placeholder="请选择" size="small" style="width:150px;" filterable>
	      	<el-option v-for="(item, index) in provinceOpts"
	      		:label="item.name" 
	      		:value="index"
	      		:key="index"
	      	></el-option>
		</el-select> 
		<el-select v-model="cityIndex" placeholder="请选择" size="small" style="width:150px;" filterable>
	      	<el-option v-for="(item, index) in cityOpts"
	      		:label="item.name" 
	      		:value="index"
	      		:key="index"
	      	></el-option>
		</el-select>
		<el-select v-model="areaId" @change="changeArea" placeholder="请选择" size="small" style="width:150px;" v-show="areaOpts.length" filterable>
	      	<el-option v-for="item in areaOpts"
	      		:label="item.name" 
	      		:value="item.id"
	      		:key="item.id"
	      	></el-option>
		</el-select>
	</div>
	
</template>
<script>
	//import areaData from './areaData'
	const areaData = ()=> import(/* webpackChunkName:"areaData" */ './areaData')
	
	export default{
		props: ['id', 'prependFirst'],
		data(){
			
			return {
				provinceIndex: undefined,
				newprovinceIndex:undefined,
				provinceOpts: [],
				cityIndex: undefined,
				cityOpts: [],
				areaId: undefined,
				areaOpts: [],
				areaIndex:undefined,
				region:undefined,
			}
		},
		mounted(){
			/*if(this.prependFirst){
				cityOpts.unshift({
					"id": undefined,
					"parentId": newVal,
					"type": 2,
					"name": '请选择'
				})
			}*/
		},
		created(){
			areaData().then((res)=>{
				let areaArr = JSON.parse(JSON.stringify(res.default));//避免污染对象数组里数据,因为是共用的数据,这里进行深拷贝
				if(this.prependFirst){
					areaArr.forEach((item)=>{
						item.city.unshift({
							"id": undefined,
							"parentId": undefined,
							"type": 2,
							"name": '请选择'
						});
						item.city.forEach((item)=>{
							if(item.area){
								item.area.unshift({
									"id": undefined,
									"parentId": undefined,
									"type": 3,
									"name": '请选择'
								});
							}
						});
					})
					
				}
				this.provinceOpts = areaArr;
				this.initArea(this.id)
				
			})
		},
		watch: {
			provinceIndex(newVal,oldVal){
				if(typeof newVal == 'undefined'){
					this.cityIndex = undefined;
					this.areaId = undefined;
				}else{
					if(this.provinceOpts[newVal]){
						let cityOpts = this.provinceOpts[newVal].city;
						
						this.cityOpts = cityOpts
						this.changeCity(newVal,oldVal)
					}
					
				}
				
			},
			cityIndex(newVal,oldVal){
				if(typeof newVal != 'undefined'){
					this.region =this.region != undefined?this.region:undefined
					this.changeCity(newVal,oldVal)
					this.region =undefined
				}
			},
			id(newVal){
				if(newVal){
					this.initArea(newVal)
				}else{
					this.provinceIndex = undefined;
				}
			}
		},
		methods: {
			initArea(id){
				if(id){
					for(let i=0; i<this.provinceOpts.length; i++){
						const provinceObj = this.provinceOpts[i];
						//console.log(id)
						if(provinceObj.id == id){
							this.provinceIndex = i;
							this.cityIndex = 0;
							break;
						}
						for(let j=0; j<provinceObj.city.length; j++){
							const cityObj = provinceObj.city[j];
							if(cityObj.id == id){
								this.provinceIndex = i;
								this.cityIndex = j;
								break;
							}else{
								if(cityObj.area){
									for(let k=0; k<cityObj.area.length; k++){
										const areaObj = cityObj.area[k];

										if(areaObj.id == id){
											this.provinceIndex = i;
											this.newprovinceIndex = i
											this.cityIndex = j;
											this.areaId = id;
											this.region = id;
											return;
										}
									}
								}
								
							}
						}
						
					}
				}
				
			},
			rest(){
				this.provinceIndex="";
				this.cityIndex="";
				this.areaOpts=[];
			},
			changeProvince(val){
				if(val != this.newprovinceIndex ){
					this.cityIndex = 0;
				}
			},
			changeCity(newVal,oldVal,change){
				const selectedProvince = this.provinceOpts[this.provinceIndex];
				const selectedCity = this.cityOpts[this.cityIndex];
				if(!selectedCity) return;
				this.areaOpts = selectedCity.area || [];

				if(selectedCity){
					if(selectedCity.id === undefined){
						const wholeName = selectedProvince.name;
						this.$emit('onChangArea', selectedProvince.id, wholeName)
					}else{
						if(this.areaOpts.length){
							this.areaId = this.region != undefined ? this.region:this.areaOpts[0].id;
							if(this.areaId){
								const selectedArea = this.areaOpts.find((item)=>item.id === this.areaId)
								this.$emit('onChangArea', this.areaId, selectedProvince.name + selectedCity.name + selectedArea.name)
							}else{
								this.$emit('onChangArea', selectedCity.id, selectedProvince.name + selectedCity.name)
							}
						}else{
							this.areaId = undefined;
							const wholeName = selectedProvince.name + selectedCity.name;
							this.$emit('onChangArea', this.cityOpts[this.cityIndex].id, wholeName)
						}
					}
				}
			},
			changeArea(){
				const findAreaItem = this.areaOpts.find((item)=>item.id == this.areaId);
				if(findAreaItem.id){
					const wholeName = this.provinceOpts[this.provinceIndex].name+this.cityOpts[this.cityIndex].name+findAreaItem.name;
					this.$emit('onChangArea', this.areaId, wholeName)
				}else{
					const wholeName = this.provinceOpts[this.provinceIndex].name+this.cityOpts[this.cityIndex].name;
					this.$emit('onChangArea', this.cityOpts[this.cityIndex].id, wholeName)
				}
				
			}
		}
	}
</script>