I want to loop a table of records for printing, based on the following condition:
If the number of records is more than 35, I will need to pause the loop, insert a footer, and a new header for the next page and continue its count till the last record.
The condition here is to use only jsp classic scriplet.
Here is what I have and I am stuck: (in pseudo code format)
<% int j=0;
for(int i=0; i < list.size(); i++){
col1 = list.get(i).getItem1();
col2 = list.get(i).getItem2();
col3 = list.get(i).getItem3();
j++;
if (j==35) {%> // stops to render footer and next page's header
</table>
<table>
<!-- footer contents -->
</table>
<table>
<!-- header for next page -->
</table>
<%}%>
<tr><td><%=col1%></td><td><%=col1%></td><td><%=col1%></td></tr>
<%}%>
the problem with this model is that if I use a break inside this if, I'd stop the loop and I can't loop from record #36 to end of record. How do I go about doing this?
If you don't want to use proper pagination then use JSTL as below. Easier to read than scrip-lets as well, apart from the obvious benefits.
Use an
if (i % 35 == 0)
to write the footer and then validate if there are more elements in the list so you would have to add a new table and its header. The code would look like this:Note that in this code sample I'm using
Iterator
instead ofList#get(int index)
since if yourList
is aLinkedList
internally it would need to traverse all the elements until reach the element on the desired index (in this case,i
). With this implementation, your code is even cleaner.