I have been attempting to implement multi-select questions in Vue using v-for.
The Select menu and its options are populated via JSON data.
Unfortunately, I am facing difficulty in retrieving the selected results as expected.
Whenever I select an option from the 2nd menu, the selection in the 1st menu gets cleared, and vice versa for the 1st menu.
new Vue({
el: "#app",
data: {
selectedValue: [],
checklists: [
{
"id":1,
"heading":"Environment",
"deleted_at":null,
"created_at":null,
"updated_at":"2020-08-08T13:24:22.000000Z",
"checklist_value":[
{
"id":1,
"checklists_id":1,
"values":"Indoor",
"notes":null,
"deleted_at":null,
"created_at":"2020-08-08T13:03:50.000000Z",
"updated_at":"2020-08-08T13:03:50.000000Z"
},
{
"id":2,
"checklists_id":1,
"values":"Outdoor",
"notes":null,
"deleted_at":null,
"created_at":"2020-08-08T13:13:27.000000Z",
"updated_at":"2020-08-08T13:13:27.000000Z"
}
]
},
{
"id":2,
"heading":"Location of Camera's",
"deleted_at":null,
"created_at":"2020-08-08T11:41:31.000000Z",
"updated_at":"2020-08-08T13:27:59.000000Z",
"checklist_value":[
{
"id":3,
"checklists_id":2,
"values":"Parking",
"notes":null,
"deleted_at":null,
"created_at":"2020-08-08T13:28:45.000000Z",
"updated_at":"2020-08-08T13:28:45.000000Z"
},
{
"id":4,
"checklists_id":2,
"values":"Balcony",
"notes":null,
"deleted_at":null,
"created_at":"2020-08-08T13:28:53.000000Z",
"updated_at":"2020-08-08T13:28:53.000000Z"
}
]
}
]
},
methods: {
getSelectedID(event) {
console.log(this.selectedValue);
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div class="input-field col s12" v-for="(checklist, index) in checklists">
<select v-model="selectedValue" @change="getSelectedID($event)" multiple="multiple">
<option disabled>Choose options</option>
<option v-for="(value,index) in checklist.checklist_value" :value="value.id">{{value.values}}</option>
</select>
<label>{{checklist.heading}}</label>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script>
$(document).ready(function() {
$('select').formSelect();
});
</script>
I anticipate that v-model should create a new object for the select menu, map the options as an array, and allow me to pass it to the database for saving.
Your insights on where I may have gone wrong would be greatly appreciated.
Fiddle Link: https://jsfiddle.net/dvwkz15c/