在Thymeleaf中连续计算嵌套循环项

Mac*_*ski 9 java thymeleaf

我正在编写一个模板,该模板应该呈现员工列表.员工以部门列表的形式传递给Thymeleaf,每个部门都有自己的员工列表.

我的任务是展示所有 - 问题是处理连续的计算.每个员工应显示为下一个号码的行.

以下尝试允许索引给定部门的员工,每个部门都有新的编号:

<table th:each="department, depStatus : departmentList">
    <tr th:each="employee, empStatus : department.employees">
        <td th:inline="text">[[${empStatus.index+1}]]</td>
Run Code Online (Sandbox Code Playgroud)

但我的观点是要通过所有部门保持连续计算,就像这样:

1. Employee A from Dept X
2. Employee B from Dept X
3. Employee C from Dept Y
Run Code Online (Sandbox Code Playgroud)

我知道我可以在服务器端使这个结构平坦,但我不能相信这是唯一的方法.

我也尝试过引入局部变量,th:with="idx = 0"然后在某处增加它th:with="idx = ${idx} + 1,但这只是覆盖了外部idx值.

gri*_*rid 4

您想要实现的目标是更新局部变量,并使新值在比更新范围更宽的范围内可见。这就是为什么它与 th:with 定义相矛盾。\n我认为您无法避免进行一些服务器端调整,例如按照您的建议提供一些更平坦的结构视图。

\n\n

另一方面,一个快速的解决方案(假设您并不严格使用表格)它可能会在使用 th:blocks 作为封闭部门时尝试有序列表:

\n\n
<ol>\n  <!--/*/ <th:block th:each="dept : ${departmentList} "> /*/-->\n\n   <li th:each="emp : dept.employees" th:text="|${emp.name} from ${dept.name}|"></li>\n\n  \xef\xbf\xbc<!--/*/ </th:block> /*/-->\n</ol>  \n
Run Code Online (Sandbox Code Playgroud)\n