问题描述
我想在树枝上创建一个表。表中的行是动态添加的,具体取决于用户在 admin 中的配置。我快到了,但是每个 tr 都需要以一个数字作为前缀。
如何使数字 (1,2,3) 动态化,因为我事先不知道表中有多少行?我已经查看了批次和树枝文档中的解释,但它没有解释当您不知道最大数量时该怎么做。
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
解决方法
由于您没有在问题中提供 twig
代码,我假设您正在使用 for
构建表格
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
{% for item in items %}
<tr>
<th scope="row">{{ loop.index }}</th>
<td>{{ item.first_name }}</td>
<td>{{ item.last_name }}</td>
<td>{{ item.handle }}</td>
</tr>
{% endfor %}
</tbody>
</table>