【问题标题】:arranging dynamic list in jsp page in the specified structure using table使用表格在指定结构的jsp页面中排列动态列表
【发布时间】:2013-02-28 14:26:27
【问题描述】:

我有一个动态生成的列表。我希望这个列表应该使用表格显示。结构应该如下

假设如果我有 6 个值,前 3 个值应该在第一行,第二个 3 应该在第二行 我怎样才能动态地做到这一点

<table>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>

上面的 tr 结构应该每 3 个元素重复一次

我尝试使用 divs.its 工作,但我希望使用 table 来实现相同的功能。 帮帮我。

问候, 拉维。

我的意思是该列表是 bean 的集合。在每个 bean 中我都有一些值。所以我的表如下所示

这是正在迭代的列表

<c:forEach value="#{theList}" var="item">

<div class="customer">
<p>${item.field1}</p>
<p>${item.field1}</p>
</div>

</c:foreach>

从上面的 div 客户应该是如下结构

  • 前 3 个 bean 应该在第一行 3 列
  • 第二个 3 个 bean 应该在第二行 3 列
  • 继续...

【问题讨论】:

    标签: java jsp jstl


    【解决方案1】:

    使用 JSTL:

    <table>
        <c:forEach items="#{theList}" var="item" varStatus="i">
        <c:if test="${i.index % 3 == 0 or i.begin}">
        <tr>
        </c:if>
            <td>${item.field}</td>
        <c:if test="${i.index % 3 == 0 or i.last}">
        </tr>
        </c:if>
        </c:forEach>
    </table>
    

    【讨论】:

    • 我了解到您想每 3 次打印一个表格(完成),然后您想打印第一行的前 3 个字段(完成)和第二行的接下来的 3 个字段(完毕)。最后,我使用相同的测试来检查表是否应该关闭。
    【解决方案2】:

    虽然我还没有测试过,但这样的东西可能会起作用。

    <table>
        <tr>
            <c:forEach var="item" items="${yourList}" varStatus="loopStatus">
                <c:if test="${loopStatus.index % 3 == 0 }"><tr></c:if>
                    <td><c:out value="${item}"/></td>
                <c:if test="${loopStatus.index % 3 == 0 }"></tr></c:if> 
            </c:forEach>
            <c:if test="${yourList.size % 3 != 0 }"></tr></c:if>
    </table>
    

    【讨论】:

      【解决方案3】:
      <c:forEach items="${loopableObject}" var="theObject" varStatus="theCount">
       <table>
        <tr>
          <c:if test='${(status.index) lt 3}'>
      
          <td>${item.field1}</td>
          <td>${item.field2}</td>
          <td>${item.field3}</td>
      
         </c:if>
       </tr>
      
       <tr>
         <c:if test='${(status.index) gt 2}'>
          <td>${item.field1}</td>
          <td>${item.field2}</td>
          <td>${item.field3}</td>
      
         </c:if>
      </tr>
      </table>
      </c:forEach>
      

      【讨论】:

      • lt 表示小于,2的模数始终为0或1。
      • 我猜是 OP 决定哪个答案更好
      • @LuiggiMendoza 查看更新后的问题,我不希望桌子出现。对于每个 tr 标签,应该有 3 个 td,并且 tr 应该根据列表的大小来决定。
      猜你喜欢
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      • 2015-09-09
      相关资源
      最近更新 更多