ajax - How set the selected option on a dynamic loaded secondary select with VueJS? -
i have city select:
<select v-model="city"> <option value="" selected>cidade aonde vocÊ estuda</option> <option value="city1">city 1</option> <option value="city2">city 2</option> <option value="cityx">city x</option> </select>
and a school select:
<select v-model="school"> <option v-for="item in schools" v-bind:value="item.name" v-bind:selected="school == item.name" > @{{ item.name }} </option> </select>
this data (filled laravel blade view), may or may not come city , school:
data: { ... city: '{{ input::old('city') }}', school: '{{ input::old('school') }}', schools: [], },
i have watch on city:
watch: { 'city': '__citychanged', },
this event handler:
__citychanged: function (event) { this.school = ''; this.__fetchschools(); },
and fetcher:
__fetchschools: function (event) { if (this.city) { this.$http.get('/schools/' + this.city, function (schools) { this.schools = schools; }); } },
almost works fine, problem is, when form reloaded (or redirected on failed validation) , city , school selected, call __fetchschools
, fill schools select correctly, not set school selected option, user sees school unselected.
is there way make work using vue?
problem in __citychanged
handler, fix:
__citychanged: function (event) { var city = '{{ input::old('city') ?: (isset($subscription) ? $subscription->city : '') }}'; if (this.city !== city) { this.school = ''; } this.__fetchschools(); },