【发布时间】:2021-02-14 08:30:23
【问题描述】:
我需要一个表格,它将像下面这样的对象(数据)作为道具并创建一个表格。 我发现一些 js 库可以帮助从 json 创建表,但我更喜欢更简单和“苗条的方式”。
我被困在生成行上。
<script>
// export let data
let data = {
ignore1: 85,
ignore2: "2020-10-31",
ignore3: "some data",
ignore4: "another data",
ignore5: "../../assets/img/avatar.jpg",
col1: ["cell1", "cell1", "cell1", "cell1"],
col2: ["cell2", "cell2", "cell2", "cell2", "cell2", "cell2"],
col3: ["cell3", "cell3", "cell3"]
}
let columns = []
let values = []
for (const [col, val] of Object.entries(data)) {
if (Array.isArray(val)) {
columns.push(col)
values.push(val)
}
}
console.log(columns)
console.log(values)
// From the list of values get the max len of a list
let rows_len = Math.max(0, ...values.map(item => item.length))
// Normalize lists to have the same length as the biggest one in the nested lists
let rows = values.map(li => {
if (li.length != rows_len) {
let fill_arr = Array.from({length: rows_len - li.length}).map(el => "")
li.push(...fill_arr)
}
return li
})
console.log(rows)
let rows_range = Array.from({length: rows_len}).map(el => "")
</script>
<table class="w-full">
<thead class="capitalize border-b-2">
<tr>
{#each columns as col}
<td>{col}</td>
{/each}
</tr>
</thead>
<tbody>
{#each rows[0] as row, i}
<td>{row}</td>
{/each}
</tbody>
</table>
【问题讨论】:
标签: html-table each svelte