前端表格渲染数据:嵌套结构实现层级展示
你可以使用嵌套的表格结构来渲染这个数据。首先,你可以使用一个外层的表格来渲染每个对象的基本信息(id、date、name、address),然后在每个对象的基本信息下面再嵌套一个表格来渲染'cardList'数组中的数据。
以下是一个示例代码:
<table>
<thead>
<tr>
<th>ID</th>
<th>Date</th>
<th>Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
{% for item in tableData %}
<tr>
<td>{{ item.id }}</td>
<td>{{ item.date }}</td>
<td>{{ item.name }}</td>
<td>{{ item.address }}</td>
</tr>
{% if item.cardList %}
<tr>
<td colspan="4">
<table>
<thead>
<tr>
<th>Card Name</th>
<th>Card Status</th>
<th>Card Size</th>
<th>Card ID</th>
</tr>
</thead>
<tbody>
{% for card in item.cardList %}
<tr>
<td>{{ card.cardName }}</td>
<td>{{ card.cardStatus }}</td>
<td>{{ card.cardSize }}</td>
<td>{{ card.cardId }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
在上面的示例代码中,我们使用了两个嵌套的表格结构。外层的表格用于渲染每个对象的基本信息,内层的表格用于渲染'cardList'数组中的数据。我们使用了{% if %}语句来判断是否需要渲染内层的表格,如果'cardList'存在,则渲染内层的表格。
你可以根据自己的需求对表格样式进行调整,并将上面的示例代码嵌入到你的前端代码中进行渲染。
原文地址: https://www.cveoy.top/t/topic/fw8a 著作权归作者所有。请勿转载和采集!