I'm starting with Vue.js and I don't know how can I calculate the partial value and total value inside a v-for display.
I'm getting some info from a JSON with the next structure:
[saldos]
[bank]
[data]
[account]
- name
- balance
[account]
- name
- balance
[meta]
- info
[bank]
[data]
[account]
- name
- balance
[account]
- name
- balance
[meta]
- info
Each bank could be 0 accounts, 1 account or more accounts.
I need to get the partial value of each bank (it is the sum of all accounts 'balance' inside the same bank) and the total value (it is the sum of all partial values previously calculated for each bank)
My Vue.js file is:
var saldo = new Vue({
el: "#listasaldos",
data: {
saldos:[],
},
created: function(){
console.log("Cargando ...");
this.get_saldos();
},
methods:{
get_saldos: function(){
fetch("./api.php?action=saldosapi")
.then(response=>response.json())
.then(json=>{this.saldos=json.saldos})
}
}
});
And my HTML file is:
<div id="listasaldos">
<h1>Title</h1>
<h2>{{totalValue}}</h2>
<div v-for="bank in saldos">
<h3>{{partialValue}}</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody v-for="account in bank.data">
<tr> {{account.name}}</tr>
<tr> {{account.balance}}</tr>
</tbody>
</table>
</div>
</div>
How can I do it?
Thanks!