Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get first letter of first and last row in Jasper Reports table

I am using JasperSoft Studio in Eclipse to generate a signup sheet for a church. The page header has the label of what last names will be on the page. So page 1 will have A-D, if the page starts with an A, and ends with a D.enter image description here

This is turning out to be very complicated for such an easy task. I just want to read what the last_name field is in the first and last row of every page, take the first letter of it, and then stick it in the header.

Any ideas are welcome, I am stumped.

like image 885
webminer07 Avatar asked Sep 27 '22 01:09

webminer07


1 Answers

To get the first and last value of a field in a page you use variables with resetType and set the correct evalutationTime on the textField (You will need to use 2 textField aligning them correctly one next to the other)

This example will show how to get the first and last value of the field $F{Last_Name} on every page

First value of a field in the page

The resetType on your variable will be resetType="None"

<variable name="firstValueOnPage" class="java.lang.String" resetType="None">
    <variableExpression><![CDATA[$F{Last_Name}]]></variableExpression>
</variable>

and in the textField, use the variable with evalution="Now" (that is default so no tag needed)

<textField>
   <reportElement x="30" y="19" width="100" height="20" uuid="e6421031-6db7-4fd9-995f-94cef2eb3621"/>
    <textFieldExpression><![CDATA[$V{firstValueOnPage}]]></textFieldExpression>
</textField>

Last value of a field in the page

The resetType of your variable is resetType="Page"

<variable name="lastValueOnPage" class="java.lang.String" resetType="Page">
    <variableExpression><![CDATA[$F{Last_Name}]]></variableExpression>
</variable>

and in the textField use the variable evaluationTime="Page"

<textField evaluationTime="Page">
    <reportElement x="170" y="19" width="100" height="20" uuid="9100baa5-0095-4dc3-ac79-2cd87562a92d"/>
    <textFieldExpression><![CDATA[$V{lastValueOnPage}]]></textFieldExpression>
</textField>

To get only the first char of first and last value I see that you have already figured it out, but to complete the answer the textFieldExpression would be

<textFieldExpression><![CDATA[($V{firstValueOnPage}!=null&&$V{firstValueOnPage}.length()>0)?String.valueOf($V{firstValueOnPage}.charAt(0)).toUpperCase():""]]></textFieldExpression>

Now just put the two textField's next to each other (align them correctly) and you will have the desired result.

Some additional information to learn more about resetType and evalutationTime (from jasper report api 6.2.0)

resetType

None - The variable is incremented with every record during the iteration through the data source Report - The variable never gets incremented during the report filling process.
Page - The variable is incremented with each new page.
Column - The variable is incremented with each new column.
Group - The variable is incremented every time the group specified by the incrementGroup attributes breaks

EvalutationTime

Auto Evaluation time indicating that each variable participating in the expression should be evaluated at a time decided by the engine.
Band The element will be evaluated at band end.
Column A constant specifying that an expression should be evaluated after each column is filled. Group A constant specifying that an expression should be evaluated after each group break.
Master Used for elements that are evaluated at the moment the master report ends.
Now A constant specifying that an expression should be evaluated at the exact moment in the filling process when it is encountered.
Page A constant specifying that an expression should be evaluated after each page is filled.
Report A constant specifying that an expression should be evaluated at the end of the filling process.

like image 192
Petter Friberg Avatar answered Sep 30 '22 08:09

Petter Friberg