繁体   English   中英

如何在 Thymeleaf- Spring - Boot 中一个接一个地迭代?

[英]How iterate by one after another in Thymeleaf- Spring - Boot?

我们如何迭代 Thymeleaf 页面上的列表? 假设我有一个包含 object 的列表

我可以在Thymeleaf页面上依次显示所有用户信息,但这实际上是我不想实现的。 有什么方法可以让我一次显示一个用户,下一个按钮上会有两个按钮,单击移动到下一个可用用户并显示信息并隐藏上一个用户信息,当单击上一个按钮时返回到前一个。 因为我有超过 20+ 甚至有时 50+ 用户信息。

public class Info {
private String name;
private String dob;
private String university;
// constructor , getter and setter
}

我的 controller class

@GetMapping("/get/All-user-info")
public String getAllUserInfo(Model model){
  List<Info> info = new ArrayList<>();
info.add(new Info("NA","22/02/2020","Florida"));
info.add(new Info("PA","22/01/2020","Florida"));
info.add(new Info("CA","22/03/2020","Florida"));
info.add(new Info("DA","22/04/2020","Florida"));
model.addAttribute("info",info);
return "page-thyme";
}

我的 Thymeleaf 页面看起来像。 它显示所有在一个 go。我真的不明白在这里做什么。

<div th:each="info, status : ${info}">
<p th:text="${info.name}">
<p th:text="${info.dob}">
<p th:text="${info.university}">
</div>

编辑:我添加了一项服务 class

@GetMapping("/get/All-user-info/{departmentId}")
public String getAllUserInfo(Model model, @PathVariable String departmentId){
  List<Info> info = userService.getUser(departmentId);
model.addAttribute("info",info);
return "page-thyme";
}

您可以像这样在控制器的参数中传递 object 的索引:

@GetMapping (path = "/modal")
public String modal(Model model, @RequestParam(name = "page", defaultValue = "0") int page) {
    List<Info> info = new ArrayList<>();
    
    info.add(new Info("NA","22/02/2020","Florida"));
    info.add(new Info("PA","22/01/2020","Florida"));
    info.add(new Info("CA","22/03/2020","Florida"));
    info.add(new Info("DA","22/04/2020","Florida"));
    model.addAttribute("info",info);
    model.addAttribute("index",page);
    return "modal";
}

并在您的 HTML 页面中添加一种分页:

<div >
  <p th:text="${info[__${index}__].name}">
  <p th:text="${info[__${index}__].dob}">
  <p th:text="${info[__${index}__].university}">
  <div th:each="info, status : ${info}"><a th:text="${status.index}" th:href="@{modal(page=${status.index})}"></a></div>
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM