我正在编写一个模板,它应该呈现员工列表。员工以部门列表的形式传递给Thymeleaf,每个部门都有自己的员工名单。
因为我的任务是把它们全部展示出来--问题是如何处理连续的数字运算。每个员工都应该以一行的形式显示下一个号码。
下面的尝试允许对给定部门的员工进行索引,每个部门都有新的数字:
<table th:each="department, depStatus : departmentList">
<tr th:each="employee, empStatus : department.employees">
<td th:inline="text">[[${empStatus.index+1}]]</td>但我的观点是,在所有部门中保持连续的计数,如下所示:
1. Employee A from Dept X
2. Employee B from Dept X
3. Employee C from Dept Y我知道我可以在服务器端使这个结构扁平化,但我不能相信这是唯一的方法。
我还尝试用th:with="idx = 0"插入局部变量,然后在某个地方使用th:with="idx = ${idx} + 1增加局部变量,但这只是覆盖了idx值。
发布于 2015-03-09 00:02:35
您试图实现的是更新局部变量,并使新值在比更新位置更宽的范围内可见。这就是为什么它与定义相矛盾的原因。我认为您无法避免对服务器端进行一些调整,例如,如您所建议的那样,提供一些关于您的结构的更平坦的视图。
另一方面,一个快速解决方案(假设您并不严格地使用一个表)它可能是在尝试一个有序列表,同时使用th:块来封装部门:
<ol>
<!--/*/ <th:block th:each="dept : ${departmentList} "> /*/-->
<li th:each="emp : dept.employees" th:text="|${emp.name} from ${dept.name}|"></li>
<!--/*/ </th:block> /*/-->
</ol> 发布于 2021-01-10 16:48:08
<div th:each="department : ${departmentList}">
<div th:each="employee : ${department.employees}">
<div th:text="${#ids.seq ('')}"></div>
</div>
</div>见docs https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#ids
发布于 2015-03-05 19:21:00
对于任何与循环相关的操作,您都应该能够使用iterationStatus实用程序。
查看下面的代码以显示迭代项的索引。
<tr th:each="emp,iterStat : ${employess}">
<td th:text="${iterStat.count}"></td>
<td th:text="${emp.name}"></td>
<td th:text="${emp.department}"></td>
</tr>https://stackoverflow.com/questions/28236781
复制相似问题