Jump to content
Changes to the Jaspersoft community edition download ×

Returning values from a subreport


Recommended Posts

By: Lecio Nunes - lnunes

Returning values from a subreport

2002-11-05 11:54

Hi,

 

I need a way to take back some totals generated withing a subreport, totalize it, and show it at the end of the master report.

The problem is that the information inside the subreport came from its own query.

The subreport is shown in the details section and is generated for each item in the master.

 

I thought in two options:

1) change one parameter inside the subreport that will contain the total. (Is that possible?)

2) Use a scriptlet to do it. But the object will "survive" from the multiple calls to the subreport?

 

I will apreciate any help or sugestions.

 

Thanks

 

Lecio

 

 

By: Teodor Danciu - teodord

RE: Returning values from a subreport

2002-11-06 13:10

 

Hi,

 

You can return values from subreports using

special parameters as containers.

 

I shall give an example here.

 

Problem:

I want to pass to my master report the total

number of records that the subreport had.

 

Solution:

In the master report, I define a special container

parameter that will hold the values that

I want to return from the subreport.

 

<parameter name="ReturnedValuesMap" class="java.util.Map">

<defaultValueExpression>new java.util.HashMap()</defaultValueExpression>

</parameter>

 

I pass this container parameter to my subreport,

so that it can store the returned values in it.

 

<subreportParameter name="ReturnedValuesMap">

<subreportParameterExpression>

$P{ReturnedValuesMap}

</subreportParameterExpression>

</subreportParameter>

 

In the subreport template, I declare the container

parameter. Even they both have the same name,

the master report parameter and the subreport

parameter are in fact totally different entities.

 

<parameter name="ReturnedValuesMap" class="java.util.Map"/>

 

In the subreport, I use the dummy

<printWhenExpression> of an invisible line element

placed on the summary section to put in the

container parameter the value that I want to return

to my master report:

 

<line>

<reportElement x="0" y="0" width="0" height="0">

<printWhenExpression>

($P{ReturnedValuesMap}.put("MY_RETURNED_VALUE", $V{REPORT_COUNT}) == null)?Boolean.FALSE:Boolean.FALSE

</printWhenExpression>

</reportElement>

</line>

 

Remember that the returned value can be placed in

the container parameter also using scriptlets.

I chose this trick for simplicity reasons.

 

Back in the master report, if I want to display the

returned value, I just extract it from the container

parameter:

 

<textField evaluationTime="Group" evaluationGroup="DetailGroup">

<reportElement x="335" y="50" width="175" height="15"/>

<textFieldExpression class="java.lang.Integer">

$P{ReturnedValuesMap}.get("MY_RETURNED_VALUE")

</textFieldExpression>

</textField>

 

You might ask why is it that the text field uses

evaluationTime="Group".

This is because one of the problems with the

returned values from subreports is that these

values are returned after the content of the band

elements has already been evaluated.

If you want to display subreport returned values in

the same band as the subreport itself, you'll end up

seeing that they were returned too late for that.

 

So, in order to be able to display the returned

values in the same master report band that

contains the subreport itself, I had to delay the text

field evaluation. This was done by introducing a

dummy group in the master report and let my text

field be evaluated when this dummy group ends.

This dummy group breaks with every record in the

data source and has no header and footer of its own.

 

<group name="DetailGroup">

<groupExpression>$V{REPORT_COUNT}</groupExpression>

</group>

 

This dummy group served my purpose because my

subreport and the text field were placed in the

detail section of the master report and it had to

break with every detail. If the subreport is placed in

a different report section, the dummy group

expression should be adapted to fit the given

situation.

The use of a dummy group is not necessary if you

do not need to display the returned values in the

same report section that contains the subreport

itself.

 

I hope this helps.

Teodor

 

 

 

By: Lecio Nunes - lnunes

RE: Returning values from a subreport

2002-11-07 11:26

Hi Teodor,

 

I did it and used scriptlets too.

It worked fine.

 

Thank you very much.

 

Lecio.

 

 

By: jaydee77ca - jaydee77ca

RE: Returning values from a subreport

2005-01-20 09:38

I'm trying to accomplish a more complicated version of this.

 

Basically, each detail line in my master report contains a subreport and these detail lines are also grouped in my subreport. I want to keep a running total of a field in the subreport to display in the group footer on the master report.

 

I modified your example slightly so that for each detail subreport instead of just putting the value in to the map, I first get the current map value, then add the value of the field in the current subreport detail, then put that value back in the map. Then in the group footer on the master report I display the value.

 

This doesn't seem to be working properly as the total in the map seems to get reset at arbitrary intervals (I show it in the subreport so I can see it) and when I display the value in the footer I get null. Any ideas?

 

Thanks,

 

jaydee

 

 

By: Samir Desai - desaisamirkir

RE: Returning values from a subreport

2005-01-24 04:21

I did tried to return value from subreport using tips given by teodord, however, it displays null value in the master report.

 

Can someone give running example of how to do it ?

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...