I create a drop-down menu with v-repeat
in Vue.js, and I have a current value where I need to add the selected
property to the select
tag. It seems that v-if
can only be used to control tags, but not properties.
<select name="flavor">
<option value=""></option>
<option v-repeat="proposed_value: proposed_values" value="{{ proposed_value }}">{{ proposed_value }}</option>
</select>
I would need something like this:
<option v-repeat="proposed_value: proposed_values"
value="{{ proposed_value }}"
{{ proposed_value == current_value ? 'selected' }}
>
{{ proposed_value }}
</option>
This is the object used to create the drop-down:
{
"name": "flavors",
"current_value": "strawberry",
"proposed_values": [
"vanilla",
"strawberry",
"lemon"
]
}
Is there a way to do this that does not force me to monkey with the object like this?
{
"name": "flavors",
"proposed_values": [
{"name": "vanilla", "selected": ""}
{"name": "strawberry", "selected": "selected"}
{"name": "lemon", "selected": ""}
]
}
I am using Vue 0.11.10.
To apply a dynamic list and apply a choice:
The HTML
selected=selected
orselected
shorthand is written only if theitem
matched theselected
In Vue 2 it seems you can't set the
:selected="option == test"
as you would expect. I believe this is due to the nature ofv-model
usage. You need to expressly set your boundv-model="model.selected"
to the option you expect to be pre-selected. You could do that in your mounted callback:In this example my option values include
"my default"
.Ran into this issue myself. Solved it with this.
In Vue 1.0 and later you can just do this
and
So what will be the selected value in data it will be auto selected in dropdown as we set the model. I don't know if it works in older version as I have started from 1.0
note: Vue.js is awesome :)
I think you could do something like that: