如何使用JSP替换HTML表行颜色? [英] How to alternate HTML table row colors using JSP?
问题描述
如何使用JSP替换HTML表格行颜色?
我的CSS看起来像:
tr.odd {background-color:#EEDDEE}
tr.even {background-color:#EEEEDD}
我想使用< c:forEach>
来遍历集合。
< c:forEach items =$ {element}var =myCollection>
< tr>
< td>< c:out value =$ {element.field}/>< / td>
...
< / tr>
< / c:forEach>
我需要一个int计数变量或boolean odd / even变量来跟踪该行。然后,我的< tr>
标签将类似于:
; tr class =odd or even取决于行>
解决方案使用
varStatus <在
forEach
标签上的code>属性,JSTL将管理javax.servlet.jsp.jstl.core.LoopTagStatus
为你指定的变量名。
然后,您可以使用三元运算符轻松输出相应的类名称:
< c:forEach items =$ {element}var =myCollectionvarStatus =loopStatus>
< tr class =$ {loopStatus.index%2 == 0?'even':'odd'}>
...
< / tr>
< / c:forEach>
此来自IBM的JSTL引物有关于
core
标记库及其提供的更多信息。 p>How do I alternate HTML table row colors using JSP?
My CSS looks something like:
tr.odd {background-color: #EEDDEE} tr.even {background-color: #EEEEDD}
I want to use
<c:forEach>
to iterate over a collection.<c:forEach items="${element}" var="myCollection"> <tr> <td><c:out value="${element.field}"/></td> ... </tr> </c:forEach>
I need an int count variable or boolean odd/even variable to track the row. Then my
<tr>
tag would look something like:<tr class="odd or even depending on the row">
解决方案Use the
varStatus
attribute on yourforEach
tag and JSTL will manage an instance of ajavax.servlet.jsp.jstl.core.LoopTagStatus
for you in the variable name you specify.You can then use a ternary operator to easily output the appropriate class name:
<c:forEach items="${element}" var="myCollection" varStatus="loopStatus"> <tr class="${loopStatus.index % 2 == 0 ? 'even' : 'odd'}"> ... </tr> </c:forEach>
This JSTL primer from IBM has more information about the
core
tag library and what it gives you.这篇关于如何使用JSP替换HTML表行颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!