2014-04-21 39 views
2

下面我有打印出第一張圖片的JSP代碼。我正在努力實現第二張照片中的內容。JSP - 如何在值重複時打印出空的​​?

我總共有8列。只要有來自內部連接表的數據(包括最後4列數據將發生變化),就會打印前4列。我正在尋找在上面複製信息的列和行中打印出空的數據或空間。每個ID最多可以有6行(最左邊的列)。

<table border="1" width="100%"> 
<tr> 
    <th>Student ID</th> 
    <th>Last Name</th> 
    <th>First Name</th> 
    <th>Grade</th> 
    <th>Events</th> 
    <th>Participation Level</th> 
    <th>Ind/Team</th> 
    <th>Event Name</th>  
</tr> 

<c:forEach var="row" items="${result.rows}"> 


    <% theStudentId = 123 ; %> 
    <% System.out.println("at " + (new java.util.Date()) + ", theStudentId is " + theStudentId); %> 

    <tr> 
    <td><c:out value="${row.idStudents}"/></td> 
    <td><c:out value="${row.LastName}"/></td> 
    <td><c:out value="${row.FirstName}"/></td> 
    <td><c:out value="${row.Grade}"/></td> 
    <td><c:out value="${row.idEvents}"/></td> 
    <td><c:out value="${row.Grade}"/></td> 
    <td><c:out value="${row.Type}"/></td> 
    <td><c:out value="${row.Name}"/></td> 
    </tr> 

</c:forEach> 

這就是JSP打印出截至目前。

current

這是我想達到的目的。

target

回答

1

你需要跟蹤的最後一個值的。就在這個部分之前:

<c:forEach var="row" items="${result.rows}"> 

你應該創建一個名爲lastStudentId變量(最初爲null)。

在循環內部,檢查當前學生ID是否等於lastStudentId。 (我不知道該怎麼做,因爲你沒有提供這些字段的類型信息,我假設它們是字符串)。如果它與最後一張相同,則打印空的td(例如包含一個& nbsp;)。如果不一樣,那就照你平時的做。

在循環底部,分配lastStudentId = row.idStudents,你很好走。

+0

非常感謝! – cstorer