Springboot-jpa-thymeleaf -bootstrap。使用模态表单更新字段后,如何刷新过滤的表内容

发布于 2025-02-08 02:40:57 字数 1215 浏览 3 评论 0原文

我有一个带有以下控制器代码的项目表,

@RequestMapping(method = RequestMethod.GET)
public String listProjects(Model model){
    model.addAttribute("projects", this.projectService.getProjects());
    return "my-projects";
}

我选择一行以显示与项目关联的作业列表,

@RequestMapping (value = "/getAssignmentsByProjectId", method = {RequestMethod.GET})
public String getAssignmentsById(Model model, Long id) {
    model.addAttribute("header", "Assignments for Project - " + this.projectService.getProjectNameById(id));
    model.addAttribute("assignments", this.assignmentService.getAssignmentsByProjectId(id));
    return "project-assignments";
}

然后编辑一行以使用模态表单和一些JS更改值

@RequestMapping("/getAssignmentById")
@ResponseBody
public Assignment getAssignmentById(Long id) {
    Assignment assignment = this.assignmentService.getAssignmentById(id);
    return assignment;
}

,然后我将更新写回到模式形式的数据库

@RequestMapping("/updateAssignment")
public String getAssignmentById(Long id, Double rate){
    this.assignmentService.updateAssignmentById(id,rate);
    return "project-assignments";
}

模态关闭,而我的作业列表现在没有行。

如何刷新仍然由同一ProjectID过滤的作业列表?

I have a table of projects populated with the below Controller code

@RequestMapping(method = RequestMethod.GET)
public String listProjects(Model model){
    model.addAttribute("projects", this.projectService.getProjects());
    return "my-projects";
}

I select a row to display a list of Assignments associated with a project

@RequestMapping (value = "/getAssignmentsByProjectId", method = {RequestMethod.GET})
public String getAssignmentsById(Model model, Long id) {
    model.addAttribute("header", "Assignments for Project - " + this.projectService.getProjectNameById(id));
    model.addAttribute("assignments", this.assignmentService.getAssignmentsByProjectId(id));
    return "project-assignments";
}

I then edit a row to change a value using a Modal Form and some JS

@RequestMapping("/getAssignmentById")
@ResponseBody
public Assignment getAssignmentById(Long id) {
    Assignment assignment = this.assignmentService.getAssignmentById(id);
    return assignment;
}

Then I write the update back to the database from the modal form

@RequestMapping("/updateAssignment")
public String getAssignmentById(Long id, Double rate){
    this.assignmentService.updateAssignmentById(id,rate);
    return "project-assignments";
}

The modal closes and my list of assignments now has no rows.

How do I refresh the list of assignments, still filtered by the same ProjectId?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2025-02-15 02:40:57

如果我正确理解您的问题,那么您会执行AJAX请求以获取在模式对话框中显示的信息,并使用常规表单帖子实际更新信息。

我猜该片段:

@RequestMapping("/updateAssignment")
public String getAssignmentById(Long id, Double rate){
    this.assignmentService.updateAssignmentById(id,rate);
    return "project-assignments";
}

应该这样完成:

@PostMapping("/updateAssignment")
public String updateAssignmentById(Long id, Double rate){
    this.assignmentService.updateAssignmentById(id,rate);
    return "redirect:/getAssignmentsByProjectId?id=" + id;
}

这将在更新后将用户重定向到正确的URL,以显示给定项目ID的作业。

If I understand your question correctly, then you do an AJAX request to get the information to show in the modal dialog and you use a regular form POST to actually update the information.

I guess that this snippet:

@RequestMapping("/updateAssignment")
public String getAssignmentById(Long id, Double rate){
    this.assignmentService.updateAssignmentById(id,rate);
    return "project-assignments";
}

should be done like this:

@PostMapping("/updateAssignment")
public String updateAssignmentById(Long id, Double rate){
    this.assignmentService.updateAssignmentById(id,rate);
    return "redirect:/getAssignmentsByProjectId?id=" + id;
}

This would redirect the user after the update to the correct URL to show the assignments for a given project id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文