How to render a 2-column table as a 4-column table with JSTL c:forEach

Viewed 127

Here I am seeing table with 50 rows and 2 columns but I want to make it 25 rows with 4 columns how can i do so?

<table class="table table-striped">
    <tr>
        <th>State Name</th>
        <th>Number of users</th>
                                                                
    </tr>
<c:forEach items="${stateuserCount}" var="obj"> 
      <tr >
      
        <td>${obj.stateName}</td>
        <td>${obj.appCount}</td> 
        
      </tr> 
    </c:forEach>
 </table>

Instead of every state showing in same row and column I want it to display under two different table with half state in one side and other half on other.

enter image description here

1 Answers

You can add 2 more columns to the same table

<%
    int totalRows = stateuserCount.length/2;
%>
<table class="table table-striped">
    <tr>
        <th>State Name</th>
        <th>Number of users</th>
        <th>State Name</th>
        <th>Number of users</th>                                                                
    </tr>

<c:forEach var="i" begin="0" end="${totalRows - 1}"> 
      <tr >
      
        <td>${stateuserCount[i].stateName}</td>
        <td>${stateuserCount[i].appCount}</td> 
        <td>${stateuserCount[i+totalRows].stateName}</td>
        <td>${stateuserCount[i+totalRows].appCount}</td>         
      </tr> 
    </c:forEach>
 </table>

Or you could have two tables side-by-side with similar logic. You just need to output stateuserCount[i+totalRows] for the second table.

Related