html - Vue 组件返回多个表行

标签 html vue.js vuejs2

我试图从单个组件 v-design-row 返回两个 tr 元素。我知道 vue 要求将模板元素包装在一个 div 中,但我不能将它们包装在一个 div 中,因为 html 表需要标签结构。当我在 v-design-row 组件中添加第二个 tr 时,vue 不会呈现任何内容。

谁能建议实现这一目标的最佳方法?

主.vue

   <table class="table">
        <thead>
            <th>Image</th>
            <th>Name</th>
            <th>Description</th>
            <th>Status</th>
            <th>Order</th>
            <th>Edit</th>
            <th>Delete</th>
        </thead>
        <tbody v-for="catagory in catagories">
            <v-catagory-row :catagory="catagory"></v-catagory-row>
            <v-design-row v-for="design in catagory.designs" :design="design"></v-design-row>
        </tbody>
    </table>

v-design-row.vue

<template>
<tr>
    <td><img :src="design.image_directory" :alt="design.name"></td>
    <td>{{ design.name }}</td>
    <td>
        <button class="btn btn-secondary" type="button" data-toggle="collapse" :data-target="'#' + design.identifier" aria-expanded="false" :aria-controls="design.identifier">
            <i class="fa fa-plus" aria-hidden="true"></i> Show
        </button>
    </td>
    <td>
        <div class="switch">
            <input :id="'active'+design.id" v-model="design.active" class="btn-toggle btn-toggle-round-flat" type="checkbox">
            <label :for="'active'+design.id"></label>
        </div>
    </td>
    <td>
        <button class="btn btn-secondary" type="button">
            <i class="fa fa-caret-up" aria-hidden="true"></i>
        </button>
        <button class="btn btn-secondary" type="button">
            <i class="fa fa-caret-down" aria-hidden="true"></i>
        </button>
        {{ design.sort_order }}
    </td>
    <td>
        <button class="btn btn-secondary" type="button">
            <i class="fa fa-pencil" aria-hidden="true"></i>
        </button>
    </td>
    <td>
        <button class="btn btn-secondary" type="button">
            <i class="fa fa-trash" aria-hidden="true"></i>
        </button>
    </td>
</tr>
<tr>
    <td class="p-0" colspan="7">
        <div class="collapse" :id="design.identifier">
            <div class="p-3">
                {{ design.description }}
            </div>
        </div>
    </td>
</tr>
</template>

最佳答案

您可以使用多个 <tbody> 对行进行分组像这样的部分:

<table>
  <thead>
    <tr> ... </tr>
  </thead>
  <tbody>
     <tr> ... </tr>
     <tr> ... </tr>
  </tbody>
  <tbody>
     <tr> ... </tr>
     <tr> ... </tr>
  </tbody>
</table>

您可以通过使用 <tbody> 在模板中使用它作为根元素:

<template>
  <tbody>
    <tr> ... </tr>
    <tr> ... </tr>
  </tbody>
</template>

关于html - Vue 组件返回多个表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46893445/

相关文章:

html - 如何使用 Tab 键使 Toggle 按钮可聚焦

javascript - Vue.js,使用插件方法

vue.js - 映射的 getter 未定义

html - 图像中的悬停效果

javascript - 从计时器函数获取秒数到我的 View

javascript - 将外部脚本添加到 vue.js

javascript - Vue.js 模型绑定(bind)到 jquery 输入

javascript - 如何告诉vuefire的firestore在 "created"钩子(Hook)之后执行?

javascript - 在 vue.js 中动态添加元素

javascript - 下拉值更改时更新 html 表