[英]Thymeleaf + Spring Boot + HTML + iterate through list in the list
[英]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.