【发布时间】:2019-11-06 14:40:14
【问题描述】:
我有一个带有一些表格数据的 JSON 提要,我正在使用 Vue.js 来显示一个 HTML 表格:
<tr v-for="row, index in content">
<td class="index" v-text="index + 1"></td>
<td v-for="column in row" v-text="column"></td>
</tr>
此提要为每一行返回一个数组,但有些行具有命名键。使用这个简单的 foreach 循环,它会忽略键,这将生成一个无法以正确方式显示数据的表。
如何使用密钥生成表格?我已经考虑获得highest value from the keys 并使用Array.fill(),但我认为必须有更好的方法。
这是我的数据:
{
"content": [
[
"",
"In scope:",
"Not in scope:"
],
[
"Cars",
"",
"X"
],
[
"Bikes",
"X",
""
],
{
"0": "Houses",
"2": "X"
}
]
}
请注意,这是虚构数据,实际数据可能会有所不同,因此我无法找到具有固定列数的解决方案。
实际输出:
| | In scope: | Not in scope: |
|--------|-----------|---------------|
| Cars | | X |
| Bikes | X | |
| Houses | X |
预期输出:
| | In scope: | Not in scope: |
|--------|-----------|---------------|
| Cars | | X |
| Bikes | X | |
| Houses | | X |
【问题讨论】:
-
您是否能够预处理数据(在将其交给视图之前)?在这种情况下,您可能希望将其转换为已知结构,例如:jsfiddle.net/exh824v1/1 - 这是否符合您的要求?
-
我希望第一个元素包含所有列标题定义,但这只是一个假设。
-
从我的脑海中我只能想到你描述的
Array.fill()方式。如果有原生的 Vue.js 解决方案会很有趣。
标签: javascript arrays vue.js