Select tag shows nothing selected when v-model variable has a value So I have a variable called client_id and has a value sent with a url parameter, and have a select like this:
<select @change ="getClientLists($event)" v-model="client_id" id="client-list" name="client_id" v-if="client_id" class="selectpicker h-100 form-control shadow-none @error ('client_id') is-invalid @enderror " data-live-search="true" required> <option v-for="client in clients" :value="client.id" :data-industry-id="client.industry_id" v-text="client.company_name"> </option>
the problem is that the select must show the v-model value as selected value, but sometimes it show "Nothing selected" instead
Maybe because the client_id value is not one of the ids in the array of clients objects?
@tykus It is, and sometimes it is selected as the page loads but sometimes it shows "Nothing selected"
@Sting is it a timing issue, where does the data come from?
Not sure if it will fix your case, but I don't see a key attribute on your option tag
@Ben Taylor Sadly, it didn't
Do you really want to fetch the client list every time you select a client? Looks to me as though you have a race condition. Why not populate the client list first. Then set the client Id for the select.
@Ben Taylor Even if I do populate the clients list first the issue persist.
I tried adding
:selected="client_id == client.id"
to the option tag and yet nothing changed
@Sting you shouldn't need to do that with the v-model
Have you cast the client_id to an integer?
The only other thing I can think of trying right now is changing the v-if to v-show
Where does the clients array come from; are they loading asynchronously? This is what I meant by timing issue - maybe the clients is not available whenever the component is mounted?
@tykus it looks to be loaded after it's mounted. Actually, it doesn't look to be a component at all, seeing as it has blade in it
@Ben Taylor It did have blade, and issue fixed thanks for your efforts.
Fixed with:
$('#client-list').selectpicker('val', _this.client_id);
in the getClientLists function
@Sting it might have been useful to know that you were using a third party library from the outset of this thread 🤦♂️
Please sign in or create an account to participate in this conversation.