Thymeleaf 中嵌套循环项的连续计数

Continuous numeration of nested loop items in Thymeleaf

我正在编写一个应呈现员工列表的模板。员工以部门列表的形式传递给 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 值。

对于任何与循环相关的操作,您应该能够使用 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>

看看我是否理解你的问题:

您有一个将员工分配到部门的模型。根据您对问题的描述,一个部门对象有一个分配给它的员工列表。

据我所见,您正在从您的控制器 class 中获取部门的集合。

如您所知,Thymeleaf 可以通过 th:each 属性在元素中使用迭代。您可以稍微减少您的代码只在员工集合中执行迭代,例如:

<table>
    <tr th:each="employee, empStatus : ${department.employees}">

NOTE: If I'm not mistaken, the result of your template will repeat the table element due to the number of departments.

这样,您的代码会更简洁,您可以只专注于与员工合作。


关于为每一行定义索引的主要问题,您可以使用称为 "iteration status" 的东西。

我相信你已经对此有所了解,但我会尝试解释我的方式,这可能会给你一些启发:

根据 Thymeleaf 文档,您可以使用 Thymeleaf 提供的变量,该变量将使您能够访问有关正在进行的迭代的一些信息。使用此变量,您可以收集一些信息,其属性在 Thymeleaf documentation(检查会话 6.2)中被引用:

The current iteration index, starting with 0. This is the index property. The current iteration index, starting with 1. This is the count property. The total amount of elements in the iterated variable. This is the size property. The iter variable for each iteration. This is the current property. Whether the current iteration is even or odd. These are the even/odd boolean properties . Whether the current iteration is the first one. This is the first boolean property. Whether the current iteration is the last one. This is the last boolean property.

要访问此变量,您需要在声明代表每次迭代的项目之后立即声明它,就像您之前所做的那样:

<tr th:each="employee, empStatus : ${department.employees}">

你也可以省略这个变量的声明,仍然使用它。这是唯一可能的,因为 Thymeleaf 向您做了这个声明,它采用给代表每个迭代元素的变量的名称(在本例中为 "employee"),并向其添加后缀 "Stat"(导致"employeeStat").

您只能在具有 th:each 属性的标记定义的代码片段中访问此变量及其内部属性。

如果您知道如何在 Web 应用程序页面中显示行和列,您所要做的就是使用迭代状态变量的属性,例如:

<td th:text="${empStatus.index}"></td>

或者,如果您愿意:

<td th:text="${empStatus.count}"></td>

如果您有任何问题,请发表评论,我会编辑我的答案以便更好地理解。


编辑

哦,我现在才明白你真正的问题是什么,抱歉。

如果你想创建一个列表,其中每个员工的名字后面跟着他们各自部门的名字,每个列表项都有不同的编号(都是规定的),那么问题就很简单了。

首先您要做的就是遍历所有部门,根据需要显示员工姓名和部门名称。这将解决部分问题,因为这只是一个简单的逻辑问题和迭代的使用。

要使其定期发生,即按顺序列出每条记录/项目,例如,您可以创建一个局部变量,这是 Thymeleaf 中的其他资源之一。

要快速了解如何设置和使用此类资源,请查看此 link:setting up a value for a variable name in thymeleaf

或者您可以查看 Thymeleaf(第 9 节)的文档:http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#local-variables

创建变量后,尝试将其用作计数器,它会随着列出的每条记录递增。

希望对您有所帮助,解决您的问题。如果这不起作用,请再次评论,我会尝试其他解决方案。同样,如果这不是您的问题,也请发表评论。

NOTE: Again I ask you to be careful in your loop structure. You may be creating a repeating structure that create multiple table structures in HTML code, which I believe not be what you want. Also, make sure to check if you are performing arithmetic operations correctly. I believe that your increment operation must be within the braces {}, and not out of them.


编辑 2

我找遍了所有的地方来做这个......但不幸的是我只能找到这个:

Counters in Loops in Thymeleaf

您可以在 Thymeleaf 文档(附录 B - 编号)中找到更多相关信息。这是使用 Thymeleaf 进行循环的有效方法。但是,很难进行嵌套循环,因为我们在 Thymeleaf 中使用属性,而不是使用标签/HTML 元素。以防万一,您只能在嵌套循环中创建新行,这是不可能的。也许你可以尝试在同一个 "tr" 标签中使用两次 "th:each" 属性,例如:

<tr th:each="" th:each"">
</tr>

虽然我相信这也行不通...

我真的很抱歉,Maciej,因为这不是最终答案,所以没有多大帮助。但是,我请您稍等片刻,并尝试检查您的 Web 应用程序。试想一下,您想要设计一个 table,对事物进行排序,并用数字列出所有事物。在 Java 代码中执行此操作不是更好吗?此外,你不会尝试在 SQL 中这样做吗?您不想使用 SQL 获得数据库的排序结果吗?意识到你正在让一个组织在这里工作,每个部门的每个员工都会被列出,部门按顺序排列。

你看,我并不是说你做错了,但是在这种情况下,如果我们停下来分析,我们开发人员通常会从应用程序的业务层得到有序的结果,我们让视图层只负责显示它们。

如果这在您的应用程序中是不可能的,并且我现在发给您的link不能指导您解决您的问题,那么我非常抱歉,但我无能为力。也许是时候在 Thymeleaf 论坛上提问了,它的开发人员可能会帮助您(可能):

http://forum.thymeleaf.org/General-Usage-f2234430.html

我真诚地希望你能成功解决你的问题。如果你这样做了,我恳请你与这里的每个人分享你的解决方案,因为很多人可能有一天会面临同样的情况。

Até depois, e boa sorte amigo.

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

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

<ol>
  <!--/*/ <th:block th:each="dept : ${departmentList} "> /*/-->

   <li th:each="emp : dept.employees" th:text="|${emp.name} from ${dept.name}|"></li>

  <!--/*/ </th:block> /*/-->
</ol>  
    <div th:each="department : ${departmentList}">
        <div th:each="employee : ${department.employees}">
            <div th:text="${#ids.seq ('')}"></div>
        </div>
    </div>

查看文档 https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#ids

感谢https://www.tutorialfor.com/questions-69803.htm