Reputation: 3004
In my spring web application I have arraylist in model attribute which I want to show in table format in JSP. I need to arrange the list items in the two columns dynamically. Below is code which creates only one column but I need to arrange then in even odd pair in two columns.
<table>
<c:forEach items="${artifact.answerOptions}" var="answeroption" varStatus="status">
<tr>
<td>
<form:radiobutton path="choosenAnswers" value="${answeroption}"/>
<label for="choosenAnswers" class="lowerlabel"><c:out value="${answeroption.answerText}"/></label>
</td>
</tr>
</c:forEach>
</table>
Here answerOptions is the list of AnswerOption beans which has answerText property. Above code creates table but with one column, but I need them to arrange in even odd manner like below:
<table>
<tr>
<td> List Item 1</td>
<td> List Item 2</td>
</tr>
<tr>
<td> List Item 3</td>
<td> List Item 4</td>
</tr>
<tr>
<td> List Item 5</td>
<td> List Item 6</td>
</tr>
</table>
Upvotes: 4
Views: 10690
Reputation: 1108692
Use the begin
, end
and step
attributes instead. You could let it iterate by 2 and get the list items by index directly.
<table>
<c:forEach begin="0" end="${fn:length(artifact.answerOptions)}" step="2" varStatus="loop">
<tr>
<td>${artifact.answerOptions[loop.index]}</td>
<td>${artifact.answerOptions[loop.index + 1]}</td>
</tr>
</c:forEach>
</table>
(no this doesn't throw ArrayIndexOutOfBoundsException
when you have odd amount of items)
Upvotes: 4
Reputation: 59660
AFAIK it is not possible cause in iterator at a time you will have access to only one answerOptions
object not 2 so you can not format it like this.
Upvotes: 0