How to properly group records when executing a <#list>

巧了我就是萌 提交于 2021-02-05 10:56:45

问题


New guy here. I have been building an advanced form in NetSuite (uses Freemarker) to display invoice data. Everything looks and works great, however, I want to group the invoice line items by location. I am using a simple <#list> loop to pull the line item records. I currently display the location on each line item.

Code (formats/styles removed for simplicity):

<table>
  <#list record.item as item>
     <tr>
        <td> ${item.location} </td>
        <td> ${item.description} </td>
        <td> ${item.quantity} </td>
        <td> ${item.rate} </td>
        <td> ${item.amount} </td>
    </tr>
  </#list>
</table>

Current Output example:

Location A     Des 1              1        $100     $100
Location B     Des 1              1        $100     $100
Location C     Des 1              1        $100     $100
Location A     Des 2              1        $100     $100
Location B     Des 2              1        $100     $100
Location C     Des 2              1        $100     $100
Location A     Des 3              1        $100     $100
Location C     Des 3              1        $100     $100

Desired Output Example:

Location A
Des 1              1        $100     $100
Des 2              1        $100     $100
Des 3              1        $100     $100
Location B
Des 1              1        $100     $100
Des 2              1        $100     $100
Location C
Des 1              1        $100     $100
Des 2              1        $100     $100
Des 3              1        $100     $100

I have tried to nest a second <#list> but it did not work correctly. Any suggestions or pointers would be helpful to push me in the correct direction.

Thank you!


回答1:


FreeMarker expects such grouping to be done by whatever sets up the variables, which is in this case NetSuite. (However, I think this could be seen as purely a presentation concern, and so maybe FreeMarker should handle this in the future.) If NetSuite indeed won't group the data for you, then you have to do it in FreeMarker, which will be a bit awkward, as it's not a real programming language... but here it is.

Define a macro like this:

<#macro listGroups items groupField>
  <#if items?size == 0><#return></#if>
  <#local sortedItems = items?sort_by(groupField)>
  <#local groupStart = 0>
  <#list sortedItems as item>
    <#if !item?is_first && item[groupField] != lastItem[groupField]>
      <#local groupEnd = item?index>
      <#nested lastItem[groupField], sortedItems[groupStart ..< groupEnd]>
      <#local groupStart = groupEnd>
    </#if>
    <#local lastItem = item>
  </#list>
  <#local groupEnd = sortedItems?size>
  <#nested lastItem[groupField], sortedItems[groupStart ..< groupEnd]>
</#macro>

You can use this macro later like this:

<@listGroups record.item "location"; groupName, groupItems>
  <p>${groupName}</p>
  <table>
    <#list groupItems as groupItem>
       <tr>
          <td>${groupItem.location}</td>
          <td>${groupItem.description}</td>
          <td>${groupItem.quantity}</td>
          <td>${groupItem.rate}</td>
          <td>${groupItem.amount}</td>
      </tr>
    </#list>
  </table>
</@listGroups>

Note that groupName and groupItems in <@listGroups ...> are just arbitrary loop variable names that you specify, and they need not match the variable names used inside the #macro definition.



来源:https://stackoverflow.com/questions/61578682/how-to-properly-group-records-when-executing-a-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!