Search code examples
htmlloopsjstl

How to iterate data horizontally in jstl


I want to iterate over 10 rows of data in a table but horizontally.

<table>
 <c:forEach var="i" begin="1" end="10">
  <tr>
    <td>${i}</td> <td>${i}</td> <td>${i}</td>
  </tr>
 </c:forEach>
</table>

This code displays data like this:

1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
...

but I want to iterate over it like this:

1 2 3
4 5 6
7 8 9
10

How to do this ? Please help me out from this. I am stuck here. Thanks.


Solution

  • I'm not near a computer to check, but it's going to be something like:

    <table>
        <tr>
            <c:forEach var="i" begin="1" end="10"> 
                <td>${i}</td>
                <c:if test="${(i mod 3) == 0}"> <%-- New row if 3rd column --%>
                    </tr><tr>
                </c:if>
            </c:forEach>
        </tr>
    </table>