在Thymeleaf表格中显示多行

huangapple go评论112阅读模式
英文:

Display multiple rows in Thymeleaf table

问题

我正试图在Spring Boot应用程序中使用Thymeleaf创建一个活动参与者表格。

我的表格如下所示:

  1. <table class="table table-bordered tabl-striped">
  2. <thead class="thead-dark">
  3. <th>First Name</th>
  4. <th>Last Name</th>
  5. </thead>
  6. <tbody>
  7. <tr th:each="tempEvent : ${event}">
  8. <td th:text="${tempEvent.clubEventAttendees[event].firstName}" />
  9. <td th:text="${tempEvent.clubEventAttendees[event].lastName}" />
  10. </tr>
  11. </tbody>
  12. </table>

如果我只使用单个td,例如 <td th:text="${tempEvent.clubEventAttendees}" />,我会得到以下结果:

  1. [EventAttendees [id=1, firstName=Paul, lastName=Jones], EventAttendees [id=2, firstName=Luke, lastName=Smyth]

这个输出包含了正确的数据。然而,上面的表格只输出了第二个参与者(Luke Smyth)。如果我将td的行更改为:

  1. <td th:text="${tempEvent.clubEventAttendees[0].firstName}" />
  2. <td th:text="${tempEvent.clubEventAttendees[0].lastName}" />

或者

  1. <td th:text="${tempEvent.clubEventAttendees[1].firstName}" />
  2. <td th:text="${tempEvent.clubEventAttendees[1].lastName}" />

根据位置显示了正确的人员。我如何更改我的表格以显示返回的任何参与者?

这是我的Event模型:

  1. @Entity
  2. @Table(name="club_events")
  3. public class Event {
  4. @OneToMany(mappedBy="clubEvent",
  5. cascade={CascadeType.PERSIST, CascadeType.MERGE,
  6. CascadeType.DETACH, CascadeType.REFRESH,
  7. CascadeType.REMOVE})
  8. private List<EventAttendees> clubEventAttendees;
  9. // 定义字段
  10. @Id
  11. @GeneratedValue(strategy=GenerationType.IDENTITY)
  12. @Column(name="id")
  13. private int id;
  14. @Column(name="name")
  15. private String name;
  16. @Column(name="location")
  17. private String location;
  18. @Column(name="description")
  19. private String description;
  20. @DateTimeFormat(pattern="yyyy-MM-dd")
  21. @Column(name="date_time")
  22. private LocalDate dateTime;
  23. public Event() {
  24. }
  25. public Event(int id, String name, String location, String description, LocalDate dateTime) {
  26. super();
  27. this.id = id;
  28. this.name = name;
  29. this.location = location;
  30. this.description = description;
  31. this.dateTime = dateTime;
  32. }
  33. }

这是我的EventAttendees模型:

  1. @Entity
  2. @Table(name="club_event_attendees")
  3. public class EventAttendees {
  4. @ManyToOne(cascade= {CascadeType.PERSIST, CascadeType.MERGE,
  5. CascadeType.DETACH, CascadeType.REFRESH})
  6. @JoinColumn(name="club_event_id")
  7. private Event clubEvent;
  8. // 定义字段
  9. @Id
  10. @GeneratedValue(strategy=GenerationType.IDENTITY)
  11. @Column(name="id")
  12. private int id;
  13. @Column(name="first_name")
  14. private String firstName;
  15. @Column(name="last_name")
  16. private String lastName;
  17. @Column(name="phone")
  18. private String phone;
  19. @Column(name="mobile")
  20. private String mobile;
  21. public EventAttendees() {
  22. }
  23. public EventAttendees(int id, String firstName, String lastName, String phone, String mobile, int clubEventId) {
  24. super();
  25. this.id = id;
  26. this.firstName = firstName;
  27. this.lastName = lastName;
  28. this.phone = phone;
  29. this.mobile = mobile;
  30. }
  31. }

注意:已删除getter和setter方法。

我目前有一个页面列出了所有事件(在一个表格中):

在Thymeleaf表格中显示多行

一旦我弄清楚了这个问题,"Attendees" 列将会被移除,但如果点击 "View Attendees" 按钮,我希望被带到一个包含类似表格的页面,其中包含所有参与者的firstName、lastName、phone和mobile信息。基本上,我想将 clubEventAttendees 中返回的数据显示在类似上面表格的页面中。

英文:

I'm trying to create a table of attendees of my event using Thymeleaf on a Spring Boot app.

My table looks like this:

  1. &lt;table class=&quot;table table-bordered tabl-striped&quot;&gt;
  2. &lt;thead class=&quot;thead-dark&quot;&gt;
  3. &lt;th&gt;First Name&lt;/th&gt;
  4. &lt;th&gt;Last Name&lt;/th&gt;
  5. &lt;/thead&gt;
  6. &lt;tbody&gt;
  7. &lt;tr th:each=&quot;tempEvent : ${event}&quot;&gt;
  8. &lt;td th:text=&quot;${tempEvent.clubEventAttendees[event].firstName}&quot; /&gt;
  9. &lt;td th:text=&quot;${tempEvent.clubEventAttendees[event].lastName}&quot; /&gt;
  10. &lt;/tr&gt;
  11. &lt;/tbody&gt;
  12. &lt;/table&gt;

If I just use a single td with &lt;td th:text=&quot;${tempEvent.clubEventAttendees}&quot; /&gt; I get this:

  1. [EventAttendees [id=1, firstName=Paul, lastName=Jones], EventAttendees [id=2, firstName=Luke, lastName=Smyth]

This output contains the correct data. The above table however only outputs my second attendee(Luke Smyth). If I change the td lines to:

  1. &lt;td th:text=&quot;${tempEvent.clubEventAttendees[0].firstName}&quot; /&gt;
  2. &lt;td th:text=&quot;${tempEvent.clubEventAttendees[0].lastName}&quot; /&gt;

or

  1. &lt;td th:text=&quot;${tempEvent.clubEventAttendees[1].firstName}&quot; /&gt;
  2. &lt;td th:text=&quot;${tempEvent.clubEventAttendees[1].lastName}&quot; /&gt;

The correct person is displayed depending on their position. How can I change my table to display any attendees that get returned?

This is my Event model:

  1. @Entity
  2. @Table(name=&quot;club_events&quot;)
  3. public class Event {
  4. @OneToMany(mappedBy=&quot;clubEvent&quot;,
  5. cascade={CascadeType.PERSIST, CascadeType.MERGE,
  6. CascadeType.DETACH, CascadeType.REFRESH,
  7. CascadeType.REMOVE})
  8. private List&lt;EventAttendees&gt; clubEventAttendees;
  9. // define fiends
  10. @Id
  11. @GeneratedValue(strategy=GenerationType.IDENTITY)
  12. @Column(name=&quot;id&quot;)
  13. private int id;
  14. @Column(name=&quot;name&quot;)
  15. private String name;
  16. @Column(name=&quot;location&quot;)
  17. private String location;
  18. @Column(name=&quot;description&quot;)
  19. private String description;
  20. @DateTimeFormat(pattern=&quot;yyyy-MM-dd&quot;)
  21. @Column(name=&quot;date_time&quot;)
  22. private LocalDate dateTime;
  23. public Event() {
  24. }
  25. public Event(int id, String name, String location, String description, LocalDate dateTime) {
  26. super();
  27. this.id = id;
  28. this.name = name;
  29. this.location = location;
  30. this.description = description;
  31. this.dateTime = dateTime;
  32. }
  33. }

This is my EventAttendees model:

  1. @Entity
  2. @Table(name=&quot;club_event_attendees&quot;)
  3. public class EventAttendees {
  4. @ManyToOne(cascade= {CascadeType.PERSIST, CascadeType.MERGE,
  5. CascadeType.DETACH, CascadeType.REFRESH})
  6. @JoinColumn(name=&quot;club_event_id&quot;)
  7. private Event clubEvent;
  8. // define fiends
  9. @Id
  10. @GeneratedValue(strategy=GenerationType.IDENTITY)
  11. @Column(name=&quot;id&quot;)
  12. private int id;
  13. @Column(name=&quot;first_name&quot;)
  14. private String firstName;
  15. @Column(name=&quot;last_name&quot;)
  16. private String lastName;
  17. @Column(name=&quot;phone&quot;)
  18. private String phone;
  19. @Column(name=&quot;mobile&quot;)
  20. private String mobile;
  21. public EventAttendees() {
  22. }
  23. public EventAttendees(int id, String firstName, String lastName, String phone, String mobile, int clubEventId) {
  24. super();
  25. this.id = id;
  26. this.firstName = firstName;
  27. this.lastName = lastName;
  28. this.phone = phone;
  29. this.mobile = mobile;
  30. }
  31. }

Note: getters and setters have been removed.

I currently have a page which lists all events(in a table):
在Thymeleaf表格中显示多行

The Attendees column will be removed once I get this figured out but if the View Attendees button is clicked I'd like to be taken to a page containing a similar table but with the firstName, lastName, phone and mobile of all attendees. Basically, I'd like to take the data returned in clubEventAttendees and display it in a table similar to the table above.

答案1

得分: 1

好的,以下是您要翻译的内容:

好的,如承诺所示,这是代码。

我可以解释一下关于 PersonCar 的示例。想法是 Person 拥有 List<Car>

在Thymeleaf表格中显示多行

在点击 View Cars 后,会打开一个新页面,您会看到一个汽车列表。

在Thymeleaf表格中显示多行

实现这个的方法相当简单。在从 modelAttributepeople 渲染 List<Person> 时,您可以使用以下代码

  1. <tr th:each="person : ${people}">
  2. <td th:text="${person.id}"></td>
  3. <td th:text="${person.firstName}"></td>
  4. <td th:text="${person.lastName}"></td>
  5. <td>
  6. <a th:href="@{'/person/' + ${person.id} + '/cars'}">View Cars</a>
  7. </td>
  8. </tr>

th:href="{'/person/' + ${person.id} + '/cars'}" 中的 Href 会执行控制器中的以下方法

  1. @GetMapping("/person/{personId}/cars")
  2. public String getCars(Model model, @PathVariable Integer personId){
  3. model.addAttribute("cars", carRepository.findAllByPersonId(personId));
  4. return "web/car-list";
  5. }

之后会渲染一个新的模板。我在 GitHub 上上传了源代码,可以在这里找到:https://github.com/Prebiusta/thymeleaf-playground

希望对您有所帮助,如果还有其他问题,请告诉我。

英文:

Okay so as promised, here is the code.

I can explain the example with Person and Car. The idea is that Person has List&lt;Car&gt;.

在Thymeleaf表格中显示多行

After pressing on View Cars, a new page is opened, where you see a list of Cars.

在Thymeleaf表格中显示多行

How to achieve this is fairly simple. When rendering List&lt;Person&gt; from modelAttribute people, you can use the following code

  1. &lt;tr th:each=&quot;person : ${people}&quot;&gt;
  2. &lt;td th:text=&quot;${person.id}&quot;&gt;&lt;/td&gt;
  3. &lt;td th:text=&quot;${person.firstName}&quot;&gt;&lt;/td&gt;
  4. &lt;td th:text=&quot;${person.lastName}&quot;&gt;&lt;/td&gt;
  5. &lt;td&gt;
  6. &lt;a th:href=&quot;@{&#39;/person/&#39; + ${person.id} + &#39;/cars&#39;}&quot;&gt;View Cars&lt;/a&gt;
  7. &lt;/td&gt;
  8. &lt;/tr&gt;

Href from th:href=&quot;@{&#39;/person/&#39; + ${person.id} + &#39;/cars&#39;}&quot; executed following method in controller

  1. @GetMapping(&quot;/person/{personId}/cars&quot;)
  2. public String getCars(Model model, @PathVariable Integer personId){
  3. model.addAttribute(&quot;cars&quot;, carRepository.findAllByPersonId(personId));
  4. return &quot;web/car-list&quot;;
  5. }

after which a new template is rendered. I uploaded source code on GitHub, it can be found here https://github.com/Prebiusta/thymeleaf-playground

Hope it helps, let me know if there is something else.

huangapple
  • 本文由 发表于 2020年10月16日 05:18:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64379806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定