Skip to content

Instantly share code, notes, and snippets.

@MicroDreamIT
Created November 9, 2018 17:59
Show Gist options
  • Save MicroDreamIT/ece9d8686496f8ea42b2f07b8a948a33 to your computer and use it in GitHub Desktop.
Save MicroDreamIT/ece9d8686496f8ea42b2f07b8a948a33 to your computer and use it in GitHub Desktop.
addressInput
export default {
props:{
address:{
type: Object,
default: ()=>{
return {}
}
},
countries:{
type: Array,
default:()=>{
return []
}
}
},
data(){
return {
countryList:this.countries,
states:[],
cities:[],
}
},
computed:{
checkPropCountry(){
if(this.countries.length){
this.countryList = this.countries
return !! this.countryList.length
}
},
triggerCountryAxio(){
if(!this.countries.length){
this.getCountry()
return true
}
}
},
mounted(){
if(this.checkPropCountry){
}
},
methods:{
getCountry(){
axios.get('/ajax/country')
.then(res=>{
this.countryList = res.data
})
.catch(err=>{
alert(err.response.message)
})
},
getState(){
if(this.address.country && this.address.country.hasOwnProperty('id')){
axios.get('/ajax/getStateByCountry',
{params:{
country_id:this.address.country.id
}})
.then(res=>{
this.states=res.data
this.cities= []
this.address.city = null
})
.catch(err=>{
})
}else{
this.address.city=null
this.address.state=null
this.states=[]
this.cities=[]
}
},
getCity(){
if(this.address.state && this.address.state.hasOwnProperty('id')){
axios.get('/ajax/getCityByState',
{params:{
state_id:this.address.state.id
}})
.then(res=>{
this.cities=res.data
})
}else{
this.address.city=null
this.cities=[]
}
}
}
}
<template>
<div v-if="checkPropCountry || triggerCountryAxio">
<v-select
name="country"
v-model="address.country"
label="name"
:options="this.countryList"
@input="getState"
class="form-control"
@clearable="getState"
></v-select>
<v-select
name="state"
v-model="address.state"
label="name"
:options="this.states"
class="form-control"
@input="getCity"
@clearable="getCity"
></v-select>
<v-select
name="city"
v-model="address.city"
label="name"
:options="this.cities"
class="form-control"
></v-select>
</div>
</template>
<script src="./addressInput.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment