AreaSelect.vue 4.24 KB
<template>	
	<el-form-item :label="label" :prop="prop">	                
		<el-select v-model="provinceId" @change="povinceChange" filterable placeholder="请选择" size="small" class="el-slect" :disabled="disabled">
		    <el-option
		      v-for="item in provinceOpts"
		      :key="item.id"
		      :label="item.name"
		      :value="item.id">
		    </el-option>
		</el-select>
		<el-select v-model="cityId" @change="cityChange" v-show="!isHideCity" placeholder="" size="small" class="el-slect" :disabled="disabled">
		    <el-option
		      v-for="item in cityOpts"
		      :key="item.id"
		      :label="item.name"
		      :value="item.id">
		    </el-option>
		</el-select>
		<el-select v-model="areaId" @change="areaChange" v-show="!isHideArea" placeholder="" size="small" class="el-slect" :disabled="disabled">
		    <el-option
		      v-for="item in areaOpts"
		      :key="item.id"
		      :label="item.name"
		      :value="item.id">
		    </el-option>
		</el-select>
	</el-form-item>
</template>
<script>
import chinaData from './china'
export default{
	props:['label', 'prop', 'id', 'init', 'disabled'],
	data(){
		const provinceOptions= chinaData.province;
		const slectedProvince = provinceOptions[0];

		return {
			province: '',
			provinceId: '',
			provinceOpts: provinceOptions,
			city: '',
			cityId: '',
			cityOpts: [],
			isHideCity: true,
			isHideArea: true,
			area: '',
			areaId: '',
			areaOpts: []
		}
	},
	computed: {
		areaStr:{
			get(){
				return this.province+this.city+this.area;
			}
		},
		areaStrSplit:{
			get(){
				let str = this.province
				if(this.city){
					str += `,${this.city}`
				}
				if(this.area){
					str += `,${this.area}`
				}
				return str
			}
		},
	},
	watch: {
		id(newVal){
			if(this.init){
				if(newVal){
					const s1 = newVal.substr(0,2)+'0000';
					const s2 = newVal.substr(0,4)+'00';

					this.provinceId = s1;
					this.povinceChange(this.provinceId)
					this.cityId = s2;
					this.pickCity(this.cityId);
					this.areaId = newVal;
					
				}else{
					this.provinceId = '';
					this.cityId = '';
					this.areaId = '';
					this.isHideCity = true;
					this.isHideArea = true;
				}
				this.$emit('areaChangeInit')
				
			}
		}
	},
	methods: {
		povinceChange(provinceId){
			this.provinceId = provinceId;
			const cityOptions = chinaData.city[provinceId];

	   		if(cityOptions){
	   			
	   			const selectedCity = cityOptions[0];
	   			this.province = selectedCity.province;
		   		this.cityId = selectedCity.id;
		   		this.city = selectedCity.name;
		   		this.cityOpts = cityOptions;
	   			this.cityChange();
	   			if(selectedCity.name == '市辖区'){
	   				this.isHideCity = true;
	   			}else{
	   				this.isHideCity = false;
	   			}
	   		}else{
	   			this.isHideCity = true;
	   			this.isHideArea = true;
	   			const provinceItem = chinaData.province.find((item) => item.id === provinceId);
	   			this.province = provinceItem.name;
	   			this.city = '';
	   			this.area = '';
	   			this.$emit('onAreaSelectChange', this.areaStr, this.provinceId, this.areaStrSplit)
	   		}
	   		
		},
		pickCity(cityId){
			this.cityId = cityId
			const areaOptions = chinaData.area[cityId];

	   		
	   		if(areaOptions.length){
	   			if (areaOptions[0].name == '市辖区') {
		   			areaOptions.splice(0, 1);
		   		}
	   			this.areaOpts = areaOptions;
	   			const selectedArea = areaOptions[0];
	   			this.city = selectedArea.city;
		   		this.areaId = selectedArea.id;
		   		this.area = selectedArea.name;
		   		this.isHideArea = false;
		   		this.$emit('onAreaSelectChange', this.areaStr, this.areaId, this.areaStrSplit)
	   		}else{
	   			this.isHideArea = true;
	   			const cityItem = chinaData.city[this.provinceId].find((item) => item.id === cityId);
	   			this.city = cityItem.name;
	   			this.area = '';
	   			this.areaId = '';
	   			this.$emit('onAreaSelectChange', this.areaStr, this.cityId, this.areaStrSplit)
	   		}
		},
		cityChange(){
			this.pickCity(this.cityId)
		},
		areaChange(){
			const areaOptions = chinaData.area[this.cityId];
			const selectedArea = areaOptions.find((o)=>{
				return o.id == this.areaId
			});
			this.area = selectedArea.name;
			this.$emit('onAreaSelectChange', this.areaStr, this.areaId, this.areaStrSplit)
		}
	}
}
</script>

<style scoped>
.el-select{
	width: 120px;
}
</style>