代码之家  ›  专栏  ›  技术社区  ›  andy boot

使用JSTL构建表

  •  2
  • andy boot  · 技术社区  · 16 年前

    我正在使用这个JSTL代码生成一个HTML表。每隔一行都有一个不同的类,所以我们可以给桌子加条纹。我知道我们可以用CSS3轻松做到这一点,但我必须支持旧浏览器。

    <c:set var="oddEven" value="true" />
    <c:forEach var="row" items="${rows}">
        <c:choose>
            <c:when test="${oddEven}">
            <tr>
            </c:when>
    
            <c:otherwise>
            <tr class="odd">
            </c:otherwise>
        </c:choose>
                <td>${row.value1}</td>
                <td>${row.value2}</td>
            </tr>
        <c:set var="oddEven" value="${!oddEven}" />
    </c:forEach>
    
    3 回复  |  直到 16 年前
        1
  •  3
  •   geowa4    16 年前

    这应该可以做到:

    <c:forEach var="row" items="${rows}" varStatus="status">
        <tr
          <c:if test="${status.count % 2 ne 0}">
            class="odd"
          </c:if>
        >
          <td>...</td>
        </tr>
    </c:forEach>
    

    我用 status.count 在这个例子中; count status.index .

        2
  •  1
  •   Relefant    14 年前

    尝试

    <tr class="${(status.index % 2) == 0 ? 'oddRow' : 'evenRow'}">
    
        3
  •  0
  •   Pierre    16 年前

    我不是JSP专家,但也许你可以写一个 自定义jsp标记 这样做?