Vue:在表格中显示嵌套数据

K G*_*oll 7 handlebars.js vue.js vue-component vuejs2

我正在重写一些旧代码,并使用 Vue 作为替代品。除了更改使用把手模板化的一张桌子之外,一切都很棒。

使用嵌套的把手{{each}}我可以通过嵌套的数据结构进行工作,同时在表格行中显示相关数据:例如使用把手:https : //jsfiddle.net/6dsruo40/1/

我不知道如何使用 Vuev-for等来做同样的事情。

像我一样摆弄:https : //jsfiddle.net/cj7go6bv/

我不知道如何在保持<tr>s 的同时处理数据结构,就像在车把中一样

这是我正在使用的数据结构,但它很灵活:

var data = [
  {
key: "Region 1",
values: [
  {
    key: "Sub region 1",
    values: [
      {
        site: "Site A",
        timestamp: 1507246300935,
        total: 3,
      },
      {
        site: "Site B",
        timestamp: 1507246300818,
        total: 0,
      },
      {
        site: "Site C",
        timestamp: 1507246300936,
        total: 0,
      }
    ],
  },
  {
    key: "Sub region 2",
    values: [
      {
        site: "Site A",
        timestamp: 1507246301442,
        total: 20,
      },
      {
        site: "Site B",
        timestamp: 1507246301788,
        total: 5,
      }
    ]
   },
  {
    key: "Sub region 3",
    values: [
      {
        site: "Site A",
        timestamp: 1507246302503,
        total: 66,
      },
      {
        site: "Site B",
        timestamp: 1507246302783,
        total: 2
      }
    ]
  }
]
   },
   {
key: "Region 2",
values: [
  {
    key: "Sub region 1",
    values: [
      {
        site: "Site A",
        timestamp: 1507246306789,
        total: 0,
      },
      {
        site: "Site B",
        timestamp: 1507246307439,
        total: 6,
      }
    ]
  },
  {
    key: "Sub region 2",
    values: [
      {
        site: "Site A",
        timestamp: 1507246308269,
        total: 10,
      },
      {
        site: "Site B",
        timestamp: 1507246308683,
        total: 30,
      }
    ]
  }
]
}
];
Run Code Online (Sandbox Code Playgroud)

到目前为止,我拥有的 Vue 代码非常简单:

Vue.component('project-table', {
  template: '#table-template',
  props: {
    data: Array
  }
});

var app = new Vue({
    el: '#table-container',
    data: {data: sites},
    });
Run Code Online (Sandbox Code Playgroud)

和模板:

<div id="table-container">
  <project-table :data="data"></project-table>
</div>
    <script id="table-template" type="text/x-template">
        <table class="u-full-width">
            <thead>
                <tr>
                <th>Project location</th>
                <th>Total</th>
                <th>Timestamp</th>
            </tr>
        </thead>
        <tbody>
        <tr class="name-row" v-for="item in data">
            <th class="name" colspan="3">{{item.key}}</th>
        </tr>
        <tbody>
    </table>
</script>
Run Code Online (Sandbox Code Playgroud)

Vue 中显示表格的机制是什么,就像在 Handlebars 中所做的那样?谢谢!

Ber*_*ert 9

这是更新的模板的相关部分。

<tbody>
  <template v-for="item in data">
    <tr class="name-row" >
      <th class="name" colspan="3">{{item.key}}</th>
    </tr>
    <template v-for="subregion in item.values">
      <tr>
        <th class="group-name" colspan="4">{{subregion.key}}</th>
      </tr>
      <tr v-for="site in subregion.values">
        <td>{{site.site}}</td>
        <td>{{site.total}}</td>
        <td>
          <span>{{site.timestamp}}</span>
        </td>  
      </tr>
    </template>
  </template>
<tbody>
Run Code Online (Sandbox Code Playgroud)

和更新的小提琴

重点是利用template元素在tr每次迭代中渲染多个。