简体   繁体   English

通过合并jstl中两个大小不等的列表以表格格式显示

[英]Display in a tabular format by merging two lists of unequal size in jstl

Sorry if my question is not clear. 对不起,如果我的问题不清楚。 Let me explain - I have two lists, model.List1 & model.List2 of unequal sizes . 让我解释一下-我有两个列表,model.List1和model.List2的大小不相等 However, I still need to display model.List2 in such a way that, the list elements appear under the Territory column. 但是,我仍然需要以如下方式显示model.List2,使得列表元素出现在Territory列下。 How do I merge these two lists? 如何合并这两个列表? I somehow need to iterate List2 within the block marked with ** ** 我以某种方式需要在标有** **的块内迭代List2

Edit: As you can see, List 1 has five entries while list 2 has only 3 entries. 编辑:如您所见,列表1只有5个条目,而列表2只有3个条目。 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). 如果我在第一个列表中迭代第二个列表,则对于List1的每个迭代,我将获得所有3个地区(请参阅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 SEC1-这不是我想要的

User     Title  Role      Territory


user1    Lead   Lead      Territory1 Territory2 Territory3

user2    Lead2   Lead2    Territory1 Territory2 Territory3

user3    Lead3   Lead3    Territory1 Territory2 Territory3

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. c:if检查第二个列表是否足够大。

Related: forEach's varStatus variable 相关: forEach的varStatus变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM