I'm cloning an array and using map to add a property to each object in the array. For some reason when I do this the added property is not being updated when used in v-model. Any ideas why this is happening?
In the below code typing in the text field does not update the item quantity. How can the code be altered so that it does?
<template>
<v-data-table
:headers="headers"
:items="items"
>
<template v-slot:item.quantity="props">
<v-text-field v-model="props.item.quantity"></v-text-field>
</template>
</v-data-table>
</template>
<script>
export default {
props: {
customer: {
type: Object,
required: true
}
},
data: () => ({
headers: [
{ text: 'ID', value: 'id' },
{ text: 'Description', value: 'description' },
{ text: 'Quantity', value: 'quantity' },
],
items: []
}),
created() {
this.initialize()
},
methods: {
initialize() {
const items = [...this.customer.items]
items.map(item => {
item.quantity = ''
})
this.items = items
}
}
</script>
Customer is a JSON response from API
{
"id": 1,
"name": "Customer",
"items": [
{
"id": 1,
"product": 1707,
"contract": null,
"plu": "709000",
"description": "ABC",
"unit": 1,
"price": "5.20"
}
],
}
Update
Link to codepen - typing in the qty2 field does not update the data because it was added to the object with map.
v-slot:item.quantity="{ item }"v-model="item.quantity". - azeós