Bootstrap panels within Thymeleaf loop - same id and href

隐身守侯 提交于 2019-12-24 06:45:17

问题


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:


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">


来源:https://stackoverflow.com/questions/45114891/bootstrap-panels-within-thymeleaf-loop-same-id-and-href

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!