【发布时间】:2018-06-05 16:49:50
【问题描述】:
我正在学习 vuejs 并正在实现一个报告系统,从模拟 API 中获取一些数据并将其输出到表格中。数据返回多年的月度订单号和值。数据示例如下:https://api.myjson.com/bins/u5gp6。
我想要做的是遍历每年和每月并输出订单数和订单值,以及每年的总和。
HTML 是这样的:
<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>
JS是这样的:
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 () {
}
}
});
可以在这里看到一个小提琴:https://jsfiddle.net/eywraw8t/63295/
我正在努力的部分是计算每年的 totalOrders 和 totalRevenue 值。
我尝试了各种方法,例如在计算的总函数中添加一个 reduce 函数,但就是无法工作。我想我对这是一个嵌套循环感到困惑。
谁能建议如何解决这个问题,以便正确填充 totalOrders 和 totalRevenue?
非常感谢。
【问题讨论】:
标签: vuejs2