请告诉我如何将模式窗口绑定到列表中的每个用户。代码附在.ps 删除规范的常用按钮工作,但我无法弄清楚它与模态窗口。
<div class="col-12">
<form action="/admin/panel" method="get">
<table class="table">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Имя</th>
<th scope="col">email</th>
<th scope="col">roles</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${userlist}">
<td th:utext="${user.id}"></td>
<td th:utext="${user.name}">...</p></td>
<td th:utext="${user.email}">...</p></td>
<td th:utext="${user.roles}">...</p></td>
<!-- Modal -->
<form action="/admin/panel/addUser" method="post" name="id" th:value="${user.id}">
<td><button type="button" class="btn btn-success" data-toggle="modal" data-target="#editor">Edit</button></td>
<div class="modal fade" id="editor" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<strong>Username</strong>
<input class="form-control" type="text" th:placeholder="${user.name}">
<strong>Email</strong>
<input class="form-control" type="text" th:placeholder="${user.email}">
<strong>Roles</strong>
<input class="form-control" type="text" th:placeholder="${user.roles}">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success" name="id" th:value="${user.id}">Save changes</button>
</div>
</div>
</div>
</div>
</form>
<form action="/admin/panel/deleteUser" method="post" th:value="${user.id}" name="id">
<td><button type="button" class="btn btn-danger" data-toggle="modal" data-target="#deleter">Delete</button></td>
<div class="modal fade" id="deleter" tabindex="-1" role="dialog" aria-labelledby="deleterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleterTitle">Delete user</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<strong>ID</strong>
<input class="form-control" type="text" th:placeholder="${userID}" readonly>
<strong>Username</strong>
<input class="form-control" type="text" th:placeholder="${name}" readonly>
<strong>Email</strong>
<input class="form-control" type="text" th:placeholder="${email}" readonly>
<strong>Roles</strong>
<input class="form-control" type="text" th:placeholder="${roles}" readonly>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</div>
</div>
</div>
</form>
</tr>
</tbody>
</table>
</form>
</div>
这是调用所有用户的控制器:
@GetMapping("/panel")
public String getUsers(Model model) {
model.addAttribute("userlist", userService.getAllUsers());
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User user = (User) authentication.getPrincipal();
model.addAttribute("username", user.getUsername());
model.addAttribute("role", user.getRoles());
return "admin/panel";
}
和删除控制器分别
@PostMapping("/panel/deleteUser")
public String deleteUser(@RequestParam("id") Long id, Model model) {
model.addAttribute("userID", userService.findOne(id).getId());
model.addAttribute("name", userService.findOne(id).getUsername());
model.addAttribute("email", userService.findOne(id).getEmail());
model.addAttribute("roles", userService.findOne(id).getAuthorities());
userService.deleteById(id);
return "redirect:/admin/panel";
}