VechicleBrand.vue 3.25 KB
<template>	
	<el-form-item :label="label" :prop="prop">      
		<el-select v-model="brandId" @change="brandChange" filterable placeholder="请选择" size="mini" class="el-slect" :disabled="disabled">
		    <el-option
		      v-for="item in brandOpts"
		      :key="item.brandId"
		      :label="item.brandName"
		      :value="item.brandId">
		    </el-option>
		</el-select>
		<el-select v-model="typeId" @change="typeChange" placeholder="" size="mini" class="el-slect" :disabled="disabled">
		    <el-option
		      v-for="item in typeOpts"
		      :key="item.vehicleTypeId"
		      :label="item.vehicleTypeName"
		      :value="item.vehicleTypeId">
		    </el-option>
		</el-select>
		
	</el-form-item>
</template>
<script>
import {services} from '../api'
const COMMON_SERVICE = services.common;
export default{
	props:['label', 'prop', 'brandId', 'typeId', 'disabled', 'init'],
	data(){
		return {
			//brandId: this.initBrandId?this.initBrandId:'',
			brandOpts: [],
			//typeId: this.initTypeId?this.initTypeId:'',
			typeOpts: [],
			bid: this.brandId,
			tid: this.typeId,
			_promise: null
		}
	},
	computed: {
		areaStr:{
			get(){
				return this.province+this.city+this.area;
			}
		}
	},
	created(){
		this._promise = new Promise((resolve, reject)=>{
			COMMON_SERVICE.getAllVehicleBrand({}).then((result)=>{
				if(result.returnCode == 0){
					this.brandOpts = result.data;
					resolve()
				}
			})
		})
	},
	watch: {
		/* brandId(newVal, oldValue){console.log('watch brandId')
			this._promise.then(()=>{
				this.bid = newVal
				if(this.init){
					
					if(newVal && newVal!=oldValue){
						if(this.brandOpts.length){
							this.ing = true
							COMMON_SERVICE.getBrandTypeByBrandId({vehicleBrandId: newVal}).then((result)=>{
								if(result.returnCode == '0'){
									this.typeOpts = result.data;
								}
							});
						}
					}
				}
			})
			
		},
		typeId(newVal){console.log('watch typeId')

			if(newVal){
				this.tid = parseInt(newVal)
			}else{
				this.tid = ''
				this.typeOpts = []
			}
			
		} */
		init(newVal){
			if(newVal === true){
				this.$emit('onBrandInitChange')
				this._promise.then(()=>{
					COMMON_SERVICE.getBrandTypeByBrandId({vehicleBrandId: this.brandId}).then((result)=>{
						if(result.returnCode == '0'){
							this.typeOpts = result.data;
							
						}
					});
				})
			}
		}
	},
	methods: {
		brandChange(){
			if(this.brandOpts.length){
				this.$emit('onBrandInitChange')
				COMMON_SERVICE.getBrandTypeByBrandId({vehicleBrandId: this.brandId}).then((result)=>{
					if(result.returnCode == '0'){
						this.typeOpts = result.data;
						
						this.typeId = this.typeOpts[0].vehicleTypeId;

						this.brandName = this.brandOpts.find(item => item.brandId === this.brandId).brandName
						this.typeName = this.typeOpts.find(item => item.vehicleTypeId === this.typeId).vehicleTypeName
						
						this.$emit('onBrandChange', this.brandId, this.typeId, this.brandName, this.typeName)
					}
				});
			}
			
		},
		typeChange(){

			this.brandName = this.brandOpts.find(item => item.brandId === this.brandId).brandName
			this.typeName = this.typeOpts.find(item => item.vehicleTypeId === this.typeId).vehicleTypeName
			
			this.$emit('onBrandChange', this.brandId, this.typeId, this.brandName, this.typeName)

		}
	}
}
</script>

<style>

</style>