JSTL foreach: get Next object

 ̄綄美尐妖づ 提交于 2019-12-30 03:15:13

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/5884638/jstl-foreach-get-next-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!