计算jstl中每个列的总计 [英] Calculate total of each columns in jstl
本文介绍了计算jstl中每个列的总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的报告
01 jan 02 jan 03 jan ... total
user1 3 5 10 18
user2 5 6 9 20
.
.
.
total ? ? ? 38
我可以使用我的JSTL代码来计算每一行的总计。我找不到用'?'来计算结束标记的列总数的方法。
I am able to calulate each row total at end using my JSTL code. I don't find the way to calculate columns total at end mark with '?'.
对于行总数,我使用像
<c:forEach var="colNames" items="${listOfRecords}">
<tr>
<td>${colNames.key}</td>
<c:set var="htotal" value="0" />
<c:forEach var="noOfTasks" items="${colNames.value}" varStatus="status">
<td>${noOfTasks.value}</td>
<c:set var="htotal" value="${htotal+noOfTasks.value}" />
</c:forEach>
<td class="foo">${htotal}</td>
</tr>
</c:foreach>
这里 $ {listOfRecords}
c $ c> HashMap< String,HashMap< String,Integer>> 。
Here ${listOfRecords}
is HashMap<String, HashMap<String, Integer>>
.
如何实现?
推荐答案
这是我能够想出的:
<jsp:useBean id="column_totals" class="java.util.LinkedHashMap" scope="page"/>
<c:forEach var="row" items="${listOfRecords}">
<tr>
<td>${row.key}</td>
<c:set var="row_total" value="0"/>
<c:forEach var="column" items="${row.value}">
<td>${column.value}</td>
<c:if test="${empty column_totals[column.key]}">
<c:set target="${column_totals}" property="${column.key}" value="0"/>
</c:if>
<c:set target="${column_totals}" property="${column.key}" value="${column_totals[column.key] + column.value}"/>
<c:set var="row_total" value="${row_total + column.value}" />
</c:forEach>
<td class="foo">${row_total}</td>
</tr>
</c:foreach>
<tr>
<td>total</td>
<c:forEach var="column_total" items="${column_totals}">
<td>${column_total.value}</td>
</c:forEach>
</tr>
不能改变一些名字,因为我不明白你的对不起。
Had to change some names cause I couldn't understand with yours sorry.
我认为它应该工作。
这篇关于计算jstl中每个列的总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文