2

I want to print specific length of table row where jstl c:forEach loops data exists or not. I have a list called 'myList' which has only two data. but I want to print 5 row of data, unavailable row data will remain blank. Like -

<c:forEach var="item" items="${myList}" begin="0" end="4" varStatus="loop">
<tr>
<td>${loop.count}</td>
<td>${item.name}</td>
</tr>
</c:forEach>

Output will be like -

<table>
<tr>
<td>Sl. No.</td>
<td>Name</td>
</tr>
<tr>
<td>1</td>
<td>Name1</td>
</tr>
<tr>
<td>2</td>
<td>Name2</td>
</tr>
<tr>
<td>3</td>
<td></td>
</tr>
<tr>
<td>4</td>
<td></td>
</tr>
<tr>
<td>5</td>
<td></td>
</tr>
</table>
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102

1 Answers1

2

I think this simple way will help to solve this problem. Like -

<table>

<tr>
<td>Sl. No.</td>
<td>Name</td>
</tr>

<c:set var="count" value="1"/>
<c:forEach var="item" items="${myList}">
<tr>
<td>${count}</td>
<td>${item.name}</td>
</tr>
<c:set var="count" value="${count + 1}"/>
</c:forEach>

<c:forEach begin="${count}" end="5">
<tr>
<td>${count}</td>
<td></td>
</tr>
<c:set var="count" value="${count + 1}"/>
</c:forEach>

</table>