I'm trying to update a list of products with an editable quantity, which updates and changes a row-wise total of product prices. Please see my code below -
<template>
<div>
<product-search-bar :product_search_route="product_search_route" />
<table class="table table-hover table-responsive table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Qty.</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr v-for="(product, index) in product_list">
<td>
{{ index + 1 }}
<input type="hidden" :name="'order_items[' + index + '][id]'" :value="product.id" />
</td>
<td>{{ product.name }}</td>
<td>
<input type="number" :name="'order_items[' + index + '][qty]'" @change="product_quantity_changed($event, product)" />
</td>
<td>{{ product.purchase_currency }} {{ product.total_price }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
name: 'form-purchase-order-items',
props: [
'product_search_route',
],
data() {
return {
product_list: []
};
},
mounted() {
},
methods: {
/**
*
* @param product
*/
product_added(product)
{
product.quantity = 1;
product.total_price = product.supplier.purchase_price;
if (!this.product_list.find((v) => { return v.id == product.id }))
this.product_list.push(product);
},
/**
*
* @param product
*/
product_quantity_changed(e, product)
{
var quantity = Number(e.target.value);
this.$set(product, 'quantity', quantity);
this.$set(product, 'total_price', (quantity * product.supplier.purchase_price));
}
},
watch: {
}
}
</script>
The price total does update correctly, seen through Vue DevTools, however, the column <td>{{ product.purchase_currency }} {{ product.total_price }}</td> doesn't reflect the changes made. I've read the documentation and I think this is something that isn't mentioned there.
Edit:
The two members quantity and total_price are being created after the object is received in the product_added(product) callback. This probably makes them non-reactive members of the object.
productinto @change, instead just pass theindexthen in method use$setonthis.product_list[index]and see if this works?inputevent it will be more informative.$setonly work on vue data ! Your product is not vue data ! It is temp array data @SagunKho