Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sting's avatar
Level 1

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

0 likes
17 replies
tykus's avatar

Maybe because the client_id value is not one of the ids in the array of clients objects?

Sting's avatar
Level 1

@tykus It is, and sometimes it is selected as the page loads but sometimes it shows "Nothing selected"

tykus's avatar

@Sting is it a timing issue, where does the data come from?

Ben Taylor's avatar

Not sure if it will fix your case, but I don't see a key attribute on your option tag

Ben Taylor's avatar

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.

Sting's avatar
Level 1

@Ben Taylor Even if I do populate the clients list first the issue persist.

Sting's avatar
Level 1

I tried adding :selected="client_id == client.id" to the option tag and yet nothing changed

Ben Taylor's avatar

The only other thing I can think of trying right now is changing the v-if to v-show

tykus's avatar

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?

Ben Taylor's avatar

@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

Sting's avatar
Level 1

@Ben Taylor It did have blade, and issue fixed thanks for your efforts.

Sting's avatar
Sting
OP
Best Answer
Level 1

Fixed with:

$('#client-list').selectpicker('val', _this.client_id);

in the getClientLists function

tykus's avatar

@Sting it might have been useful to know that you were using a third party library from the outset of this thread 🤦‍♂️

Please or to participate in this conversation.