I am new to VueJS and I just figure out how to populate Select Options box using v-for loop.
JavaScript
x
4
1
<select>
2
<option v-for="person in persons" :value="personid">{{ personname }}</option>
3
</select>
4
Here is the list I have.
JavaScript
1
6
1
"persons": {
2
"2": "Person1",
3
"3": "Person2",
4
"4": "Person3"
5
}
6
This is our desired output.
JavaScript
1
5
1
<select id="persons">
2
<option value="3">Person1</option>
3
<option value="4">Person2</option>
4
</select>
5
Advertisement
Answer
Luckily for you, Vue can loop through the properties in an Object as described in v-for with an Object.
I’ve also included a snippet below which should help you achieve what you want.
JavaScript
1
15
15
1
Vue.config.productionTip = false;
2
Vue.config.devtools = false;
3
4
new Vue({
5
el: "#app",
6
data: () => {
7
return {
8
persons: {
9
"2": "Person1",
10
"3": "Person2",
11
"4": "Person3"
12
}
13
}
14
}
15
});
JavaScript
1
6
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
2
<div id="app">
3
<select id="persons">
4
<option v-for="(name, id) in persons" :value="id">{{name}}</option>
5
</select>
6
</div>