Counter = 0
<div th:each="student: ${students}">
<div th:if="${student.name == 'Felix'}">
Counter = Counter + 1
<div/>
</div>
<div th:text="${Counter}"></div>
大家好,我仍然在基于数组中的匹配进行条件渲染方面遇到困难。当 Counter > 0
时是否有渲染某些内容的选项?我知道代码无效,但这只是所需的输出。
我知道 th:if
或类(class)。简单的 contains()
不起作用,因为数组包含多个对象,而不是字符串或易于使用的内容。
有人知道答案吗?
最佳答案
不,你不能这样做。您不应该更新 Thymeleaf 标记中的变量。要么:
- 在渲染之前添加计数(在模型上)。
-
<div th:text="${#lists.size(students.?[name == 'Felix'])}" />
关于java - Thymeleaf 计数每个循环中的匹配项(对象数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59590579/