【问题标题】:Print table in freemarker/html if value exists in table如果表中存在值,则在 freemarker/html 中打印表
【发布时间】:2021-11-21 06:52:46
【问题描述】:

在 ERP 系统中进行 PDF 设计。该设计工具是 HTML 和 Freemarker 的组合。如果订单的任何订单行在特定字段中的值大于 0,我想添加一个表格,并且表格数据应仅包含这些行。现在看起来像这样:

    <#if order.item?has_content>
    <table class="itemtable" style="width: 100%; margin-top: 10px;">
    <thead>
        <tr>
        <th colspan="8">Item</th>
        <th align="right" colspan="3">Quantity</th>
      </tr>
    </thead>
      
      <#list salesorder.item as tranline><#if tranline.quantity!=0>

            <tr>
                <td colspan="8">{tranline.item}</td>
                <td align="right" colspan="3">${tranline.quantity}</td>
                            
                
            </tr>
                                        </#if> 
                  </#list>
                    
        </table>
       </#if> 

--

因此,该表仅包含数量 > 0 的行,这是我想要的结果。但我也希望只有在我的条件正常的情况下才能打印表头。现在,如果没有订单行的数量 > 0,则创建的表头没有行。在那种情况下,我根本不希望它出现。

我猜这很简单,但我是新手。

有什么想法吗?

【问题讨论】:

    标签: html freemarker


    【解决方案1】:

    希望这不是 Netsuite(它使用一些旧 FreeMarker 版本的修改变体),您可以使用?filter,结合#list+#items(参见https://freemarker.apache.org/docs/ref_directive_list.html#ref.directive.items):

    <#list salesorder.item?filter(order -> order.quantity != 0)>
      <table>
        <thead>...</thead>
        <#items as order>
          <tr>... print order data here
        </#items>
      <table>
    </#list>
    

    在 Netsuite 下,如果阅读本文时仍然不支持 ?filter,则可以循环两次。在第一个循环中检查是否有任何行满足条件,但不打印任何内容,然后只有在匹配时,才执行第二个循环来打印项目:

    <#assign hasMatchingOrder = false>
    <#list salesorder.item as order>
      <#if order.quantity != 0>
        <#assign hasMatchingOrder = true>
        <#break>
      </#if>
    </#list>
    <#if hasMatchingOrder>
      <table>
        <thead>...</thead>
        <#list salesorder.item as order>
          <#if order.quantity != 0>
            <tr>... print order data here
          </#if>
        </#list>
      </table>
    </#if>
    

    所以这样做更丑陋且容易出错,所以请纠缠 Oracle 更新 Netsuite 中的 FreeMarker。

    否则,如果经常需要上面的东西,那么可以将它放入宏中,虽然我不知道Netsuite是否支持具有共享宏的模板(即共享#include-ed或#importe -ed 文件)。

    【讨论】:

    • 嗨!它是 Netsuite :D 你能告诉我那个双循环吗?非常感谢!
    • Netsuite... 然后我更新了我的答案
    • 非常感谢,工作就像一个魅力!
    【解决方案2】:

    所以,我只想做这样的事情:

    IF MAX(Order.Quantity) > 0 THEN .... 创建表

    【讨论】:

    • 如果您有说明,请下次编辑问题,而不是添加答案。无论如何,要计算该值,您可以使用objs?map(order -&gt; order.quantity)?max,但正如我的回答所示,您不需要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2016-11-25
    • 2017-02-01
    • 1970-01-01
    • 2014-10-04
    • 2013-08-07
    相关资源
    最近更新 更多