仅显示偶数项的表格的替代行颜色
我正在使用 JSTL 并排显示两个表中的数据。为此,我检查偶数和奇数,并在一个表中显示奇数列表,在另一个表中显示偶数列表。
现在,对于具有偶数记录的表,我想将交替行设置为白色和黑色。你能让我知道我该怎么做吗?
我也尝试使用 JavaScript 来获取表格的 ID,然后分配颜色,但它不起作用。
<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td width="50%">
<!-- Table on left side -->
<table id="tabbed" width="100%" border="1" cellspacing="0" cellpadding="1">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Description</th>
<th>Disp Ind</th>
</tr>
</thead>
<c:forEach items="${dynaItemGroupDetailListForm.map.itemsList}" var="item" varStatus="rowstatus">
<tr>
<c:choose>
<c:when test="${rowstatus.count % 2 == 1}">
<td><c:out value="${item.itemRefId}" /></td>
<td><c:out value="${item.itemNm}" /></td>
<td><c:out value="${item.itemDesc}" /></td>
<td><c:out value="${item.itemDisplayInd}" /></td>
</c:when>
</c:choose>
</tr>
</c:forEach>
</table>
</td>
<td width="50%">
<!-- Table on right side -->
<table width="100%" border="1" cellspacing="0" cellpadding="1">
<tr class="even">
<th>Id</th>
<th>Name</th>
<th>Description</th>
<th>Disp Ind</th>
</tr>
<c:forEach items="${dynaItemGroupDetailListForm.map.itemsList}" var="item" varStatus="rowstatus">
<tr>
<c:choose>
<c:when test="${rowstatus.count % 2 == 0}">
<td><c:out value="${item.itemRefId}" /></td>
<td><c:out value="${item.itemNm}" /></td>
<td><c:out value="${item.itemDesc}" /></td>
<td><c:out value="${item.itemDisplayInd}" /></td>
</c:when>
</c:choose>
</tr>
</c:forEach>
</table>
</td>
</tr>
</table>
I am using JSTL and displaying the data in two tables side by side. To do that I am checking for even and odd and displaying odd list in one table and even in another.
Now for the table which has even records, I want to color the alternate row white and black. Can you let me know how I can do that?
I tried JavaScript as well to get the ID of the table and then assigning the color but it's not working.
<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td width="50%">
<!-- Table on left side -->
<table id="tabbed" width="100%" border="1" cellspacing="0" cellpadding="1">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Description</th>
<th>Disp Ind</th>
</tr>
</thead>
<c:forEach items="${dynaItemGroupDetailListForm.map.itemsList}" var="item" varStatus="rowstatus">
<tr>
<c:choose>
<c:when test="${rowstatus.count % 2 == 1}">
<td><c:out value="${item.itemRefId}" /></td>
<td><c:out value="${item.itemNm}" /></td>
<td><c:out value="${item.itemDesc}" /></td>
<td><c:out value="${item.itemDisplayInd}" /></td>
</c:when>
</c:choose>
</tr>
</c:forEach>
</table>
</td>
<td width="50%">
<!-- Table on right side -->
<table width="100%" border="1" cellspacing="0" cellpadding="1">
<tr class="even">
<th>Id</th>
<th>Name</th>
<th>Description</th>
<th>Disp Ind</th>
</tr>
<c:forEach items="${dynaItemGroupDetailListForm.map.itemsList}" var="item" varStatus="rowstatus">
<tr>
<c:choose>
<c:when test="${rowstatus.count % 2 == 0}">
<td><c:out value="${item.itemRefId}" /></td>
<td><c:out value="${item.itemNm}" /></td>
<td><c:out value="${item.itemDesc}" /></td>
<td><c:out value="${item.itemDisplayInd}" /></td>
</c:when>
</c:choose>
</tr>
</c:forEach>
</table>
</td>
</tr>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,该表(第二个表,位于右侧)包含第 2、4、6 行、 8、 10、 12 等,并且您想要将 2、 6、 10 等着色为白色,将 4、 8、 12 等着色为黑色。
换句话说,如果
count % 4 == 0
,那么它应该是黑色,否则它应该是白色。最好通过在 EL 表达式中使用条件运算符?:
设置类名来完成。
因此,应该执行以下操作:
使用此 CSS:
请注意,我用简单的
替换了笨拙的
。 code> 并修复了其不合逻辑的位置(您最初的尝试将呈现无效的空元素)。
So, that table (the second table, which is on the right side) contains rows 2, 4, 6, 8, 10, 12, etc and you want to color 2, 6, 10, etc white and 4, 8, 12, etc black.
In other words, if
count % 4 == 0
, then it should be black, else it should be white. This is best to be done by setting the<tr class>
classname with a conditional operator?:
in the EL experssion.So, the following should do:
with this CSS:
Note that I replaced the clumsy
<c:choose><c:when>
by a simple<c:if>
and fixed its illogical place as well (your original attempt would render empty<tr>
elements which is invalid).