I am trying to set a selected value for an object option inside a select if this ID matched another object. I have a Company sectors array of objects that come from an API and a company that has a sector, coming from another API call. This way, if the sector_id field coming from the Company's API call matched the ID of the Array of sectors coming from another API, then this item should have the select "check". For some reason the way i am doing is not working. Does someone know why?
This is my code:
<select v-model="company.descripion" name="role">
<option v-for="company_sector in this.company_sectors" :key="company_sector.id" :company_sector.id="company_sector"
v-bind:select="company_sector.id == company.sector_id">
{{company_sector.name}}
</option>
</select>
This for example, a piece of what comes from the Company API. No problem with that, the Json returned works fine:
"company": {
"id": 8,
"sector_id": 19,
"sector": {
"id": 19,
"name": "Consultancy/ Services"
},
and this is a piece of what comes from the company_sectors API, also working great
[
{
"id": 19,
"name": "Consulting"
},
...then many other elements...
]
Instead of displaying the Consulting name as the option selected on the list, the first element of the company_sectors list if displayed as selected, as by default.
Any ideas on . what is going on?
v-bind:selectis supposed to be. Were you aiming for the standard attributeselected? For a Vue<select>you control the current selection via thev-model. Further,descripionappears to be spelt incorrectly and it is unclear what you're trying to achieve with the attribute:company_sector.id="company_sector". You may find the documentation on binding values to options useful: vuejs.org/v2/guide/forms.html#Select-Options