代码之家  ›  专栏  ›  技术社区  ›  Jonas

如何在JasperReports中使用条件文本字段?

  •  7
  • Jonas  · 技术社区  · 15 年前

    我想要一对文本字段,这取决于一个值。“Y”值应根据空位进行调整。

    当值为 "0" 我想隐藏文本字段。

    也就是说,我想把 staticText 以及 textField 如果参数 red 等于 “0” 并在下面的jrxml代码中向上移动蓝色值:

      <staticText>
        <reportElement x="100" y="30" width="100" height="30"/>
        <text><![CDATA[Red items:]]></text>
      </staticText>
      <textField>
        <reportElement x="200" y="30" width="40" height="30"/>
        <textFieldExpression>
          <![CDATA[$P{red}]]>
        </textFieldExpression>
      </textField>
    
      <staticText>
        <reportElement x="100" y="60" width="100" height="30"/>
        <text><![CDATA[Blue items:]]></text>
      </staticText>
      <textField>
        <reportElement x="200" y="60" width="40" height="30"/>
        <textFieldExpression>
          <![CDATA[$P{blue}]]>
        </textFieldExpression>
      </textField>
    

    输出示例:

    //if blue = 3 and red = 2    if blue = 3 and red = 0    if blue = 0 and red = 2
        Red items: 2               Blue items: 3              Red items: 2
        Blue items: 3    
    

    这些文本字段将放在我报告的末尾。我该怎么做?

    3 回复  |  直到 11 年前
        1
  •  11
  •   Alex K    13 年前
    <reportElement ...>
        <printWhenExpression><![CDATA[$P{red} == 0]]></printWhenExpression>
    </reportElement>
    

    您可以使用iReport通过一个令人愉快的用户界面来修改它。

        2
  •  1
  •   mohdajami    15 年前

    这样,不,我不确定是否有可能。

    有一个选项叫做 Remove Link When Blank ,但只有当您想删除整行时,它才起作用。在这里,您要删除特定列中的一行。

    在这种情况下,我建议使用 crosstab 或交叉表功能。

    给列组x的值(假设x是列号) 并向行组提供颜色字段的值,从这里可以动态更改标签,如下所示:

    $F{color}==null?"": ($F{color}.equals("RED")?"Red Items":"Blue Items")
    
        3
  •  0
  •   Alexis Pigeon Shawn Skelton    11 年前

    你可以这样用

    Declare RED as [class="java.lang.Number"]
    

    印刷时

    $P{red}.intValue() == 0 ? null : $P{red}.intValue()
    

    当字段的空选项为空时,使用

    textField isBlankWhenNull="true">               
    <reportElement x="100" y="30" width="100" height="30" isRemoveLineWhenBlank="true"/>