使用velocity将基于列的一个表拆分为多个表

时间:2017-08-08 06:58:20

标签: java html velocity vtl

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

HashMap<HashMap<String, Object>>

所需表的列数等于外部hashmap的数量。行计数将等于内部hashmap中的Sting / Objects数。

以下是生成表格的代码。如上所述,表中的列数取决于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 

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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)。该方法将基于group和maxCols返回hashmap的子集。因此,例如,如果组为0,那么将返回从0到2的值,如果组值为1,则将返回从3到5的子映射,依此类推..