Jasper Report - Current Number of Group in Page - jasper-reports

I have a PDF report split in several pages.
In every page I may have one or more groups. What I need is a count of current group number inside that page resetting at page change.
So the first group in page has 1, the next one 2 etc...
When I change a Page, the count has to restart from one.
I have found this good variable definition but it counts current group number for the entire report, ignoring the page.
<variable name="currentGroupNumber" class="java.lang.Integer" incrementType="Group" incrementGroup="GroupName">
<variableExpression><![CDATA[($V{GroupName_COUNT} == 1) ? $V{currentGroupNumber}+1 : $V{currentGroupNumber}]]></variableExpression>
<initialValueExpression><![CDATA[1]]></initialValueExpression>
</variable>
Any ideas?

The variable is missing resetType="Page"
Also, the variable looks a little more complex than it should, something like the following might do:
<variable name="currentGroupNumber" class="java.lang.Integer" calculation="Count" incrementType="Group" incrementGroup="GroupName" resetType="Page">
<variableExpression><![CDATA["foo"]]></variableExpression>
</variable>

Related

Crystal reports uses PreviousValue What is the equivalent in Jasper reports?

Crystal reports uses PreviousValue (fld) What is the equivalent in Jasper reports?
I have searched online for possible answers but as yet not found anything. Any help would be appreciated.
There's no direct way to get the previous value of a field in JasperReports, but it can be done using two variables like this (the variable class should match the field type):
<variable name="PreviousFldValue" class="java.lang.String">
<variableExpression><![CDATA[$V{CurrentFldValue}]]></variableExpression>
</variable>
<variable name="CurrentFldValue" class="java.lang.String">
<variableExpression><![CDATA[$F{fld}]]></variableExpression>
</variable>
You would then use $V{PreviousFldValue} as the previous value of the field.
Note that the order in which the variables are declared is important, the solution relies on PreviousFldValue being calculated before CurrentFldValue.

jasper report detect if datail band or subreport is present in current page

I'm developing a report with jasper.
The main report has several subreports.
Only one of these subreport has a fixed text with a note number, something like this.
my text<sup>(2)</sup>
In main report I must print the footnote, but only in all the pages in which the text with the note is present.
An image to better explain:
Is there something that permit to detect if a subreport is present in the current page?
Or something that permit to detect if a detail band is present in the current page?
Or something that permit to detect if a particular text will be printed in current page?
Or other strategy to handle this particular situation?
. Create Variable in main report 'subReportCount'
Type = Integer, default 0
Reset Type = 'Page'
. Place this variable (hidden) somewhere after Subreport
. Create variable 'SubVar' in subreport , expression == $V{REPORT_COUNT}
. In main, go to subreport properties -> Edit return Values - >
Set From Varible= SubVar , To Variable=subReportCount, Calculation type = Sum
. Page Footer Print When Expression = $V{subReportCount} > 0
This may work if you are trying if subreport is printed atleast once in page.
Does not matter if you place subreport variable in report or not.
I tried on my local , its working for similar situation
Variable in Main report
<variable name="subReportCount" class="java.lang.Integer" resetType="Page" incrementType="Page" calculation="Sum">
<variableExpression><![CDATA[$V{subReportCount}.equals( null ) ? 0 : $V{subReportCount}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
Return Value for the subreport
<returnValue subreportVariable="SubVar" toVariable="subReportCount"/>
Print when expression for Footer element, in my case I am showing Page number
<printWhenExpression><![CDATA[$V{subReportCount} > 0]]></printWhenExpression>
This should work , if not please share your jrxml and sample data.
Good Luck!

Can I change the value of a variable?

I'm new to JasperReports, but I kind of feel like I'm getting it. After all it is just another mark-up language.
My scenario is that I have a table of data, with the date and some other text columns, and I want to to display a subtotal for each section of the data based on a date. I am expecting zero, one, or many dates to be in the table.
I have a variable defined that calculates the sum of the rows, but I want to reset the sum to zero with each sub-group, is it possible?
Here is my Variable declaration:
<variable name="G_COUNT" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[(($F{ROW_NUMBER}.intValue() != 9)?$F{ITEM_COUNT}:new Integer(0))]]></variableExpression>
<initialValueExpression><![CDATA[new Integer(0)]]></initialValueExpression>
Is there a way I can use this variable, resetting to 0 when a new date is found, or am I using the wrong type to do what I want?
As a caveat, I am editing the .jrxml file in a text editor, not iReport.
What I was able to figure out from reading the dtd is that groups are available to trigger a reset event.
<variable name="G_COUNT" class="java.lang.Integer" calculation="Sum" resetType="Group" resetGroup="SubCalc">
<variableExpression><![CDATA[(($F{ROW_ORDINAL}.intValue() != 9)?$F{ITEM_COUNT}:new Integer(0))]]></variableExpression>
<initialValueExpression><![CDATA[new Integer(0)]]></initialValueExpression>
<group name="SubCalc">
<groupExpression><![CDATA[$F{ROW_ORDINAL} == 1]]></groupExpression>
</group>
So when the ROW_ORDINAL == 1 it triggers a reset event, which sets the value of G_COUNT to the initialValueExpression

JasperReport table column sum for page

I need TO add a column sum to column footer of table object for each page.
If I create variable and set sum aggregation function than sum value appear for full report. But I need sum column data for each page, not for full report.
Can anyone help me solve this problem?
This is not a solution for the jr:table component since it uses a subdataset where its not allowed to set resetType="Page", but using a subreport it can be used.
I don't think there is a work around in components (jr:table,jr:list) using a subdataset but would be happy If someone can prove me wrong
Solution using subreport
On your variable set resetType="Page"
es.
<variable name="var" class="java.lang.Double" resetType="Page" calculation="Sum"/>
....
</variable>
For more info on resetType see JRVariable API
On your textField set evaluationTime="Page"
es.
<textField evaluationTime="Page">
....
</textField>
For more info on evaluationTime see JRTextField API
In case of Dataset set resetType="Report"

How to make JasperReports groups start on new page only when needed?

I'm using JasperReports and iReport (output to PDF), and I have a problem with a certain report:
Inside the report I have defined a group. On average, each "instance" of that group only contains a small number of detail rows, so I don't want each of them to start on a new page, since it would be a waste of paper (the output is printed on paper).
But on the other hand, I don't want to have a group that starts on one page and ends on the next one. For example, if on page 1 I can fit the first two groups and half of the third group, I want the entire third group to start on page 2, and so on.
Is there any way to do that?
The "group" property keepTogether and isStartNewPage will solve your problem and makes group result together and always starts new group from new page.
<group name="CITY" isStartNewPage="true" keepTogether="true">
<groupExpression><![CDATA[$F{CITY}]]></groupExpression>
<groupHeader>
...
</groupHeader>
</group>
The <group> property keepTogether does exactly what you want! It was added in around Jasper 3.6.2, see this discussion.
So:
<group name="CITY" keepTogether="true">
<groupExpression><![CDATA[$F{CITY}]]></groupExpression>
<groupHeader>
...
</groupHeader>
</group>
Right Click on the detail band and Add another Detail Band. You can group your page using these detail band blocks.
Add sufficient height to each band so that it will not overlap on the new page
For displaying group data in new page in Table component we need to set the following properties.
Right on the group name --> Advanced properties --> select new page as true.
observe the output , the group data is displayed in new page.
Regards,
Harish
View > Report Groups > Modify > Min Heiht to Start New Page = 100