'add a serial number while performing iteration in thymeleaf
I have a senario in which i must perform iteration on a list and display result as a grid.Also include serial number in it. Now what I did for serial number is given below
<div th:with="i=0">
<tr th:each="mydate:${data.Listdata}" >
<div th:with="i=${i+1}">
<td th:text="${i}">
</div>
//other codes
</tr>
</div>
but only 1 appears in all serial number.Can anyone help me with this?
Solution 1:[1]
You can use the index of the iteration status:
<tr th:each="item,iterator : ${items}">
<td th:text="${iterator.index}"></td>
</tr>
Solution 2:[2]
You can use the iterationStatus to begin with 1 instead of 0.
<tr th:each="item,iterationStatus : ${items}">
<td th:text=${iterationStatus.count}></td>
</tr>
This gives the table Sno. Starting with 1.
Solution 3:[3]
To get the index:
<tr th:each="item: ${items}">
<td th:text="${iterator.indexOf(item)}"></td>
</tr>
To get a numbering increment index by one.
<tr th:each="item: ${items}">
<td th:text="${iterator.indexOf(item) + 1}"></td>
</tr>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Tony Banks |
Solution 3 | David Kariuki |