Skip to main content

Post 11: iReport: Displaying a field along with its label in the body of your report and handling null values

Displaying a field along with its label in the body of your report and handling null values

This post teaches you the simplest operation you can do inside the body of your report. This includes displaying your report data as name-value pairs in the body. The name-value pair  consists of a label (the name) and a field value (the value). The post also teaches you  how to associate the field value with a column in your database table.

Getting ready 
Refer to the Post6:  iReport: Installation of PostgreSQL, which shows how you can install and run PostgreSQL. Note that your installation of PostgreSQL should be up and running before you proceed.

Refer to the post named Copy Sample Data Into Postgres  which helps you to create a database named jasperdb2and copy sample data for this post into the database. 

How to do it...
1.  Create the SingleFieldBody.jrxml file such as following the Designer tab of iReport shows a title and a header: 





2.  Drag-and-drop a new static text component from the a new static text component from the Palette of components into the Detail 1 section of your report. Double-click the static text component and enter Invoice Value as its label. Position it to the extreme left of your report.


3. Now, drag-and-drop a text field component into the Detail 1 section. Place the text field to the right of the static text you already dropped and positioned in step 2. This text field will show the value of each invoice issued to a customer in a month. 




4. Open the Report query window by clicking the Report query button to the right of the Preview tab.

5.  Enter the following SQL query into the Report querywindow:
 

Select * from "public"."CustomerInvoices"
WHERE "public"."CustomerInvoices"."CustomerName" = 'Packt
Publishing'
AND "public"."CustomerInvoices"."InvoicePeriod" = 'Mar09'
 

The lower part of the Report query window will show all five columns of the CustomerInvoicestable. Click OK.

6.  Now you will link the text field you created in step 3 with the InvoiceValuecolumn of the CustomerInvoicestable. For this purpose, right-click the text field and select the Edit expressionoption from the pop-up menu. 

7.  Delete the existing text ($F{field}) from the Expression editorwindow and select Fields in the first column of the lower half of the Expression editorwindow. Then select InvoiceValuein the second column.

8.  Double-click InvoiceValue in the second column and click the Apply button. This will attach InvoiceValue with the text field: 

9.Now you will decrease the height of the Detail 1 section. For this purpose, click inside Now you will decrease the height of the Detail 1 section. For this purpose, click inside the Detail 1 section; its properties will appear below the Palette of components on the right of your iReport main window. 

Set 40 as the value of the Band height property in the Properties window. The Detail 1 section will look as follows:


10. Switch to the Preview tab and you will see that your report looks like the following. Also note that your report body contains null values, shown encircled in the screenshot here:


11. Click on the $F{InvoiceValue} text field component in the Detail 1 section.  Its properties will appear in the Properties window just below the Paletteof  components. Find the Blank When Null property and check it, as shown in the
following screenshot:



 12. Click the Preview button and you will see that the null values are replaced with blank spaces, as shown in the following screenshot:






How it works...
Switch to the XM Ltab and you will see the following JRXML code:

1:  <?xml version="1.0" encoding="UTF-8"?>  
2:  <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="SingleFieldBody" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="45022e96-c649-453f-aa0f-c0913e9dc6ae">  
3:       <property name="ireport.zoom" value="1.0"/>  
4:       <property name="ireport.x" value="0"/>  
5:       <property name="ireport.y" value="0"/>  
6:       <queryString>  
7:            <![CDATA[SELECT * from "public"."CustomerInvoices"  
8:  WHERE "public"."CustomerInvoices"."CustomerName" = 'Packt Publishing'  
9:  AND "public"."CustomerInvoices"."InvoicePeriod" = 'Mar09']]>  
10:       </queryString>  
11:       <field name="InvoiceID" class="java.lang.Integer"/>  
12:       <field name="CustomerName" class="java.lang.String"/>  
13:       <field name="ProductName" class="java.lang.String"/>  
14:       <field name="InvoicePeriod" class="java.lang.String"/>  
15:       <field name="InvoiceValue" class="java.lang.Float"/>  
16:       <background>  
17:            <band splitType="Stretch"/>  
18:       </background>  
19:       <title>  
20:            <band height="79" splitType="Stretch">  
21:                 <staticText>  
22:                      <reportElement uuid="4848cf79-2279-4cfe-9942-4a3d54de0a0a" x="70" y="10" width="415" height="42"/>  
23:                      <textElement>  
24:                           <font fontName="Verdana" size="24"/>  
25:                      </textElement>  
26:                      <text><![CDATA[Māsika Customer Invoice values]]></text>  
27:                 </staticText>  
28:            </band>  
29:       </title>  
30:       <pageHeader>  
31:            <band height="50" splitType="Stretch">  
32:                 <textField>  
33:                      <reportElement uuid="27cef21d-a11b-47e9-a2c6-6ea0a1bb69cd" x="115" y="18" width="100" height="20"/>  
34:                      <textElement/>  
35:                      <textFieldExpression><![CDATA[$F{CustomerName}]]></textFieldExpression>  
36:                 </textField>  
37:                 <staticText>  
38:                      <reportElement uuid="9102308a-0c54-47d5-9d05-5be892e44680" x="15" y="18" width="100" height="20"/>  
39:                      <textElement/>  
40:                      <text><![CDATA[Customer Name:]]></text>  
41:                 </staticText>  
42:                 <staticText>  
43:                      <reportElement uuid="89d70f8d-83af-4ad5-9339-38fe49919d09" x="337" y="18" width="100" height="20"/>  
44:                      <textElement/>  
45:                      <text><![CDATA[Invoice Period:]]></text>  
46:                 </staticText>  
47:                 <textField>  
48:                      <reportElement uuid="ef839e8a-c9bf-450e-97e8-65e7d8202d29" x="437" y="18" width="100" height="20"/>  
49:                      <textElement/>  
50:                      <textFieldExpression><![CDATA[$F{InvoicePeriod}]]></textFieldExpression>  
51:                 </textField>  
52:            </band>  
53:       </pageHeader>  
54:       <columnHeader>  
55:            <band height="10" splitType="Stretch"/>  
56:       </columnHeader>  
57:       <detail>  
58:            <band height="40" splitType="Stretch">  
59:                 <staticText>  
60:                      <reportElement uuid="09892c33-04f5-4118-961a-10f93ae59894" x="16" y="11" width="100" height="20"/>  
61:                      <textElement/>  
62:                      <text><![CDATA[Invoice Value:]]></text>  
63:                 </staticText>  
64:                 <textField isBlankWhenNull="true">  
65:                      <reportElement uuid="3068cf40-557f-4d30-85cc-e2f4bf5644db" x="116" y="11" width="100" height="20"/>  
66:                      <textElement/>  
67:                      <textFieldExpression><![CDATA[$F{InvoiceValue}]]></textFieldExpression>  
68:                 </textField>  
69:            </band>  
70:       </detail>  
71:       <columnFooter>  
72:            <band height="45" splitType="Stretch"/>  
73:       </columnFooter>  
74:       <pageFooter>  
75:            <band height="54" splitType="Stretch"/>  
76:       </pageFooter>  
77:       <summary>  
78:            <band height="42" splitType="Stretch"/>  
79:       </summary>  
80:  </jasperReport>  
You can see that the <queryString>tag is highlighted in this JRXML code. The <queryString>tag wraps the SQL query that you authored in step 5 of this post.

