Thymeleaf-为表行创建动态ID

时间:2017-04-01 12:15:17

标签: spring-boot thymeleaf

我是Thymeleaf的新手,我正在尝试为tr创建ID并获取动态行。

成功获取表格行但我不知道如何为Thymeleaf中的每一行创建ID。

<table class="table table-hover" id="table">
  <thead style="background-color:#CCE5FF">
  <tr>
    <th>ID</th>
    <th>Code</th>
    <th>Created Date</th>
    <th></th>
  </tr>
  </thead>
  <tbody>
  <tr th:each="emp,iterStat : ${empList}">
    <td th:text="${emp.id}">ID</td>
    <td th:text="${emp.mdrcode}">Code</td>
    <td th:text="${emp.createDate}">Created Date</td>
    <td>
      <a id="editview" class="btn btn-sm btn-default" th:href="@{/}"><i class="fa fa-edit"></i> View</a>
    </td>
  </tr>
  </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

您可以使用th:id

<!-- this would assign the emp.id to the id attribute of the tr.
<tr th:id="${emp.id}" th:each="emp,iterStat : ${empList}">
    <td th:text="${emp.id}">ID</td>
    <td th:text="${emp.mdrcode}">Code</td>
    <td th:text="${emp.createDate}">Created Date</td>
    <td><a  id="editview" class="btn btn-sm btn-default" th:href="@{/}"><i class="fa fa-edit"></i> View</a></td>
</tr>

我们还可以在id中添加一些文字:

<!-- this would assign someText + emp.id to the id attribute of the tr.
<tr th:id="'someText__${emp.id}__'" th:each="emp,iterStat : ${empList}">
    <td th:text="${emp.id}">ID</td>
    <td th:text="${emp.mdrcode}">Code</td>
    <td th:text="${emp.createDate}">Created Date</td>
    <td><a  id="editview" class="btn btn-sm btn-default" th:href="@{/}"><i class="fa fa-edit"></i> View</a></td>
</tr>