I am new to Thymeleaf and I am trying to create id for tr and getting dynamic rows.
Successfully getting table rows but I don't know hot to create id for each row in Thymeleaf.
<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>
You can use th:id for that.
<!-- 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>
We can also add some text to the 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With