希望创建一个4x10表,使用foreach循环以速度列出我的数组

时间:2015-05-15 17:59:05

标签: arrays apache foreach velocity

我想在第一行循环播放[1-4]数组,在第二行循环[5-8]并继续直到输出整个数组。

#set ($cats = [
  "Accounting", "Admin & Clerical", "Automotive", "Banking", 
  "Broadcast - Journalism", "Business Development", "Construction", "Customer Service",
  "Design", "Distribution - Shipping", "Education - Teaching", "Facilities",
  "Finance", "General Business", "General Labor", "Government", 
  "Grocery", "Health Care", "Hotel - Hospitality", "Human Resources", 
  "Installation - Maint - Repair", "Inventory", "Legal Admin", "Management",
  "Manufacturing", "Marketing", "Nurse", "Pharmaceutical",
  "Purchasing - Procurement", "QA - Quality Control", "Real Estate", "Research",
  "Restaurant - Food Service", "Retail", "Sales", "Skilled Labor - Trades",
  "Strategy - Planning", "Supply Chain", "Transportation", "Warehouse"
] )

如何抓取阵列中的前4个项目,将它们放在一行中,然后继续下一个4?

<table cellpadding="5px" cellspacing="0" width="100%" style="width:100%!important;font-size:9px;font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;font-weight:normal;text-align:center;background:;color:black">
  #set ($counter = 0)
  #set ($j = )
  #foreach ($i in $cats)
    #if ( $counter % 2 == 0)
    <tr>
      <td>$i</td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    #else
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    #end
  #end

</table>

1 个答案:

答案 0 :(得分:0)

我根本不了解Velocity,但根据我对其他编码语言的了解,尝试这样的事情让我知道它是怎么回事。

<table cellpadding="5px" cellspacing="0" width="100%" style="width:100%!important;font-size:9px;font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;font-weight:normal;text-align:center;color:black">
  <!-- The two sets in your code here looked superfluous --!>
  #foreach ($i in $cats)
    #if ( ( $foreach.index - 1 ) % 4 == 0)
      <tr>
    #end
    <td>$i</td>
    #if ( ( $foreach.index - 1 ) % 4 == 3 || $foreach.last)
      </tr>
    #end
  #end

</table>

编辑:快速搜索后,我相信Velocity foreach索引不会从零开始,因此我更新了代码。如果这不正确,只需将$foreach.index - 1的两个实例替换为$foreach.index(删除- 1)。