Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I will give you a similar answer to the one from Evgeny but a bit more accurate. But I need to make the following assumptions:</p> <ul> <li>Each of your rows should have a rounded value of (<em>number of items / number of columns</em>) instead of (<em>length of list / total number of items</em>) since in that statement your columns will always have 1 item.</li> <li>Your JSP context has following attributes: <ul> <li><strong>values</strong>: a collection or array containing all the elements</li> <li><strong>numRows</strong>: An integer value defining the number of rows to distribute your list</li> </ul></li> </ul> <p>First of all, since you can not use any scriptlet, you will need the following taglib declarations:</p> <pre><code>&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %&gt; &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt; &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %&gt; </code></pre> <p>Now, to build your grid of items you will write something similar to this:</p> <pre><code>&lt;table&gt; &lt;c:forEach begin="0" end="${numRows - 1}" varStatus="i"&gt; &lt;c:set var="rowStart" value="${i.index * numColumns}" /&gt; &lt;tr&gt; &lt;fmt:formatNumber var="numColumns" value="${fn:length(values) / numRows}" maxFractionDigits="0" /&gt; &lt;c:forEach begin="0" end="${numColumns - 1}" varStatus="j"&gt; &lt;c:set var="index" value="${rowStart + j.index}"/&gt; &lt;td&gt; &lt;c:choose&gt; &lt;c:when test="${index lt fn:length(values)}"&gt; &lt;%-- Replace following code with the one needed to display your item --%&gt; &lt;c:out value="${values[index]}" /&gt; &lt;/c:when&gt; &lt;c:otherwise&gt;&amp;nbsp;&lt;/c:otherwise&gt; &lt;/c:choose&gt; &lt;/td&gt; &lt;/c:forEach&gt; &lt;/tr&gt; &lt;/c:forEach&gt; &lt;/table&gt; </code></pre> <p>In this example I am first rounding the value of the division to calculate the number of rows to render (see answer <a href="https://stackoverflow.com/questions/1443385/how-do-i-round-a-number-in-jstl">How do I round a number in JSTL?</a>). Then the iteration through items to generate rows and columns start almost exactly as Evgeny did. The main difference resides in that you will need to perform an index check before accessing your list and in case your index is out of bounds, just render a "<em>non breaking space</em>" so your table will be "HTML correct".</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload