问题
I am creating a report in Jasper
with four parts. Each part consists of a text field as the header and a subreport. The subreport can be empty. If it is empty it is hidden using Remove Line When Blank. This works so far. Now, the corresponding header text field should be hidden too if the subreport is hidden.
I have tried many things but couldn't come up with a solution.
E.g. using Print When Expression: new Boolean ($P{otherField}.read() != -1)
. This never showed the header.
As well I tried to hide the header in my Java program but couldn't find an appropriate API command. I guess getBands()[x].getChildren()[y]
lead me to the right header but I couldn't hide it there.
Any other ideas?
回答1:
You should put the header text inside the subreport in for example a title
band
In subreport
<title>
<band height="38">
<textField>
<reportElement x="10" y="0" width="100" height="20" uuid="4b8ece47-1608-4d08-bc0b-e6b73bcf8346"/>
<textFieldExpression><![CDATA["TITLE"]]></textFieldExpression>
</textField>
</band>
</title>
Make sure you do not have the attribute whenNoDataType on jasperReport
tag, so it uses default value NoPages
If the subreport has no records the attribute whenNoDataType=NoPages
(default) will ensure that nothing is returned and you will not see the title.
If you need it outside of subreport (for some strange reason) pass record count from subreport back to main report and use a text field with printWhenExpression
and evalutationTime="Report"
Check out this for some additional details on how to pass back report count.
How to get report_count from subreport in iReport
回答2:
Thank you for your help. I finally could solve it by using the following expression under "Print When Expression" in the header:
($P{SUBR}!=null)
where
$P{SUBR}
is the expression of the subreport.
来源:https://stackoverflow.com/questions/34682494/hide-text-field-if-subreport-is-empty