Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I increment a variable with value of another variable in JasperReports?

I need to make a grand total of the items I'm counting in a subReport. To do that, I think I need to add the value of that variable to another variable for each iteration, or "increment" it by that value. The subReport gets called for each group, and I get a total for that group. I need to add the variable values, rather than database columns/fields.

I'm receiving an integer returnValue from the subReport, which is itself the count of rows in the sub-report. I want to get the grand total, since that subReport is called multiple times for the different results (each for a GROUP) from my main SQL query. I want to add up all the results, but I'm getting a null value. I tried adding an operation to the subReport as a new returnValue and choosing Sum as the operation, but that also yielded a null.


   <variable name="itemCount" class="java.lang.Integer" resetType="None"/>
   <variable name="grandCount" 
      class="java.lang.Integer" 
      incrementType="Group" 
      incrementGroup="ITEM_BUNDLE">
      <variableExpression><![CDATA[$V{itemCount}]]></variableExpression>
   </variable>

... <returnValue subreportVariable="countItems" toVariable="itemCount"/>

like image 310
sventechie Avatar asked Sep 29 '09 21:09

sventechie


People also ask

How do you increment a variable in Jasper report?

check your variable calculation type is "sum" or "None". if your calculation type is "None", variable behavior seems normal because I assume that you sum with a constant value. Set the calculation type "Sum" and Reser type "report", Go the textbox on the report that holds your variable. Set the avaluation time "Now".

How do I add a parameter to iReport?

Parameters promptDrag the parameter from the report inspector inside the title band. iReport creates a textfield to display the parameter value. Run the report using an empty data source by clicking the preview button. The parameter prompt dialog will appear asking for a value for the MESSAGE parameter.

How do I add a text field in Jaspersoft?

To add fields to your report, click the field in the Outline view and drag it to the Design view. When the field object is dragged inside the detail band, Jaspersoft Studio creates a text field element and sets the text field expression for that element.


2 Answers

Add attribute calculation="Sum" to variable name="grandCount"

or pass grandCount to subreport as parameter

<subreportParameter name="grandCount">
<subreportParameterExpression><![CDATA[$P{grandCount}]]></subreportParameterExpression>
</subreportParameter>

in subreport declare variable countItems with initialValue of parameter grantCount

<variable name="countItems" .... >
   <variableExpression><![CDATA[$P{itemCount} + $P{grandCount}]]></variableExpression>
   <initialValueExpression><![CDATA[$P{grandCount}]]></initialValueExpression>
</variable>

and return

<returnValue subreportVariable="countItems" toVariable="grandCount" calculation="Sum"/>
like image 179
cetnar Avatar answered Sep 19 '22 14:09

cetnar


Im not exactly shure how to write it in JRXML since i use iReport. In iReport, i create a new Variable, with class type "Integer", and calculation type "System" The calculation type is important here.

In the variable expression, you will need something like $V{grandCount} = $V{grandCount} + $V{itemCount}

NOTE: JasperReports render band by band, so you wont be able to use the grandCount variable in a band before the subreport band.

Hope im not too late

like image 30
medopal Avatar answered Sep 20 '22 14:09

medopal