This query fetches all the records of the CustomerInvoices table.
You will also see a <detail>tag in the preceding JRXML code. The <detail>tag represents the body of your report, which you just authored in this recipe. The <band> child of the <detail>tag specifies the height of the section that you set in step 9.


The <band>tag contains a pair of <staticText>and <textField>tags. This pair of tags represents the label and value components you dragged-and-dropped in steps 2 and 3.
The <textField>tag has a child named <textFieldExpression>, shown highlighted in this JRXML code. The <textFieldExpression>tag has an expression ($F{InvoiceValue}) as its content. This $F{InvoiceValue}expression attaches the text
field with the InvoiceValue column of your database. You authored this attachment in step 7 of the recipe, when you double-clicked InvoiceValuein the field expression window.


Finally, note that anything put inside the <detail>tag will be repeated for every record. You can simply say that JasperReports evaluates the data of the Detail 1section once for every record. Then it appends the evaluated data at the end of what it already contains from the previous evaluation. This way JasperReports evaluates the Detail 1section record by record until all the records are delivered. This is why the preview of step 10 shows the invoice values for all the records.


Comments

Popular posts from this blog

TIME TABLE: VTU BE/B.Tech June/July 2016 Exam Time table Draft Layout

Semester-wise June-July 2016 Exam time table (Draft Layout) Are You Using updated Kwikstudy App?

Walkins Bangalore: 01st-Oct-2016

1. UTC Aerospace : GET – S&IS-Software Team hiring Electronics & CSE / IT Freshers ! Walkin on 1st OCT | BE/BTECH - 2015 & 2016 both batch can try Selection Process : Written Test Group Discussion Technical & HR Interviews How To Apply : It is a walk-in drive for 2016 batch eligible freshers on 1 Oct 2016 at Bangalore location. Interested and eligible candidates can walk-in to the following venue on 1 Oct 2016 Date of Drive : 01 Oct 2016 Reporting Time : 8:00 AM to 11:30 AM Venue : UTC Aerospace Systems – Site 1, Sy. Nos. 14/1 & 15/1, Maruthi Industrial State, Phase 2, Hoody Village, Whitefield Road, K R Puram Hobli, Bangalore – 560048 ______________________________________ 2. HP : Software Eng/ Software Tester (Call based ,less chance of getting entry , try at your own risk) Event Date : 1-October-2016 Event Time : 8 : 00 am Job Location : Bangalore / Chennai Job Description: Software Engineer /

How to get VTU Provisional Degree Certificate (PDC) ?

Here is the procedure to get the Provisional Degree Certificate from the VTU by passed out candidates. You can get the certificate through post by sending the required documents to VTU. Documents and things required: 1. Provisional Degree Certificate (PDC) application form 2. DD of specified fees amount or print copy of receipt if you paid the fees online 3. A letter to the VTU registrar 4. A4 size envelope Step 1: Fill the PDC application You can download the PDC application form from the VTU website. Click below link for application download. PDC application form download     After downloading the application take a print of it and properly fill the form. Step 2: Make a DD of specified amount as the fees of PDC: Go to the bank and make the DD of prescribed fee amount for the PDC. The fee amount is specified in the above link from where you downloaded the application. Make the DD in favor of " Finance Officer, VTU Belgaum ".   Wr