Returning a value from Sub Report to Main Report i

2019-01-18 00:07发布

问题:

Hi every one I am using iReports for generating one of the reports and stuck at one place.

The situation is like this:

I am using one sub report in my main report and i want to return a variable (float) back to the main report from sub report after the query has been executed. And i'm just getting null values back to main report i have wasted 2 days googling and searching but problem is still there..

bellow is my dummy code of my JRXMLS (perfectly same) and snaps...

Main Report JRXML

  <?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="anuj" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
        <property name="ireport.zoom" value="1.0"/>
        <property name="ireport.x" value="0"/>
        <property name="ireport.y" value="0"/>
        <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
            <defaultValueExpression><![CDATA["/home/anuj/Reports/"]]></defaultValueExpression>
        </parameter>
        <queryString language="SQL">
            <![CDATA[select * from "SensorType"]]>
        </queryString>
        <field name="SensorTypeId" class="java.lang.Integer"/>
        <field name="SensorTypeName" class="java.lang.String"/>
        <variable name="A" class="java.lang.Integer" resetType="None" calculation="System"/>
        <background>
            <band splitType="Stretch"/>
        </background>
        <title>
            <band splitType="Stretch"/>
        </title>
        <pageHeader>
            <band splitType="Stretch"/>
        </pageHeader>
        <columnHeader>
            <band splitType="Stretch"/>
        </columnHeader>
        <detail>
            <band height="23" splitType="Stretch">
                <textField>
                    <reportElement x="71" y="3" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{SensorTypeId}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="202" y="3" width="112" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{SensorTypeName}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
        <columnFooter>
            <band height="5" splitType="Stretch"/>
        </columnFooter>
        <pageFooter>
            <band height="1" splitType="Stretch"/>
        </pageFooter>
        <summary>
            <band height="42" splitType="Stretch">
                <subreport>
                    <reportElement x="183" y="16" width="257" height="26"/>
                    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    <returnValue subreportVariable="A" toVariable="A"/>
                    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "anuj_subreport1.jasper"]]></subreportExpression>
                </subreport>
                <textField>
                    <reportElement x="71" y="22" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$V{A}]]></textFieldExpression>
                </textField>
            </band>
        </summary>
    </jasperReport>

Sub Report JRXML

<?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="anuj_subreport1" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="149"/>
    <queryString>
        <![CDATA[select Sum("SensorTypeId") from "SensorType";]]>
    </queryString>
    <field name="sum" class="java.lang.Long"/>
    <variable name="A" class="java.lang.Integer" resetType="None" calculation="System">
        <variableExpression><![CDATA[$F{sum}]]></variableExpression>
    </variable>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="169" y="59" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[sum]]></text>
            </staticText>
            <textField>
                <reportElement x="216" y="59" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{sum}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch"/>
    </detail>
        <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

Thanks in advance..

回答1:

In the subreport variable, you have calculation=System. This should be used when you are calculating the variable yourself using a scriptlet. You do not have an appropriate scriptlet attached to the report, so the variable is never being calculated.

You probably want to change the variable definition so that it is calculated at the start of the subreport, something like this:

<variable name="A" class="java.lang.Integer" resetType="Report" calculation="Nothing">
    <variableExpression><![CDATA[$F{sum}]]></variableExpression>
</variable>

resetType="Report" will never reset the variable while the report is running. calculation="Nothing" will evaluate the variableExpression for every row in the dataset.

These attributes could be omitted though, as they are the default values used. You should then find that the variable is initialised correctly.



回答2:

Although the question is already answered, I would like to highlight the importance of having calculation="System" on the variable in the Main Report (the report calling the SubReport).

I wasted a lot of time before figuring this out...

In the dummy-code for Main Report JRXML above, the variable "A" correctly has calculation="System". NB: The answer by @GenericJon covers the variable in the SubReport.



回答3:

From the JasperReports Ultimate Guide:

The value coming from the subreport is available only when the whole band containing the subreport is printed. If you need to print this value using a textfield placed in the same band as your subreport, set the evaluation time of the textfield to Band



回答4:

i am agree with @bubba_hego99, the calculation type as below:

system no caculation, just set as system, or valued as other operator.

nothing no calulation, just set the variable.

sum execute sum($Feild("A")).

....

The detail you want to know other caculation, you should be to see the ireport document.



回答5:

Note, in earlier versions of JasperReports:

When using a subreport returnValue, the variable returned must not be in the same band as the subreport itself.

The affectation is done I think at the end of the rendering of the band.

So add another band and use your variable inside.