如何根据几个参数的值隐藏文本字段?

时间:2012-07-14 09:12:34

标签: jasper-reports ireport

我正在尝试隐藏我在页眉标题区中放置的文本字段。字段值由运行时传递到报表中的多个参数确定。基本上我希望当参数值为空(或者其中一些为空)时,该字段为空白或隐藏。

当我执行我的代码时,我得到incompatable operand int and integer。我附上以下报告:

<?xml version="1.0" encoding="UTF-8"  ?>
<!-- Created with iReport - A designer for JasperReports -->
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport
         name="Date_Register"
         columnCount="1"
         printOrder="Vertical"
         orientation="Landscape"
         pageWidth="842"
         pageHeight="595"
         columnWidth="802"
         columnSpacing="0"
         leftMargin="20"
         rightMargin="20"
         topMargin="20"
         bottomMargin="20"
         whenNoDataType="AllSectionsNoDetail"
         isTitleNewPage="false"
         isSummaryNewPage="false">
    <property name="ireport.zoom" value="1.0" />
    <property name="ireport.x" value="95" />
    <property name="ireport.y" value="3" />
    <property name="ireport.scriptlethandling" value="0" />
    <property name="ireport.encoding" value="UTF-8" />
    <import value="java.util.*" />
    <import value="net.sf.jasperreports.engine.*" />
    <import value="net.sf.jasperreports.engine.data.*" />
    <import value="com.google.common.base.*" />

    <parameter name="date_start" isForPrompting="true" class="java.sql.Timestamp">
        <defaultValueExpression ><![CDATA[new java.sql.Timestamp(0)]]></defaultValueExpression>
    </parameter>
    <parameter name="date_end" isForPrompting="true" class="java.sql.Timestamp">
        <defaultValueExpression ><![CDATA[new java.sql.Timestamp(System.currentTimeMillis())]]></defaultValueExpression>
    </parameter>
    <parameter name="lookup_month" isForPrompting="true" class="java.lang.Integer">
        <defaultValueExpression ><![CDATA[new Integer(-1)]]></defaultValueExpression>
    </parameter>
    <parameter name="lookup_year" isForPrompting="true" class="java.lang.Integer">
        <defaultValueExpression ><![CDATA[new Integer(-1)]]></defaultValueExpression>
    </parameter>
    <parameter name="lookup_quarter" isForPrompting="true" class="java.lang.Integer">
        <defaultValueExpression ><![CDATA[new Integer(-1)]]></defaultValueExpression>
    </parameter>
    <parameter name="lookup_weeks" isForPrompting="true" class="java.lang.Integer">
        <defaultValueExpression ><![CDATA[new Integer(-1)]]></defaultValueExpression>
    </parameter>
    <parameter name="woman_status" isForPrompting="true" class="java.lang.String">
        <defaultValueExpression ><![CDATA["N"]]></defaultValueExpression>
    </parameter>
    <parameter name="industry_group" isForPrompting="true" class="java.lang.Integer">
        <defaultValueExpression ><![CDATA[new Integer(-1)]]></defaultValueExpression>
    </parameter>
    <parameter name="equity_structure" isForPrompting="true" class="java.lang.String">
        <defaultValueExpression ><![CDATA["BUMIPUTERA"]]></defaultValueExpression>
    </parameter>
    <queryString><![CDATA[select distinct convert(varchar(10),a.entered,103) as daterange,
a.org_id,
a.name,
a.business_name_two,
a.organization_type,
c.description as companygroup,lead,
a.industry_temp_code,
b.description,
a.equity_structure,
case when a.sic_description = 1 then 'BUMIPUTERA'
     when a.sic_description = 2 then 'NON BUMIPUTERA' end race,
convert(varchar(10),a.entered,103) as
dateregister
from organization a
left join lookup_industry b on a.industry_temp_code = b.code
left join lookup_company_group c on c.code = a.company_group
left join ticket d on d.org_id = a.org_id
left join lookup_month lm on lm.code =a.operation_date 
left join lookup_weeks lw on lw.code =a.operation_date
left join lookup_quarter lq on lq.code =a.operation_date
left join lookup_year ly on ly.code =a.operation_date
where
a.equity_structure is not null
and
business_name_two is not null
and
c.description is not null
and
((a.business_name_two = $P{woman_status}) or ($P{woman_status} = ' '))
and
((a.industry_temp_code = $P{industry_group}) or($P{industry_group} = -1))
and
(((case when a.sic_description = 1 then 'BUMIPUTERA'
     when a.sic_description = 2 then 'NON BUMIPUTERA' end ) = $P{equity_structure}) or ($P{equity_structure} = ' '))
and
((a.operation_date BETWEEN convert(datetime,$P{date_start},103) AND convert(datetime,$P{date_end},103))
or (convert(datetime,$P{date_start},103) =' ' AND convert(datetime,$P{date_end},103) = ' '))

AND
((lm.code = $P{lookup_month}) or ($P{lookup_month} = -1)) 
AND
((ly.code = $P{lookup_year}) or ($P{lookup_year} = -1))
AND
((lq.code = $P{lookup_quarter}) or ($P{lookup_quarter} = -1))
AND
((lw.code = $P{lookup_weeks}) or ($P{lookup_weeks} = -1))]]></queryString>

    <field name="daterange" class="java.lang.String"/>
    <field name="org_id" class="java.lang.Integer"/>
    <field name="name" class="java.lang.String"/>
    <field name="business_name_two" class="java.lang.String"/>
    <field name="organization_type" class="java.lang.String"/>
    <field name="companygroup" class="java.lang.String"/>
    <field name="lead" class="java.lang.String"/>
    <field name="industry_temp_code" class="java.lang.Integer"/>
    <field name="description" class="java.lang.String"/>
    <field name="equity_structure" class="java.lang.String"/>
    <field name="race" class="java.lang.String"/>
    <field name="dateregister" class="java.lang.String"/>

        <background>
            <band height="0"  isSplitAllowed="true" >
            </band>
        </background>
        <title>
            <band height="0"  isSplitAllowed="true" >
            </band>
        </title>
        <pageHeader>
            <band height="187"  isSplitAllowed="true" >
                <staticText>
                    <reportElement
                        x="0"
                        y="0"
                        width="802"
                        height="18"
                        key="staticText"/>
                    <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
                    <textElement textAlignment="Center">
                        <font fontName="SansSerif" size="12" isBold="true"/>
                    </textElement>
                <text><![CDATA[Date Registered
]]></text>
                </staticText>
                <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        x="0"
                        y="54"
                        width="802"
                        height="42"
                        key="textField-1"
                        isRemoveLineWhenBlank="true"/>
                    <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
                    <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" isStyledText="true" lineSpacing="1_1_2">
                        <font size="11" isBold="true"/>
                    </textElement>
                <textFieldExpression   class="java.lang.String"><![CDATA[($P{date_start}!= null) && ($P{date_end}!=null) && ($P{lookup_month}!= -1) && ($P{lookup_year}!=-1)&& ($P{lookup_quarter}!= -1) && ($P{lookup_weeks}!=-1) ? "Date Ticket Created From : " + new SimpleDateFormat("dd/MM/yyyy").format($P{date_start}) +"\n" + "Date Ticket Created to : " + new SimpleDateFormat("dd/MM/yyyy").format($P{date_end})
: null]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true" isBlankWhenNull="true" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        x="0"
                        y="20"
                        width="802"
                        height="35"
                        key="textField-2"
                        isRemoveLineWhenBlank="true"/>
                    <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
                    <textElement textAlignment="Center" verticalAlignment="Top" rotation="None" lineSpacing="Single">
                        <font size="12" isBold="true"/>
                    </textElement>
                <textFieldExpression   class="java.lang.String"><![CDATA[($P{date_start}!= null) && ($P{date_end}!=null) && ($P{lookup_month}== -1) && ($P{lookup_year}==-1)&& ($P{lookup_quarter}== -1) && ($P{lookup_weeks}==-1)? " From ( " + new SimpleDateFormat("dd/MM/yyyy").format($P{date_start}) + " - " + new SimpleDateFormat("dd/MM/yyyy").format($P{date_end}) + " )"
:($P{date_start}== null) && ($P{date_end}==null) && ($P{lookup_month}!= -1) && ($P{lookup_year}!=-1)&& ($P{lookup_quarter}== -1) && ($P{lookup_weeks}!=-1)? "For week " + $P{lookup_weeks} +" "+ $P{lookup_month} +" "+ $P{lookup_year}
:($P{date_start}== null) && ($P{date_end}==null) && ($P{lookup_month}!= -1) && ($P{lookup_year}!=-1)&& ($P{lookup_quarter}== -1) && ($P{lookup_weeks}==-1)? "For monthly " +$P{lookup_month} +" "+ $P{lookup_year}
:($P{date_start}== null) && ($P{date_end}==null) && ($P{lookup_month}== -1) && ($P{lookup_year}!=-1)&& ($P{lookup_quarter}!= -1) && ($P{lookup_weeks}==-1)? "For Quarter " + $P{lookup_quarter} +" "+$P{lookup_year}
:($P{date_start}== null) && ($P{date_end}==null) && ($P{lookup_month}== -1) && ($P{lookup_year}!=-1)&& ($P{lookup_quarter}== -1) && ($P{lookup_weeks}==-1)? "For Year " + $P{lookup_year}
: $P{lookup_year}]]></textFieldExpression>
                </textField>
            </band>
        </pageHeader>
        <columnHeader>
            <band height="0"  isSplitAllowed="true" >
            </band>
        </columnHeader>
        <detail>
            <band height="0"  isSplitAllowed="true" >
            </band>
        </detail>
        <columnFooter>
            <band height="0"  isSplitAllowed="true" >
            </band>
        </columnFooter>
        <pageFooter>
            <band height="0"  isSplitAllowed="true" >
            </band>
        </pageFooter>
        <summary>
            <band height="0"  isSplitAllowed="true" >
            </band>
        </summary>
</jasperReport>

2 个答案:

答案 0 :(得分:2)

  

基本上我希望当参数值为空(或者其中一些为空)时,该字段为空白或隐藏

为此目的,我猜打印表达时会非常方便。假设param1是你的参数,你可以添加类似(!$ P {param1} == null)的东西。仅当参数具有值时,才会显示该字段。 最重要的是,当指定默认值时,这将不起作用。因为参数永远不能为空,或者指定了默认值为null。

答案 1 :(得分:0)

这里有几个问题:

1)您确实为参数date_startdate_end设置了默认值。这意味着它们永远不会为空。 (如果你明确传入参数的空值,这可能会被覆盖,但我不确定是否诚实)。您最好的办法是不为这两个参数设置默认值。

