我有一个看起来像这样的数组:
sales = [
[{'Year': 2018, 'Month': 01, 'Sale'; 512}, {'Year': 2018, 'Month': 02, 'Sale'; 1025}, ....],
[{'Year': 2017, 'Month': 01, 'Sale'; 155}, {'Year': 2017, 'Month': 02, 'Sale'; 12}, ....]
]
我想用 vue 在表格中显示它:
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>2018</th>
<th>2017</th>
</tr>
</thead>
<tbody>
<tr v-for="(sale,i) in sales" :key="i">
<th scope="row">{{ ??? }}</th> //Month
<td>{{ ??? }}</td> //currentYear.Sale
<td>{{ ??? }}</td> //previousYear.Sale
</tr>
</tbody>
</table>
不幸的是,我不知道如何遍历我的销售数组以在当前年份和上一年的每个表行中显示销售情况。
最佳答案
<div id="app">
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>2018</th>
<th>2017</th>
</tr>
</thead>
<tbody>
<tr v-for="(sale,i) in sales[0]" :key="i">
<th scope="row">{{ sale.Month }}</th>
<td>{{ sale.Sale }}</td>
<td>{{ sales[1][i].Sale }}</td>
</tr>
</tbody>
</table>
</div>
new Vue({
el: "#app",
data: {
sales: [
[{'Year': 2018, 'Month': 01, 'Sale': 512}, {'Year': 2018, 'Month': 02, 'Sale': 1025}],
[{'Year': 2017, 'Month': 01, 'Sale': 155}, {'Year': 2017, 'Month': 02, 'Sale': 12}]
]
}
})
关于javascript - 如何使用 v-for 使用 vue.js 渲染表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51963481/