@loriendarenya You call an array colors in your v-for but don't seem to define it. Maybe looping through markingColors in place of colors was what you intended doing
Check checkboxes using database information and Vue?
Alright, here we go!
I am developing a site with Vue and Laravel. I'm very new to Vue and am self-taught. I am looking for help with the following scenario:
Markings are associated with specific colors via a color_marking table. When querying the database while an edit form is active, I am retrieving the marking itself, as an Array in the Vue props section of my component, and the marking colors associated with it as a relationship using with(). It looks like this:
$marking = Marking::with('colors')->find(1);
I know, of course, how to retrieve the colors while in a Laravel blade file. However, these colors have to change dynamically based on the marking type and be checked at the outset if they are part of the relationship. My Vue looks similar to this (shortened for the sake of the reader's sanity):
<template>
<div>
<div v-for="(color, index) in colors" :key="index">
<label>
<input type="checkbox" :value="{{ color.id }}" v-model="markingColors" />
{{ color.name }}
</label>
</div>
</template>
<script>
import MarkingApi from "../apis/MarkingApi.js";
export default {
name: 'marking-edit',
props: {
marking: {
type: Object,
required: true
}
},
data: {
markingColors: this.marking.colors;
selectedMarkingColors: [],
}
}
}
</script>
Now, I'm looking for the following to happen:
The marking data is pulled up in the form. This happens just find with my extended code. The marking colors (all possible ones), are pulled from the database and listed. Again, no problem here. The marking colors are checked based on this.marking.colors (my relationship). This does not happen unless I specify the marking colors in an array like [40, 41, 42]. I know from endless tinkering that the this.marking.colors is being returned as an object.
How in the world can I ensure that the selected marking colors are checked upon render, however?
Thank you so much!
I would personally make markingColors into an array of objects that looked something like this:
{
id: { the color id},
name: { the color name},
selected: { set this to true or false at the start depending if the color id is in this.marking.colors }
}
You would set this when you load colors. Have it as an empty array to begin. It's a little hard to tell you specifically how to do that since I have no idea what this.marking.colors returns. I'm assuming its an array of objects?
So maybe something like this.
this.markingColors = colors.map(color => ({
id: color.id,
name: color.name,
selected: this.marking.colors.some(mc => mc.id === color.id)
}))
Then in your checkbox loop, i'd change it to something more like this:
<div v-for="(color) in markingColors" :key="color.id">
<label>
<input type="checkbox" v-model="color.selected" />
{{ color.name }}
</label>
</div>
You can then get rid of selectedMarkingColors from data, or make it into a computed property that returns markingColors filtered by the selected property.
computed: {
selectedMarkingColors() {
return this.markingColors.filter(c => c.selected)
}
}
Please or to participate in this conversation.