Bootstrap panels within Thymeleaf loop - same id a

2019-08-03 21:21发布

I'm trying to generate bootstrap collapse panels within Thymeleaf loop, like that:

<table>
  <tr th:each="p, iterStat : ${completedList}">
    <td style="padding: 0 15px 0 15px;">
      <div class="panel panel-success">
        <div class="panel-heading">
          <h4 class="panel-title">
            <a data-toggle="collapse" data-parent="#accordion"
               href="#collapseOne" th:text="${p.key}">p.k</a>
            <span style="float: right;" class="glyphicon glyphicon-ok"></span>
          </h4>
        </div>
        <div th:id="collapseOne" class="panel-collapse collapse in">
          <div class="panel-body">
            <span th:text="${p.value}"></span>
          </div>
        </div>
      </div>
    </td>
  </tr>
</table>

But I have the same id and href for every element of the loop so the panels doesn't works.

Can I do something to change dynamically id and href?

1条回答
三岁会撩人
2楼-- · 2019-08-03 21:41

You can use the status variable (iterStat) to create a unique id for every row:

<a data-toggle="collapse" data-parent="#accordion" th:href="'#collapse' + ${iterStat.index}" th:text="${p.key}">

and

<div th:id="'collapse' + ${iterStat.index}" class="panel-collapse collapse in">
查看更多
登录 后发表回答