【问题标题】:Vue component returning multiple table rowsVue组件返回多个表行
【发布时间】:2017-10-23 15:48:18
【问题描述】:

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

任何人都可以提出最好的方法来实现这一点吗?

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

【问题讨论】:

    标签: html vue.js vuejs2


    【解决方案1】:

    您可以使用多个&lt;tbody&gt; 部分对行进行分组,如下所示:

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

    您可以在模板中使用 &lt;tbody&gt; 作为根元素:

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

    【讨论】:

      【解决方案2】:

      由于您的v-design-row 中的第二个tr 包含设计说明,我建议这是整个元素,它应该占据一排并有自己的布局。

      或者,您可以在组件中单独包含描述和所有其他数据。

      还有,声明

      vue 要求模板元素被包裹在一个 div 中

      不太正确。模板可以有任何根元素,而它是单个元素。所以你可以拥有

      <template>
        <tr>
        </tr>
      </template>
      

      甚至

      <template>
        <tr v-if="condition == 1">
        </tr>
        <tr v-else-if="condition == 2">
        </tr>
        <tr v-else>
        </tr>
      </template>
      

      【讨论】:

      • 我想我明白了,我将v-for 添加到tbody 内的模板中。这个模板可以用vue逻辑做一个不可见的html标签。
      • 您不能在组件模板中使用
      • @marlar , you can.
      • @SuiDream。你是对的,我不清楚。我的意思是你不能使用
      猜你喜欢
      • 1970-01-01
      • 2022-01-25
      • 2020-03-26
      • 2022-07-27
      • 2013-06-12
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多