2)如果你想要隐藏字段,那么我会在字段上表达时使用打印。例如,您的日期范围文本字段最终将如下所示:

<textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
  <reportElement uuid="9bcc0870-adaf-4bb3-bd4d-40bf120a0d1f" key="textField-1" x="0" y="54" width="802" height="42" isRemoveLineWhenBlank="true">
    <printWhenExpression><![CDATA[($P{date_start}!= null) && ($P{date_end}!=null)]]></printWhenExpression>
  </reportElement>
  <box>
    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
  </box>
  <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" markup="styled">
    <font size="11" isBold="true"/>
    <paragraph lineSpacing="1_1_2"/>
  </textElement>
  <textFieldExpression><![CDATA["Date Ticket Created From : " + new SimpleDateFormat("dd/MM/yyyy").format($P{date_start}) +"\n" + "Date Ticket Created to : " + new SimpleDateFormat("dd/MM/yyyy").format($P{date_end})]]></textFieldExpression>
</textField>

我也会为你的顶级文本域做类似的事情:

<textField isStretchWithOverflow="true" isBlankWhenNull="true">
  <reportElement uuid="37137d13-5d49-40fb-8627-0ffdfc555ef4" key="textField-2" x="0" y="20" width="802" height="35" isRemoveLineWhenBlank="true">
    <printWhenExpression><![CDATA[(($P{date_start}!= null) && ($P{date_end}!=null)) || ($P{lookup_year}!=-1)]]></printWhenExpression>
  </reportElement>
  <box>
    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
  </box>
  <textElement textAlignment="Center" verticalAlignment="Top" rotation="None">
    <font size="12" isBold="true"/>
    <paragraph lineSpacing="Single"/>
  </textElement>
  <textFieldExpression><![CDATA[($P{date_start}!= null) && ($P{date_end}!=null) ? " From ( " + new SimpleDateFormat("dd/MM/yyyy").format($P{date_start}) + " - " + new SimpleDateFormat("dd/MM/yyyy").format($P{date_end}) + " )"
  : ($P{lookup_month}!= -1) && ($P{lookup_year}!=-1) &&  ($P{lookup_weeks}!=-1) ? "For week " + $P{lookup_weeks} +" "+ $P{lookup_month} +" "+ $P{lookup_year}
  : ($P{lookup_month}!= -1) && ($P{lookup_year}!=-1) ? "For monthly " +$P{lookup_month} +" "+ $P{lookup_year}
  : ($P{lookup_year}!=-1) && ($P{lookup_quarter}!=-1) ? "For Quarter " + $P{lookup_quarter} +" "+$P{lookup_year}
  : ($P{lookup_year}!=-1) ? "For Year " + $P{lookup_year}
  : null]]></textFieldExpression>
</textField>

我使用一个空的数据源运行了这个修改报告,并且参数的值不同,它看起来像你想要的那样。