I need display product in list into 3 columns with foreach
.
Here is my code:
<table>
<c:forEach items="${lstProduct}" var="product" varStatus="status" step="3">
<tr>
<td>
<!--product of column left will be display here -->
${product.id}
${product.name}
</td>
<td>
<!--product of column middle will be display here -->
<!--I need something like this: productMiddle = product.getNext() -->
</td>
<td>
<!--product of column right will be display here -->
<!-- productRight = productMiddle.getNext() -->
</td>
</tr>
</c:forEach>
</table>
The question is how do I get next product in list?
Skaffman has given a good answer. As an alternative, you can also just put the <tr>
outside the loop and print the intermediate </tr><tr>
s at the right moments (i.e. every 3rd item).
<table>
<tr>
<c:forEach items="${lstProduct}" var="product" varStatus="loop">
<c:if test="${not loop.first and loop.index % 3 == 0}">
</tr><tr>
</c:if>
<td>
${product.id}
${product.name}
</td>
</c:forEach>
</tr>
</table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With