Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSTL foreach: get Next object

Tags:

jsp

jstl

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?

like image 324
ductran Avatar asked May 04 '11 14:05

ductran


1 Answers

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>
like image 121
BalusC Avatar answered Nov 03 '22 02:11

BalusC