1.情景展示
<table style="order-collapse: collapse;" cellspacing="0" cellpadding="1" border="0" width="99%"> <tbody> <tr class="TableHeader" align="center"> <td class="TableHeaderCell" nowrap="nowrap">日期</td> <td class="TableHeaderCell" nowrap="nowrap">线上正式卡</td> <td class="TableHeaderCell" nowrap="nowrap">线上临时卡</td> <td class="TableHeaderCell" nowrap="nowrap">线下正式卡</td> <td class="TableHeaderCell" nowrap="nowrap">线下临时卡</td> </tr> <c:forEach var="card" items="${model.data}"> <tr class="TableRow" align="center"> <td nowrap="nowrap" class="TableCell" align="left">${card.REGTIME}</td> <td nowrap="nowrap" class="TableCell" align="left">${card.ONLINE_FORMAL}</td> <td nowrap="nowrap" class="TableCell" align="left">${card.ONLINE_INFORMAL}</td> <td nowrap="nowrap" class="TableCell" align="left">${card.OFFLINE_FORMAL}</td> <td nowrap="nowrap" class="TableCell" align="left">${card.OFFLINE_INFORMAL}</td> </tr> </c:forEach> </tbody> </table>
如上图所示,在使用c:foreach标签迭代完毕后,需要加一行合计,对每列数据进行求和,如何实现?
2.思路分析
foreach标签起到Java里for循环的作用;
set标签可以用于声明变量或者变量赋值;
EL表达式支持各种数值运算和字符串函数。
3.解决方案
<table style="order-collapse: collapse;" cellspacing="0" cellpadding="1" border="0" width="99%"> <tbody> <tr class="TableHeader" align="center"> <td class="TableHeaderCell" nowrap="nowrap">日期</td> <td class="TableHeaderCell" nowrap="nowrap">线上正式卡</td> <td class="TableHeaderCell" nowrap="nowrap">线上临时卡</td> <td class="TableHeaderCell" nowrap="nowrap">线下正式卡</td> <td class="TableHeaderCell" nowrap="nowrap">线下临时卡</td> </tr> <c:forEach var="card" items="${model.data}"> <tr class="TableRow" align="center"> <td nowrap="nowrap" class="TableCell" align="left">${card.REGTIME}</td> <td nowrap="nowrap" class="TableCell" align="left"> ${card.ONLINE_FORMAL} <c:set var="SUM_ONLINE_FORMAL" value="${SUM_ONLINE_FORMAL + card.ONLINE_FORMAL}"/> </td> <td nowrap="nowrap" class="TableCell" align="left"> ${card.ONLINE_INFORMAL} <c:set var="SUM_ONLINE_INFORMAL" value="${SUM_ONLINE_INFORMAL + card.ONLINE_INFORMAL}"/> </td> <td nowrap="nowrap" class="TableCell" align="left"> ${card.OFFLINE_FORMAL} <c:set var="SUM_OFFLINE_FORMAL" value="${SUM_OFFLINE_FORMAL + card.OFFLINE_FORMAL}"/> </td> <td nowrap="nowrap" class="TableCell" align="left"> ${card.OFFLINE_INFORMAL} <c:set var="SUM_OFFLINE_INFORMAL" value="${SUM_OFFLINE_INFORMAL + card.OFFLINE_INFORMAL}"/> </td> </tr> </c:forEach> <tr class="TableRow" align="center"> <td nowrap="nowrap" class="TableCell" align="left">合计</td> <td nowrap="nowrap" class="TableCell" align="left">${SUM_ONLINE_FORMAL}</td> <td nowrap="nowrap" class="TableCell" align="left">${SUM_ONLINE_INFORMAL}</td> <td nowrap="nowrap" class="TableCell" align="left">${SUM_OFFLINE_FORMAL}</td> <td nowrap="nowrap" class="TableCell" align="left">${SUM_OFFLINE_INFORMAL}</td> </tr> </tbody> </table>