I'm dealing with a problem I can't figure out where I have an array of items that should be rendered in a component but inside that component they can be manipulated into a new array, so whenever a change is made into one of the items it should be pushed into the itemsToEdit array, instead of modifying the original item because later I need to send that new array to the server with only items modified and only the fields modified...
My child component has a simple checkbox (that is working the way it should) with a checked property which shows the default value if given, and a v-model with all the logic that actually works.
If I set up the v-model to v-model="item.show" it changes the original item, so there's nothing to change in there, but I can't send from parent to children the itemsToEdit array because it is empty and v-model="items.id.show" won't work.
I've worked with multiple checkboxes and an array v-model but it is a different workflow because I actually edit the original array of items, so it will push/remove items as I check the checkboxes but that's not what I want, the original array should stay as it is all the time.
Here's my simplified code, the children actually has a lot of checkboxes but I'll show just one because simplicity.
Parent component
<template>
<div>
<TestComponent v-for="i in items" :key="i.id" :item="i" :items-to-edit="itemsToEdit"/>
</div>
</template>
<script>
import TestComponent from '@/TestComponent'
export default {
name: 'MyParent',
components: { TestComponent },
data () {
return {
items: [
{ id: 1, name: 'test', show: false },
{ id: 2, name: 'test 2', show: false },
{ id: 3, name: 'test 3', show: true },
{ id: 4, name: 'test 4', show: false }
],
itemsToEdit: []
}
}
}
</script>
Child component
<template>
<tr>
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>
<MyCheckbox :checked="item.show"/>
</td>
</tr>
</template>
<script>
export default {
name: 'TestComponent',
components: { MyCheckbox },
props: ['item', 'itemsToEdit']
}
</script>
EDIT: One thing I forgot, I obviously can use $emit and listen on parent then push into the array, but that's not what I want, I am looking for a better way to implement this, if I have no other option, I will go with the events.
EDIT2: I can't 'clone' original array into the itemsToEdit because I want that array to be only filled up whenever a real change comes in, because later, the request send to server will only contain real changes, if I send the whole array of id's it will try to modify them even if they have no changes so it will be a waste of performance checking everything serverside.
currentItems, and make its initial value a copy of the original array. Then, on your code, mutatecurrentItemsinstead ofitems$emitapproach?v-modelapproach (which of course may or not may be the best solution) I'm just curious about how other people would approach this without$emits. @Ayrton oh yeah, didn't get it at all, but it will have the same effect as using the original one, right? I'll have items without changes inside that array?