jsp页面中的Foreach循环 - 结束和继续列表

时间:2015-04-09 04:30:06

标签: jsp jstl jsp-tags

我只是想知道如何结束特定列中的项目列表,并在单独的列中继续其余项目。例如:在第一列中,有些行的值为" item1"," item2"," item3"在第二栏作为延续,有值" item4"," item5"," item6"。

实际上,我希望连续4列各有5个项目。如何在jsp页面中使用foreach循环在下面的代码中应用它?

<table>
    <c:forEach items="${itemList}" var="item"> 
        <tr>
            <td><input type ="submit" class="Items btnColor" 
                       value="${item.itemName}" label ="${item.itemId}" />
            </td>
        </tr>
    </c:forEach>
</table>    

请帮助,非常感谢!

1 个答案:

答案 0 :(得分:0)

您可以使用varStatus变量来确定循环的索引,并使用if条件跳过该迭代,如:

假设您的表有5列,您必须跳过第3列,然后

<c:set value="2" var="firstIndex" />    // this is the columnIndex which is not required
<c:set value="5" var="splitColumn" />   // thi is the total length of arraylist
    <table>
        <c:forEach items="${itemList}" var="item" varStatus="itemLoop">
           <c:if test="${not (itemLoop.count eq firstIndex)}"> // this condition will skip the third column elements indexes from your arraylist.
             <c:set value="${firstVal+5}" var="firstIndex" /> // this is increment in you arraylist index for all values ocurring at 3rd column
             <c:if test="${(itemLoop.count eq splitColumn)}"> // this condition will create a row element after every 5 elements read from array list.
               <tr>
             </c:if>
                <td><input type ="submit" class="Items btnColor" 
                           value="${item.itemName}" label ="${item.itemId}" />
                </td>
              <c:if test="${(itemLoop.count eq splitColumn)}"> // increment for the no of rows to split after very 5 elements
                <c:set value=${splitColumn+5} var="splitColumn" />
                </tr>
              <c:if/>
          </c:if>
        </c:forEach>
    </table> 
相关问题