Hi! I’m not getting how checkboxes should be handled with v-model
dynamically. I have to define the object as a local array for them to bind correctly.
I won’t know how many entries I’ll receive, so the goal is to create a nested array for each set of checkboxes.
Here’s a link to a sandbox showing the problem: vue-checkbox-problem - CodeSandbox
Here’s the same code:
<template>
<div id="app">
{{ filter }}
<div v-for="item in list" :key="Object.keys(item)[0]">
<h2>{{ Object.keys(item)[0] }}</h2>
<div v-for="option in Object.values(item)[0]" :key="option._id">
<input
@change="log"
type="checkbox"
name=""
:id="option.name"
:value="option.name"
v-model="filter[Object.keys(item)[0]]"
/>
{{ option.name }} ({{ Object.keys(item)[0] }})
</div>
</div>
</div>
</template>
<script>
const api_response = require("@/assets/dummy-data.json");
export default {
name: "App",
components: {},
data() {
return {
list: api_response,
filter: {
list_one: [],
},
};
},
methods: {
log() {
console.log("log", this.filter);
},
},
mounted() {
console.log(this.list, Object.entries(this.list));
},
};
</script>
<style>
#app {
}
</style>
dummy-data.json:
[
{
"list_one": [
{
"_id": "62da46072b8ad1f9dcb8733b",
"name": "Miller Brennan"
},
{
"_id": "62da460771b37c9f7f942011",
"name": "Moses Houston"
},
{
"_id": "62da460739d2512479611349",
"name": "Ware Cox"
}
]
},
{
"list_two": [
{
"_id": "62da46477af2976444c052a0",
"name": "Simmons Montgomery"
},
{
"_id": "62da4647120f84ffda17785f",
"name": "Barrera Pickett"
},
{
"_id": "62da464771d327f305eadb09",
"name": "Sheryl Frost"
}
]
}
]