Issue
<tbody>
<tr th:each="bean , beanStat : ${list}">
<td class="text-center" th:text="${beanStat.size-beanStat.count+1}+(${paging.cri.page}-1)*${cri.perPageNum}" >1</td>
<!-- paging.totalcount -->
<!-- <tr th:each="bean , beanStat : ${list}">
<td class="text-center" th:text="${beanStat.size-beanStat.count+1}+(${paging.cri.page}-1)*${cri.perPageNum}" >1</td>
First of all, I'm sorry for not speaking English well.
I want to number the articles, but I want to number them in reverse order. I think you can do it like below in jsp In thymeleaf, I wonder how to do this in reverse order (is there an index?)
Total number of records - ( (Current page number - 1) * I think it is the number of records displayed per page.
${(totalCount - status.index) - ( (currentPage - 1) * displayNum ) }
I implemented it like this, but I wonder if there is such a part as status.index in the thymeleaf.
Solution
There is an equivalent to your JSP approach in Thymeleaf - a set of iteration tracking attributes you can use when processing a list.
Assuming you have a Java list like this:
List<String> list = new ArrayList<>();
list.add("One");
list.add("Two");
list.add("Three");
Then you can use the following Thymeleaf:
<table>
<thead>
<tr>
<th>Number</th>
<th>Item</th>
</tr>
</thead>
<tbody>
<tr th:each="item, iterStat : ${list}">
<td th:text="${iterStat.size - iterStat.count +1}"></td>
<td th:text="${item}"></td>
</tr>
</tbody>
</table>
This generates:
<table>
<thead>
<tr>
<th>Number</th>
<th>Item</th>
</tr>
</thead>
<tbody>
<tr>
<td>3</td>
<td>One</td>
</tr>
<tr>
<td>2</td>
<td>Two</td>
</tr>
<tr>
<td>1</td>
<td>Three</td>
</tr>
</tbody>
</table>
Update
If you want to add paging into the calculation, then your Java code needs to know:
- current page number
- maximum number of rows per page (there may be fewer in the final page)
- total number of records (all pages)
Let's assume I pass those 3 values from Java to my Thymeleaf model with some new data:
List<String> list = new ArrayList<>();
list.add("Alfa");
list.add("Bravo");
list.add("Charlie");
list.add("Delta");
list.add("Echo");
int currentPage = 1;
int rowsPerPage = 5;
int totalRecords = 9;
Now, the Thymeleaf changes to this:
<table>
<thead>
<tr>
<th>Number</th>
<th>Item</th>
</tr>
</thead>
<tbody>
<tr th:each="item, iterStat : ${list}">
<td th:text="${totalRecords - (currentPage * rowsPerPage)
+ rowsPerPage - iterStat.count +1}"></td>
<td th:text="${item}"></td>
</tr>
</tbody>
</table>
For page 1 this generates:
<tbody>
<tr>
<td>9</td>
<td>Alfa</td>
</tr>
<tr>
<td>8</td>
<td>Bravo</td>
</tr>
<tr>
<td>7</td>
<td>Charlie</td>
</tr>
<tr>
<td>6</td>
<td>Delta</td>
</tr>
<tr>
<td>5</td>
<td>Echo</td>
</tr>
</tbody>
For page 2 (the final 4 items), we have:
List<String> list = new ArrayList<>();
list.add("Foxtrot");
list.add("Golf");
list.add("Hotel");
list.add("India");
int currentPage = 2;
int rowsPerPage = 5;
int totalRecords = 9;
Note: Only the current page number has changed. The rows per page and the total records: those values remain the same across all pages.
And the same Thymeleaf template generates this:
<tbody>
<tr>
<td>4</td>
<td>Foxtrot</td>
</tr>
<tr>
<td>3</td>
<td>Golf</td>
</tr>
<tr>
<td>2</td>
<td>Hotel</td>
</tr>
<tr>
<td>1</td>
<td>India</td>
</tr>
</tbody>
Answered By - andrewJames
Answer Checked By - Mildred Charles (JavaFixing Admin)