开发者

JSTL foreach: get Next object

开发者 https://www.devze.com 2023-03-02 15:20 出处:网络
I need display product in list into 3 columns with foreach. Here is my code:开发者_开发知识库 <table>

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号