先看效果:
关键代码:
<span style="font-size:18px;"> <div id="tableWrap" class="tableWrap">
<div id="fixTableHeadWrap" class="fixTableHeadWrap">
<table id="fixTableHead" class="fixTableHead table" width="100%">
<tr bgcolor="#f6f8fa" height="35">
<th width="30%">烟叶名称</th>
<th width="30%">原烟产地</th>
<th width="25%">原烟等级</th>
<td width="15%">操作</td>
</tr>
</table>
</div>
<div id="fixTableBodyWrap" class="fixTableBodyWrap">
<table id="fixTableBody" class="table" width="100%">
<tr height="35">
<td width="30%">烟叶名称</td>
<th width="30%">原烟产地</th>
<th width="25%">原烟等级</th>
<td width="15%">操作</td>
</tr>
<!-- outer loop start -->
<c:forEach var="mp" items="${mainList}">
<!-- inner loop start -->
<c:forEach var="sp" items="${mp.cbList}" varStatus="status">
<tr data-zbdm="${mp.zbdm}">
<c:if test="${status.index == 0 }">
<td width="30%" rowspan="${fn:length(mp.cbList)}">${mp.cdmc} ${mp.djmc}</td>
</c:if>
<c:if test="${status.index % 2 == 1 }">
<td bgcolor="#f6f8fa" align="left" style="padding-left:30px;">${sp.cdmc}</td>
<td bgcolor="#f6f8fa">${sp.djmc}</td>
</c:if>
<c:if test="${status.index % 2 == 0 }">
<td align="left" style="padding-left:30px;">${sp.cdmc}</td>
<td>${sp.djmc}</td>
</c:if>
<c:if test="${status.index == 0 }">
<td rowspan="${fn:length(mp.cbList)}"><button data-zbdm="${mp.zbdm}" class="button delete">删除 </button></td>
</c:if>
</tr>
</c:forEach>
<!-- inner loop end -->
</c:forEach>
<!-- outer loop end -->
<!-- no data -->
<c:if test="${fn:length(mainList) == 0}">
<tr><td colspan="4">对不起,没有数据!</td></tr>
</c:if>
</table>
</div></span>
关键点解析:
1:fixTableHeadWrap 是为了实现一个 仿 excel 冻结首行 的效果;
2: <!-- outer loop start --> .... ...<!-- outer loop end --> 为循环部分, 也是本文重点;
注意:此处引用了 jstl, 来做循环
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
课外拓展:
///课外拓展 start
<span style="font-size:18px;"><html>
<body>
<h4>横跨两列的单元格:</h4>
<table border="1">
<tr>
<th>姓名</th>
<th colspan="2">电话</th>
</tr>
<tr>
<td>Bill Gates</td>
<td>555 77 854</td>
<td>555 77 855</td>
</tr>
</table>
<h4>横跨两行的单元格:</h4>
<table border="1">
<tr>
<th>姓名</th>
<td>Bill Gates</td>
</tr>
<tr>
<th rowspan="2">电话</th>
<td>555 77 854</td>
</tr>
<tr>
<td>555 77 855</td>
</tr>
</table>
</body>
</html></span>
结果:
横跨两列的单元格:
姓名 | 电话 | |
---|---|---|
Bill Gates | 555 77 854 | 555 77 855 |
横跨两行的单元格:
姓名 | Bill Gates |
---|---|
电话 | 555 77 854 |
555 77 855 |
///课外拓展 end
3: ${fn:length(mp.cbList)} 是 jstl 中的函数, 详情:
http://blog.csdn.net/xueshandugu/article/details/35278105