Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf- Create dynamic id for table row

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>
like image 814
Durga Avatar asked Oct 24 '25 20:10

Durga


1 Answers

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>
like image 194
Tommy Schmidt Avatar answered Oct 26 '25 15:10

Tommy Schmidt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!