【发布时间】:2021-07-19 21:58:27
【问题描述】:
我想在我的 Thymeleaf 模板中对多个迭代器保持全局计数。 我的父母有孩子的模型如下所示:
val people = listOf(
Person("p1", listOf(Person("c1"), Person("c2"))),
Person("p2", listOf(Person("c3"))),
Person("p3", listOf(Person("c4"), Person("c5"))),
)
我现在的模板中是这样的:
<div>
<h1>Keep count over multiple iterators</h1>
<div th:each="parent, parentStat : ${model.data.people}">
<h2 th:text="${parent.name}"></h2>
<div th:each="child, childStat : ${parent.children}">
<span th:text="${child.name}"/> (Global count: <span th:text="${childStat.count}"/>)
</div>
</div>
</div>
输出如下:
Keep count over multiple iterators
p1
c1 (Global count: 1)
c2 (Global count: 2)
p2
c3 (Global count: 1)
p3
c4 (Global count: 1)
c5 (Global count: 2)
显然,全局计数不起作用,因为它只是当前父级的子级计数。 (我只对 1 级父子层次结构感兴趣)。
在 Thymeleaf 中我想要的一切可能吗?或者是在模型中保持计数的唯一可能性?
【问题讨论】:
-
你试过了吗:
<span th:text="${childStat.count + parentStat.count}"/>? -
是的,我做了,输出是:c1=2, c2=3, c3=3, c4=4, c5=5。这样做的问题是我应该添加所有以前父母的孩子的数量,我不能将其存储在百里香变量中
标签: thymeleaf