Reputation: 315
I have an array of items with amount and price (pre-tax). I would like to count net price and price per one item. I also have a filter that allows to display currency and would like to display that filter next to created computed property. Computed properties and displaying filter next to them don't work. What can I do to make them work? JSFiddle here
HTML:
<table>
<thead>
<tr class="table-head">
<th v-for="item in tableHead">{{ item.name }} <span>{{ item.desc }}</span></th>
</tr>
</thead>
<tbody>
<tr v-for="(element, index) in amount">
<td>{{ element.amount }}</td>
<td>
{{ priceNet }}
</td>
<td>
{{ element.price | curr }}
{{ pricePerItem }}
</td>
</tr>
</tbody>
</table>
Vue Js:
new Vue({
el: "#app",
data: {
tableHead: [
{ name: 'amount', desc: '' },
{ name: 'price', desc: '(net)' },
{ name: 'price', desc: '(pre-tax)' }
],
amount: [
{ amount: 100, price: 80.61 },
{ amount: 250, price: 72.10 },
{ amount: 500, price: 79.62 },
{ amount: 1000, price: 100.20 },
{ amount: 2500, price: 147.60 },
{ amount: 5000, price: 232.56 }
]
},
computed: {
priceNet() {
this.amount.forEach((element) => {
let net = (element.price / 1.23);
return net;
})
},
pricePerItem() {
this.amount.forEach((element) => {
let priceP = element.price / element.amount;
return priceP;
})
}
},
filters: {
curr: (value) => {
return `${value.toFixed(2)} euro`
}
}
})
Upvotes: 0
Views: 71
Reputation: 56753
Instead of computed
you want methods
:
methods: {
priceNet(price) {
return price / 1.23
},
pricePerItem(price, amount) {
return price / amount
}
},
then, in your html update the bindings:
<tr v-for="(element, index) in amount">
<td>{{ element.amount }}</td>
<td>{{ priceNet(element.price) | curr }}</td>
<td>
{{ element.price | curr }}
{{ pricePerItem(element.price, element.amount) | curr }}
</td>
</tr>
Updated fiddle:
https://jsfiddle.net/75Lk2tpe/1/
Upvotes: 2