I'm learning vuejs and am implementing a report system, taking some data from a mock API and outputting it into a table. The data returns monthly order numbers and values for multiple years. An example of the data is here: https://api.myjson.com/bins/u5gp6.
What I want to do is loop through each year and month and output the number of orders and order values, with a sum per year.
The HTML is like this:
<div id="app">
<div v-for="report in reports">
<h2>{{ report.year }}</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Month</th>
<th>Number of orders</th>
<th>Total revenue</th>
<th>Average order</th>
</tr>
</thead>
<tbody>
<tr v-for="value in report.values">
<td>{{ value.month }} {{ value.year }}</td>
<td>{{ value.orders }}</td>
<td>£{{ value.revenue }}</td>
<td>£{{ value.average }}</td>
</tr>
</tbody>
<tfoot v-if="reports">
<tr>
<td>Total {{report.year }}</td>
<td>{{ totalOrders }}</td>
<td>£{{ totalRevenue }}</td>
<td></td>
</tr>
</tfoot>
</table>
</div>
</div>
The JS is like this:
const app = new Vue({
el: '#app',
data: {
reports: []
},
created() {
fetch('https://api.myjson.com/bins/u5gp6')
.then(response => response.json())
.then(json => {
this.reports = json.reports
});
},
computed: {
totalOrders: function () {
},
totalRevenue: function () {
}
}
});
A fiddle can be seen here: https://jsfiddle.net/eywraw8t/63295/
The part I'm struggling with is calculating the totalOrders and totalRevenue values for each year.
I've tried various things like adding a reduce function in the computed total functions but just can't get anything to work. I think I'm getting confused by the fact this is a nested loop.
Can anyone suggest how to approach this such that totalOrders and totalRevenue are populated correctly?
Many thanks.