1

I want to conditionally display a text field in the title band based on the existence of some value retrieved in one of the records in the details band.

Is there a way I can get access to the dataset result list from inside the expression editor so I can loop through the result list and verify the existence of that particular value?

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
T.G
  • 53
  • 5

1 Answers1

0

I would solve this problem by using a variabile and then evaluation time on the text field.

First define a variabile that verify the existence of that particular value, by for example counting how many times it present.

<variable name="SERIES_D_COUNT" class="java.lang.Integer" calculation="Sum">
   <variableExpression><![CDATA["D".equals($F{Series})?1:0]]></variableExpression>
</variable>

Then set on the conditionally display a text field, evaluationTime="Report"

From JasperReports api

A constant specifying that an expression should be evaluated at the end of the filling process.

Do note, you can not use the printWhenExpression this does not have delayed evaluation. If you do not like to display any text you need use a similar expression

<textField evaluationTime="Report">
    <reportElement x="0" y="0" width="200" height="30" uuid="c5ce6914-e96a-4b7a-bcd9-b9febfb4d003"/>
    <textFieldExpression><![CDATA[($V{SERIES_D_COUNT}>0? "Series D is present " + $V{SERIES_D_COUNT} + " times":"")]]></textFieldExpression>
</textField>

If you need "floating" layout, hence different textField should move if the value is present this technique can not be used, since the fill manager will need to reserve the place, only the content (expression) is evaluated after report is filled. In these cases you need to do a subreport and for example re-query the database.

Full Example

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="TestAccessValue" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4d3c7e04-2b50-46a0-b840-05ab72633357">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="ChartData"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="Series" class="java.lang.String"/>
    <field name="X" class="java.lang.String"/>
    <variable name="SERIES_D_COUNT" class="java.lang.Integer" calculation="Sum">
        <variableExpression><![CDATA["D".equals($F{Series})?1:0]]></variableExpression>
    </variable>
    <title>
        <band height="35" splitType="Stretch">
            <textField evaluationTime="Report">
                <reportElement x="0" y="0" width="200" height="30" uuid="c5ce6914-e96a-4b7a-bcd9-b9febfb4d003"/>
                <textFieldExpression><![CDATA[($V{SERIES_D_COUNT}>0? "Series D is present " + $V{SERIES_D_COUNT} + " times":"")]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <columnHeader>
        <band height="24" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="20" uuid="8ff3bf66-0530-47d1-bd9e-c8cd392b418d"/>
                <text><![CDATA[Series]]></text>
            </staticText>
            <staticText>
                <reportElement x="100" y="0" width="100" height="20" uuid="b6e5470d-fcd8-4770-8dc4-bd194f05e15b"/>
                <text><![CDATA[X]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="24" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="20" uuid="f50e1756-50da-40a3-89f3-7a906659feb1"/>
                <textFieldExpression><![CDATA[$F{Series}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="0" width="100" height="20" uuid="7adb0ce7-3bf5-42ed-b41d-2daee9ee52df"/>
                <textFieldExpression><![CDATA[$F{X}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109