javascript - 如何使用 v-for 使用 vue.js 渲染表格

标签 javascript vuejs2

我有一个看起来像这样的数组:

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}]
    ]
  } 
})

示例 https://jsfiddle.net/mcqwtdgr/

关于javascript - 如何使用 v-for 使用 vue.js 渲染表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51963481/

相关文章:

javascript - 通过过滤器添加@click或路由器

javascript - 如果将正则表达式传递给 "RegExp"构造函数会发生什么?

javascript - 部署后 react 路由器 url 问题

javascript - 如何根据服务器端 JavaScript 确认返回停止点击事件?

eloquent - Vue 2 Laravel 5.3 Eloquent 无法从对象检索数据

javascript - Vue.js 中的动态样式

javascript - flask : How to pass JSON to javascript file?

javascript - 使用 jquery 在多选下拉列表中禁用 optgroup 中未选择的选项

javascript - Quasar 按钮组件中的图标不会改变大小

javascript - v-if 上平滑的 vue 折叠过渡