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

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

我不知道如何使用Vue和v-for等来实现同样的效果.

尽我所能胡乱摆弄:https://jsfiddle.net/cj7go6bv/

我不知道如何处理数据 struct ,同时保持车把中的<tr>

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

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,
      }
    ]
  }
]
}
];

到目前为止,我掌握的Vue代码非常有限:

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

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

模板:

<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>

Vue中显示桌子的机制是什么,就像在把手中完成的一样?谢谢

推荐答案

以下是模板更新的相关部分.

<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>

更新了fiddle个.

主要的一点是利用template元素在每次迭代中渲染多个tr元素.

Vue.js相关问答推荐

虚拟DOM在Vue中姗姗来迟

如何防止Nuxt3重新加载时滚动跳到网页顶部

Vue 3使用计算(computed)属性对象获取图像信息,在模板中呈现值时变为NaN

v-model 修饰符返回空

超出最大调用堆栈大小 - Vue路由

为什么我在 Vue npm run serve 的 localhost 中得到无法获取

已分配计算(computed)属性,但它没有设置器

使用 Vue.js 将文件输入绑定到按钮

VueJS - 将单独的组件加载到 vue 实例中

Vuex 和 Event Bus 有什么区别?

在 vuejs 中显示两个组件,但只显示一个,为什么?

在 Vue 中,如何使用 npm run build 将 .htaccess 包含到生产环境中?

Vue3在'vue-router'中找不到导出'createWebHistory,createRouter'

如何在不实例化根实例的情况下使用 vue 组件?

事件 click点击的无效处理程序:未定义

vuejs中通过变量动态调用方法

Vue3在按钮单击时以编程方式创建组件实例

如何在 Vuetify DataTable 组件中设置初始每页行数值?

如何使用vue js判断组件本身的数据何时发生变化?

这个业务逻辑有多少属于 Vuex?