There are some variants.
1st
You can add fields to your products array when you get it
script
this.products = someData;
this.products.forEach((pr) => {
pr.totalQuantities = pr.quantities.map((q)=>q.qty).reduce(function(total, q) {
return total + q}, 0);
})
template
<tr v-for="product in products">
<td>{{ product.name }}</td>
<td>
<span v-for="price in product.prices" v-html=" '₱ ' +
price.price + ', '"></span>
</td>
<td >
<span>{{product.totalQuantities}}</span>
</td>
</tr>
2nd
Add filter to count total
script (insert into vue instance)
filters: {
total: function (quantities) {
return quantities.map((q)=>q.qty).reduce(function(total, q) {
return total + q}, 0);
}
}
template
<tr v-for="product in products">
<td>{{ product.name }}</td>
<td>
<span v-for="price in product.prices" v-html=" '₱ ' +
price.price + ', '"></span>
</td>
<td >
<span>{{product.quantities | total}}</span>
</td>
</tr>
3rd
script
computed: {
quantities(){
return products.map(function(pr){
return pr.quantities.map((q)=>q.qty).reduce(function(total, q) {
return total + q}, 0);
});
}
}
template
<tr v-for="product, index in products">
<td>{{ product.name }}</td>
<td>
<span v-for="price in product.prices" v-html=" '₱ ' +
price.price + ', '"></span>
</td>
<td >
<span>{{ quantities[index] }}</span>
</td>
</tr>
variant for work with index
script
quantities(){
return (index) => {
return this.$store.getters.products[index].quantities.map((q)=>q.qty).reduce(function(total, q) { return total + q}, 0) }
}
}
template
{{quantities(index)}}
product.quantities.reduce((sum, product) => sum + product.qty, 0). You could also create a filter. See vuejs.org/v2/guide/computed.htmldonutData: [ { label: 'Openstaande verzoeken', value: 20 }, { label: 'Geaccepteerde verzoeken', value: 25 }, { label: 'Afgewezen verzoeken', value: 10 } ],My solution is really stupid. I'll post it here. I hope someone can refine the code. Sigh....