使用速度根据列将一个 table 拆分为多个 table

Splitting one table into multiple tables based on columns using velocity

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

HashMap<HashMap<String, Object>>

所需的 table 将具有等于外部散列映射数的列。行数将等于内部散列图中 Sting/Objects 的数量。

以下是生成 table 的代码。如上所述,table 中的列数将取决于 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 时,是否可以将数据拆分为不同的 table?

例如,考虑 table 看起来像

的场景
            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
    #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 的 return 子集。因此,例如,如果组为 0,则从 0 到 2 的值将被 returned,如果组值为 1,则从 3 到 5 的子图将被 returned,依此类推..