spring 计数Thymeleaf if语句在循环中计算为true的次数

at0kjp5o  于 2024-01-05  发布在  Spring
关注(0)|答案(1)|浏览(226)

我有一个thymeleaf模板,它在List(家务完成对象)上迭代,并在满足if条件时打印一个字母(当家务完成对象具有特定的chore_id和person_id时)。这将导致一个字母字符串。我如何打印出字母的数量而不是重复字母的字符串?thymeleaf是否可以使用迭代计数器变量?

  1. <tr th:each="chore: ${chores}">
  2. <td>
  3. <span th:each="completion : ${completions}">
  4. <span th:if="${(completion.chore?.chore_id) == chore.chore_id and (completion.person?.person_id) == persons[0].person_id}">T</span>
  5. </span>
  6. </td>
  7. </tr>

字符串
这张照片可以说明意图。


的数据

xxls0lw8

xxls0lw81#

在控制器中创建一个数据结构,让您可以轻松地输出所需的内容,这是非常好的。
例如,您可以创建一个记录(或者如果您使用的是较旧的Java版本,则创建一个类),其中包含一个用于显示计数的字段,并将此类记录的示例添加到模型中:

  1. @GetMapping
  2. public String showChoresOverview(Model model) {
  3. List<ChoresOverviewItemViewModel> items = ... // Get the info from your service and convert those object to ChoresOverviewItemViewModel instances in the controller
  4. mode.addAttribute("items", items);
  5. return "chores/index"; //name of you Thymeleaf template
  6. }
  7. private record ChoresOverviewItemViewModel(String description, String lastCompletionUser, Map<String,Integer> thisMonthUserToCountMapping) {
  8. }

字符串

展开查看全部

相关问题