new Vue({
el: '#app',
data: function() {
return {
countries: [],
cities: [],
selectedContinent: "",
selectedCountry: "",
selectedCity: ""
}
},
watch: {
selectedContinent: function() {
// Clear previously selected values
this.countries = [];
this.cities = [];
this.selectedCountry = "";
this.selectedCity = "";
// Populate list of countries in the second dropdown
if (this.selectedContinent.length > 0) {
this.countries = this.places[this.selectedContinent]
}
},
selectedCountry: function() {
// Clear previously selected values
this.cities = [];
this.selectedCity = "";
// Now we have a continent and country. Populate list of cities in the third dropdown
if (this.selectedCountry.length > 0) {
this.cities = this.places[this.selectedContinent][this.selectedCountry]
}
}
}
})
.dropdown {
margin: 10px 0;
padding: 10px 0;
border-bottom: 1px solid #DDD;
}
.dropdown span {
display:inline-block;
width: 80px;
}
<body>
<div id="app">
<div class="cascading-dropdown">
<div class="dropdown">
<span>Continent:</span>
<select v-model="selectedContinent">
<option value="">Select a Continent</option>
<option v-for="(country_obj, country) in places" :value="country">{{country}}</option>
</select>
</div>
<div class="dropdown">
<span>Country:</span>
<select :disabled="countries.length == 0" v-model="selectedCountry">
<option value="">Select a Country</option>
<option v-for="(city_obj, city) in countries">{{city}}</option>
</select>
</div>
<div class="dropdown">
<span>City:</span>
<select :disabled="cities.length == 0" v-model="selectedCity">
<option value="">Select a City</option>
<option v-for="city in cities">{{city}}</option>
</select>
</div>
</div>
</div>
</body>
Below is the REST API for countries, states and cities https://www.universal-tutorial.com/rest-apis/free-rest-api-for-country-state-city and I have generated a token. Generated Token : IIPwMXygilaKrQJfNLuj-LHZWn5kxBVZRLJ-KtU5jxRivdTBkHio6IOyF60CERyOPjo
Working codesandbox link:- https://jsfiddle.net/Dhanunjay/sj24kn0b/6/
With the list of items in data, i am able to load data in dropdown. But i am not sure how to load data using rest api in dropdown
Followed this documentation for Free REST API for countries, states and cities