0

好的,下面是我拥有的代码。在这里,我们从 JAVA 对象中读取映射并根据映射的键和值填充列。Java 对象映射的形式为

HashMap<HashMap<String, Object>>.

所需表的列数等于外部哈希图的数量。行数将等于内部哈希图中的 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">&nbsp;</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 
4

2 回答 2

1
    #set ($allLegs = $ConfirmationObject.getAllLegs())
    #set ($columns = $allLegs.keySet())
    #set ($maxCols = 3)
    #set ($groups = ($columns.size() + $maxCols - 1)/$maxCols)
    #set ($lastGroup = $groups - 1)

    #foreach ($group in [0..$lastGroup])
    #if($group >0 )
    <br>
    <br>
    #end
    <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 = (($group*$maxCols)+1))
     #set ($groupLegs = $ConfirmationObject.getGrouplLegSet($group, $maxCols))

     <tr>
      <td valign="top" width="30%"> </td>
       #foreach($legSzie in $groupLegs.keySet())
        <td valign="top" width="30%" align="left"><b>Leg $i</b></td>
         #set ($i=$i+1)
       #end
      <td></td>
      <td valign="top" width="10%" align="right">&nbsp;</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 $groupLegs.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>
 #end

这里我们编写了一个 java 方法getGrouplLegSet($group, $maxCols)。该方法将仅返回基于组和 maxCols 的 hashmap 子集。因此,例如,如果组为 0,则返回 0 到 2 的值,如果组值为 1,则返回 3 到 5 的子映射,依此类推。

于 2017-08-10T14:14:07.700 回答
1

你可以尝试这样的事情:

#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
于 2017-08-08T08:28:03.597 回答