好的,下面是我的代码。在这里,我们从 JAVA 对象读取映射,并根据映射的键和值填充列。 Java 对象映射的形式为
HashMap<HashMap<String, Object>>
。
所需表的列数等于外部 HashMap 的数量。行数将等于内部 HashMap 中的 Sting/Object 数量。
以下是生成表格的代码。如上所述,表中的列数将取决于 java 对象中的值。我们面临的问题是,如果 hashmap 中的值大于 10,则 PDH 生成会导致数据丢失。
<table style="font-family:Arial;font-size:xx-small;color:black" width="100%" border="0" cellspacing="0" cellpadding="0">
#set ($allLegs = $ConfirmationObject.getAllLegs())
#set ($i = 1)
<tr>
<td valign="top" width="30%"> </td>
#foreach($legSzie in $allLegs.keySet())
<td valign="top" width="30%" align="left"><b>Leg $i</b></td>
#set ($i=$i+1)
#end
<tr><td></td></tr>
<td valign="top" width="10%" align="right"> </td>
</tr>
<td colspan="1">
<table style="font-family:Arial;font-size:xx-small;color:black" width="100%" border="0" cellspacing="0" cellpadding="0">
#set ($map = $ConfirmationObject.getLegMap(1))
#foreach($key in $map.keySet())
<tr>
<td valign="top" width="60%">$key </td>
</tr>
#end
</table>
</td>
#foreach($legString in $allLegs.keySet())
<td colspan="1">
<table style="font-family:Arial;font-size:xx-small;color:black" width="100%" border="0" cellspacing="0" cellpadding="0">
#set ($legMap = $allLegs.get($legString))
#foreach($legKey in $legMap.keySet())
<tr>
<td >$legMap.get($legKey)</td>
</tr>
#end
</table>
</td>
#end
</table>
期望: 当列值超过3时,是否可以将数据拆分到不同的表中?
例如,考虑表格看起来像这样的场景
LEG 1 LEG 2 LEG 3 LEG 4 LEG 5
A 12 13 14 15 16
B 12 13 14 15 16
C 12 13 14 15 16
D 12 13 14 15 16
E 12 13 14 15 16
我们如何分割它
LEG 1 LEG 2 LEG 3
A 12 13 14
B 12 13 14
C 12 13 14
D 12 13 14
E 12 13 14
LEG 4 LEG 5
A 15 16
B 15 16
C 15 16
D 15 16
E 15 16
最佳答案
你可以尝试这样的事情:
#set ($columns = $allLegs.keySet().toArray())
#set ($maxCols = 3)
#set ($groups = ($columns.size() + $maxCols - 1)/$maxCols)
#set ($lastGroup = $groups - 1)
#foreach ($group in [0..$lastGroup])
<table style="font-family:Arial;font-size:xx-small;color:black" width="100%" border="0" cellspacing="0" cellpadding="0">
#set ($firstCol = $maxCols*$group )
#set ($lastCol = $firstCol + $maxCols - 1)
#if ($lastCol >= $columns.size())
#set ($lastCol = $columns.size() - 1)
#end
<tr>
<th></th>
#foreach ($col in [$firstCol..$lastCol])
<th>$columns[$col]</th>
#end
</tr>
#set ($rows = $allLegs.get($columns[$firstCol]).keySet())
#foreach($row in $rows)
<tr>
<th>$row</th>
#foreach ($col in [$firstCol..$lastCol])
#set ($legMap = $allLegs.get($columns[$col]))
<td>$legMap.get($row)</td>
#end
</tr>
#end
</table>
#end
关于java - 使用速度根据列将一张表拆分为多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45561330/