1

Sorry if my question is not clear. Let me explain - I have two lists, model.List1 & model.List2 of unequal sizes. However, I still need to display model.List2 in such a way that, the list elements appear under the Territory column. How do I merge these two lists? I somehow need to iterate List2 within the block marked with ** **

Edit: As you can see, List 1 has five entries while list 2 has only 3 entries. If I iterate my second list within my first list, for each Iteration of List1, I am getting all the 3 Territories (Please see SEC1). This is not what I want..

<table>
<thead>
 **<tr>**
  <th>User</th>
  <th>Title</th>
  <th>Role</th>
  <th>Territory</th>

  **</tr>**
</thead>
<tbody>
<c:forEach items="${model.List1}" var="list">
<tr>
<td>${list.name} </td>
<td>${list.title} </td>
<td>${list.role} </td>
</tr>
</c:forEach>



<c:forEach items="${model.List2}" var="terr">
  <td>${terr}</td>      
</c:forEach> 
</tbody>

</table>

Current Output:

User     Title  Role  Territory


user1    Lead   Lead


Territory1

Desired Output:

User     Title  Role      Territory


user1    Lead   Lead      Territory1

user2    Lead2  Lead2     Territory2

user3    Lead3  Lead3     Territory3

User4    Lead4  Lead4

User5    Lead5  Lead5     

SEC1 - This is not what I want

User     Title  Role      Territory


user1    Lead   Lead      Territory1 Territory2 Territory3

user2    Lead2   Lead2    Territory1 Territory2 Territory3

user3    Lead3   Lead3    Territory1 Territory2 Territory3
user1736333
  • 183
  • 2
  • 3
  • 11

1 Answers1

0

You could do it like this

<table>
<c:forEach items="${ list1 }" var="item" varStatus="status1"> 
  <tr>
    <td>${ item.name }</td>

    <td>
      <c:if test="${ status1.index lt fn:length(list2) }">
        ${ list2[status1.index].name }
      </c:if> 
    </td>      
  </tr>
</c:forEach>
</table>

The c:if checks, if the 2nd list is large enough.

Related: forEach's varStatus variable

Community
  • 1
  • 1
Beryllium
  • 12,808
  • 10
  • 56
  • 86
  • this would print the contents in List2 X times( where X=sizeof(List2)) for a single iteration of List 1. this is not what I want. – user1736333 Aug 07 '13 at 16:43
  • @user1736333 Could you please update your question: Add more lines/users, so that we can see what you want. Do you want the output *only* in the first line? – Beryllium Aug 07 '13 at 17:10
  • Hi, thanks for helping me. When I tried your suggestion I am getting something mentioned in SEC1 of my original post. This is not what I want. Please refer desired output section! – user1736333 Aug 08 '13 at 12:27
  • @user1736333 Give me a second while I compile the new version (which includes a varStatus on the first loop, and a c:if around the access of the 2nd loop) – Beryllium Aug 08 '13 at 12:31
  • @user1736333 do you need any more help with this question? – Beryllium Aug 08 '13 at 19:22
  • @user1736333 Does the answer give the result you expect? – Beryllium Aug 13 '13 at 13:10
  • @user1736333 Of course, that's the other possibility. But this does not tell, if this answer is the solution to your original question. So it won't help other people. So please verify the code, and give a feedback. – Beryllium Aug 24 '13 at 17:16