Vue.js dynamic dropdown
18,340
Try this.
new Vue({
el: '#app',
data: {
filters: filters,
selectedValue: null
}
})
<div id="app">
<select v-model="selectedValue">
<option disabled value="">Please select one</option>
<option v-for="item in filters" :value="item">{{item}}</option>
</select>
</div>
Note: For future readers, there was also an issue where the normal delimiters for text interpolation needed to be customized in @captnvitman's evnironment.
Author by
captnvitman
Updated on July 24, 2022Comments
-
captnvitman almost 2 years
How would I make a dynamic dropdown in vue, not sure what I am doing wrong.
in my html I have...
<div id="app"> <select v-model="selected"> <option disabled value="">Please select one</option> <option v-for="item in selected"></option> </select>
and my js looks like....
new Vue({ el: '#app', data: { selected: ["Apache", "Cochise"], } })
EDIT: the values appear in the html DOM tree in the inspector
-
captnvitman about 7 yearsthat work mostly, except for the options do not show in the dropdown, I edited my question with pictures to show what I mean
-
Bert about 7 years@captnvitman where does
filters
come from? And can you link where you are seeing this? It works in the example. -
Bert about 7 years@captnvitman possibly a version issue? I am using Vue 2 in the example.
-
captnvitman about 7 yearsfrom an ajax call, however I changed it to filters: ['Apache','Cochise'] like in your example to keep it simple. I'll edit my question to reflect that
-
captnvitman about 7 yearsI switched to the same version to check, still the same problem. perhaps its another library im using like bootstrap that is messing with it
-
Bert about 7 years@captnvitman It looks like you forgot to add
{{item}}
inside the<option></option>
. If you look at your screenshot, there is avalue
but no text. -
captnvitman about 7 yearsi have it like this <option v-for="item in filters" :value="item">{{item}}</option> ... still no luck
-
Bert about 7 yearsLet us continue this discussion in chat.
-
captnvitman about 7 yearsfor anyone looking at this in the future with the same problem, it was a problem with the delimiters (maybe getting confused with the django templating?) set delimiters: ['${', '}'], in new Vue () and use ${} for vue templating. thanks to @Bert Evans