【问题标题】:Splitting one table into multiple tables based on columns using velocity使用速度根据列将一个表拆分为多个表
【发布时间】:2018-01-15 14:39:51
【问题描述】:

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

HashMap<HashMap<String, Object>>

所需表的列数等于外部哈希图的数量。行数将等于内部 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 

【问题讨论】:

  • 什么是 ConfirmationObject?
  • @MauricePerry :这只是形式 hashmap> 的 java 集合的名称
  • 好的,那么getAllLegs和getLegMap的方法是什么?
  • @MauricePerry:getAllLegs 将返回对应于表列数的外部哈希图。 getLegMap 将返回对应于表行的内部哈希图。感谢您抽出宝贵时间。感谢您对此的调查。

标签: java html velocity vtl


【解决方案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 的子映射,依此类推..

【讨论】:

    【解决方案2】:

    你可以试试这样的:

    #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
    

    【讨论】:

    • 非常感谢您的帮助/指导。您的回答并没有完全解决我的问题,但它给了我足够的提示来最终找到解决方案。所以我还是会接受你的回答。下面粘贴的是我通过修改您的代码的解决方案。再次感谢朋友
    猜你喜欢
    • 2022-12-22
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2021-12-17
    相关资源
    最近更新 更多