Search code examples
javamysqllistjspjstl

Displaying data from 3 different list in one table in jsp/jstl


I have three lists. Employee , skillsets , EmployeeSkillsetsRating.

I wish to print a table as below, where the blank spaces will have their rating in that skillset.

            EmployeeName1   EmployeeName2   EmployeeName3

Skillset1
Skillset2
Skillset3
Skillset4
Skillset5

Now i am able to print all the Employee names as first row successfully using 'Employee' list. Also I am able to print all Skillsets as first column successfully using 'Skillset' list. Following is the working code

        <table border="1">

        <th>Activities</th>
        <c:forEach var="employee" items="${listEmployee}">
            <th>
                <td>${employee.name}</td>
            </th>
        </c:forEach>
        <c:forEach var="activity" items="${listActivity}" >
            <tr>
                <td>${activity.activityDetails}</td>
            </tr>
        </c:forEach>
    </table>

Now the rating for employees is coming from Rating List which also contain EmployeeID from EmployeeList and SkillsetID from Skillset List. How do I print rating in the table by matching both employee name and skillset.


Solution

  • How about something like this:

        <th>Activities</th>
        <c:forEach var="employee" items="${listEmployee}">
            <th>
                <td>${employee.name}</td>
            </th>
        </c:forEach>
        <c:forEach var="activity" items="${listActivity}" >
            <tr>
                <td>${activity.activityDetails}</td>
               <c:forEach var="employee" items="${listEmployee}">
                  <c:forEach var="rating" items="${listRating}">
                     <c:if test = "${rating.employee==employee.ratingDetails && rating.activity==activity.activityDetails)">
                          <td>${rating.ratingDetails}</td>
                   </c:if>
    
                  </c:forEach>
               </c:forEach>
    
            </tr>
        </c:forEach>
    </table>
    

    For every activity you loop through each employee and if you find rating for that employee and activity in the ratings list -you print it as a column value.

    There must a rating for every employee in the ratings list.