Home > Mobile >  How to loop in thymeleaf using 2 different tables?
How to loop in thymeleaf using 2 different tables?

Time:10-10

I looked a lot for an answer and i couldn't find any

I have 2 tables Students and Meetings, connected by the student id

I want to show different columns on a table using thymeleaf to show firstname and lastname from student table, and other fields like date and reading level from meetings table

Student Class

package com.myprojects.takenotewebapp.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "students")
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Column(name = "first_name")
    private String firstName;
    @Column(name = "last_name")
    private String lastName;
    @OneToMany(mappedBy = "student", cascade = CascadeType.ALL)
    private List<Meeting> meetings = new ArrayList<Meeting>();

    public Student(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

Meeting class

package com.myprojects.takenotewebapp.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.time.LocalDate;
import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@IdClass(MeetingId.class)
@Table(name = "meetings")
public class Meeting {
    @Id
    private LocalDate date;
    @Id
    private String subject;
    @Id
    private String type;
//    @EmbeddedId
    private MeetingId meetingId;
    private Character subjectLevel;
    private String strength;
    private String teachingPoint;
    private String nextStep;
    @ManyToOne(cascade = CascadeType.ALL)
    private Student student;
}

Controller method

    public String viewAllStudentsPage(Model model, Student student) {
        model.addAttribute("listStudents", studentService.getAllStudents());
        model.addAttribute("listMeetings", meetingService.getAllMeetings());
        return "students";
    }

This is the thymeleaf code that is wrong at the moment and looping twice for the meeting fields within the same table

 <th:block th:each="student : ${listStudents}">
                <tr th:each="student : ${listStudents}">
                    <td th:text="${student.firstName}"></td>
                    <td th:text="${student.lastName}"></td>
                    <th:block th:each="meeting : ${listMeetings}">
                        <td th:text="${meeting.subjectLevel}"></td>
                        <td th:text="${meeting.type}"></td>
                    </th:block>
                    <td>
                        <a th:href="@{/showUpdateForm/{id}(id=${student.id})}" >Update</a>
                        <a th:href="@{/deleteStudent/{id}(id=${student.id})}" >Delete</a>
                    </td>
                </tr>
            </th:block>

CodePudding user response:

  1. Create setter and getter in every class:

        <table>
            <tr th:each="post : ${postList}">
                <td th:if="${post.isPublished()}">
                    <h1 th:text="${post.getTitle()}"></h1>
                    <p th:text="${post.getExcerpt()}"></p>
                    <p th:text=" 'Author : '   ${post.getAuthor()}"></p>
                    <p th:text="'Published At : '   ${post.getPublishedAt()}"></p>
                    <div >
                        <p>Tags :</p>
                        <p id="tags" th:each="tag :${post.getTags()}"
                            th:text="${tag.getName()}"></p>
                        <br>
                    </div> <a th:href="@{/posts/{id}(id=${post.getId()})}">read more</a>
                </td>
            </tr>
        </table>

this the example of nested loop in thymeleaf it will help you tags is a list in post class.

CodePudding user response:

I found a way to what i needed by looping through the students and then looping through the student.meetings

'<th:block th:each="student : ${listStudents}">
                <tr th:each="meeting, iterStat : ${student.meetings}" th:if="${iterStat.last}">
                    <td>
                        <a th:text="${student.firstName   ' '   student.lastName}"
                           th:href="@{/notebook/student/{id} (id=${student.id})}"></a>
                    </td>
                    <td th:text="${meeting.subjectLevel}"></td>
                    <td th:text="${meeting.date}" th:if="${meeting.subject=='reading'}"></td>
                    <td th:unless="${meeting.subject=='reading'}"></td>
                    <td th:text="${meeting.date}" th:if="${meeting.subject=='writing'}"></td>
                    <td th:unless="${meeting.subject=='writing'}"></td>
                    <td>
                        <div >
                            <a th:href="@{/showUpdateForm/{id}(id=${student.id})}"
                               ><span
                                    >edit_square</span></a>
                            <a th:href="@{/deleteStudent/{id}(id=${student.id})}"
                               ><span
                                    >delete</span></a>
                        </div>
                    </td>
                </tr>
            </th:block>'
  • Related