【发布时间】:2020-02-15 17:47:05
【问题描述】:
我正在尝试使用 Vue 呈现具有固定标题的动态表。行使用带有行组件的v-for 呈现,然后使用一些单元组件使用v-for 呈现每一行的单元(单元的数量等于<thead> 中的<th> 元素的数量)。问题是 Vue 似乎只在第一列内呈现整行,而不是相关列标题下的每个单元格。
我的代码:
<template>
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr-component v-for="row in dataRows" :key="row.ID">
<td-component v-for="cell in dataCells" :key="cell.ID"></td-component>
</tr-component>
</tbody>
</table>
</template>
结果:
Column 1 | Column 2 | Column 3
Cell 1 | Cell 2 | Cell 3 |
我做错了什么?这不是格式问题 - 更改单元格宽度会更改第 1 列的宽度。
PS。 <td> 组件中还有一些其他元素(div 等),但总是包裹在 <td> 中。
【问题讨论】:
-
我猜是
tr-component的模板有问题。可以发一下吗? -
给你:它看起来像这样
<template> <tr class="record"> <component v-for="cell in dataCells" // dataCells is a prop passed from parent, array containing config data for cells :key="recordItem.userIndex" :is="recordItem.compName" ></component> </tr> </template> -
我发现了一个罪魁祸首——这种行为是由使用“display: flex”附加到
的 CSS 类引起的。感谢您尝试回答我的问题。
标签: vue.js html-table