微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

表号迭代器

如何解决表号迭代器

我想在树枝上创建一个表。表中的行是动态添加的,具体取决于用户在 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>

demo

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。