Your approach is strange.
You should have a structure like this one.
-
a batches component with an array of batches and a function to add a new batch add(newBatch)
-
batch component to display a batch and which accepts a batch props
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I declare an items object like so:
const newItem = {itemId: '', price: '0.00', discount: 0, discountedPrice: 0, batches: [2]}
const items = ref([{...newItem, ...{id: 0}}])
And I want to be able to push a new member to the batches key:
const addItem = () => {
items.value.push({...newItem, ...{id: items.value.length}})
}
const addBatch = (batches) => {
console.log(batches)
batches.push(0)
}
But when I call addBatch from the template, it is updating all of the batches on every item:
<tr v-for="(item, index) in items" :key="item.id" class="bg-slate-200">
<td class="p-1 text-center align-top">
<input :name="'itemId[' + index + ']'"
type="text"
placeholder="Item..."
v-model="item.itemId"
class="rounded border border-gray-500 text-gray-900 p-1 w-full text-xs min-w-[100px]"
>
</td>
<td class="p-1 text-center align-top">
<div class="grid grid-cols-2">
<div>
<input v-for="(batch, batchIndex) in item.batches"
:key="batchIndex + 'batch'"
:name="'quantity[' + index + '][]'"
:value="batch"
type="number"
placeholder="Quantity..."
min=".001"
max="1000000000"
step=".001"
class="rounded border border-gray-500 text-gray-900 p-1 text-xs w-full"
>
</div>
<div>
<button @click="addBatch(item.batches)" //////// HERE
type="button"
class="text-xs bg-green-600 flex p-1 items-center rounded text-white ml-1"
>
I've tried everything but I can't get my head around why all of the batches are being updated instead of just the one being passed to the function?
Please or to participate in this conversation.