Thymeleaf-为表行创建动态 id

Dur*_*rga 4 thymeleaf spring-boot

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

成功获取表行,但我不知道如何为 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>
Run Code Online (Sandbox Code Playgroud)

Tom*_*idt 6

你可以用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>
Run Code Online (Sandbox Code Playgroud)

我们还可以向 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>
Run Code Online (Sandbox Code Playgroud)