Creating a simple table of records along with labels for each column
In this post you will learn how to design and display a table formulation in the main body of your report. Table formulation includes the following steps:
1. Placing column headings in the table.
2. Linking your report to fields in your database.
3. Placing data fields under their corresponding column headings.
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...
The following steps take you through creating a table:
1. Create the SimpleTable.jrxml file such that the Designer tab of iReport shows an empty report with a title (Māsika Customer Invoices) as shown in the following screenshot:
2. Select a static text component from the palette and drag it into the Column Header section of your report.
3. Double-click on the static text component, and the text inside will be selected; type Invoice Number and press Enter.
4. While the static text component is selected, select the Left property from the Properties section of the Invoice Number - Properties window below the palette. Change the value of this property to 0, as shown in the following screenshot:
5. Similarly, change the Top property value to 0. The static text component will be positioned to the top-left area in the Column Header section of your report, as shown in the following screenshot:
6. From the Report Inspector window on the left side of the report window, try to expand the Fieldsnode. You will notice that it is empty and, therefore, not expandable, as shown in the following screenshot:
7. Click the Report Query Button at the top of the report window.
8. A Report query dialog will appear. While your cursor is in query editor, type
SELECT * FROM "public"."CustomerInvoices".
Five field names of the CustomerInvoicestable (InvoiceID, CustomerName, ProductName, InvoicePeriod, and InvoiceValue) will appear in the bottom section, as shown in the following screenshot:
9. Click the OK button to dismiss the dialog. From the Report Inspector window on the left side of the report window, expand the Fields node. You will notice that this time it contains InvoiceID, CustomerName, ProductName, InvoicePeriod, and InvoiceValue
fields, as shown in the following screenshot:
10. Drag-and-drop the InvoiceID field from the Fields node into the Detail 1 section of your report. A text field component with the expression $F{InvoiceID}will appear in the Detail 1 section of your report, as shown in the following screenshot:
11. Using the arrow keys, move your text field component, so that it appears immediately below the blue line that separates the Column Header and the Detail 1 sections.
12. Now align this text field component using the arrow keys just below the static text component of step 2 (which is a label for the InvoiceIDField), as shown in the following screenshot:
13. Double-click the blue line that separates the Column Header section and the Detail 1 section. This will adjust the band height for the Column Header section, as shown in the following screenshot:
14. Similarly, add the CustomerName, ProductName, InvoicePeriod, and InvoiceValue table fields along with their corresponding static text labels into your report. After adding these components your report will look similar to the one in the following screenshot:
15. Switch to the Preview tab to see the result of your work, as follows:
16. Notice that in your table in the Preview tab there is a large white space between each record of your table
17. Switch back to the Designer tab. Double-click on the blue line that separates the Detail 1 section and the Column Footer section. This will adjust the band height for the Detail 1 section, as shown in the following screenshot:
18. Switch to the Preview tab to see the result of your work, as shown in the following screenshot:
How it works...
Switch to the XML tab to see the following JRXML code:
These three tags form the report body (the table) that you designed in this recipe. I have omitted other tags for clarity.
Note that when you executed steps 2 to 5 of the recipe, iReport authored the <columnHeader>tag. Similarly, when you executed steps 10 to 12, iReport authored the <detail>tag.
As the recipe did not do anything with the column footer, the <columnFooter>tag is empty.
In this post you will learn how to design and display a table formulation in the main body of your report. Table formulation includes the following steps:
1. Placing column headings in the table.
2. Linking your report to fields in your database.
3. Placing data fields under their corresponding column headings.
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 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.
How to do it...
The following steps take you through creating a table:
1. Create the SimpleTable.jrxml file such that the Designer tab of iReport shows an empty report with a title (Māsika Customer Invoices) as shown in the following screenshot:
2. Select a static text component from the palette and drag it into the Column Header section of your report.
3. Double-click on the static text component, and the text inside will be selected; type Invoice Number and press Enter.
4. While the static text component is selected, select the Left property from the Properties section of the Invoice Number - Properties window below the palette. Change the value of this property to 0, as shown in the following screenshot:
5. Similarly, change the Top property value to 0. The static text component will be positioned to the top-left area in the Column Header section of your report, as shown in the following screenshot:
6. From the Report Inspector window on the left side of the report window, try to expand the Fieldsnode. You will notice that it is empty and, therefore, not expandable, as shown in the following screenshot:
7. Click the Report Query Button at the top of the report window.
8. A Report query dialog will appear. While your cursor is in query editor, type
SELECT * FROM "public"."CustomerInvoices".
Five field names of the CustomerInvoicestable (InvoiceID, CustomerName, ProductName, InvoicePeriod, and InvoiceValue) will appear in the bottom section, as shown in the following screenshot:
9. Click the OK button to dismiss the dialog. From the Report Inspector window on the left side of the report window, expand the Fields node. You will notice that this time it contains InvoiceID, CustomerName, ProductName, InvoicePeriod, and InvoiceValue
fields, as shown in the following screenshot:
10. Drag-and-drop the InvoiceID field from the Fields node into the Detail 1 section of your report. A text field component with the expression $F{InvoiceID}will appear in the Detail 1 section of your report, as shown in the following screenshot:
11. Using the arrow keys, move your text field component, so that it appears immediately below the blue line that separates the Column Header and the Detail 1 sections.
12. Now align this text field component using the arrow keys just below the static text component of step 2 (which is a label for the InvoiceIDField), as shown in the following screenshot:
13. Double-click the blue line that separates the Column Header section and the Detail 1 section. This will adjust the band height for the Column Header section, as shown in the following screenshot:
14. Similarly, add the CustomerName, ProductName, InvoicePeriod, and InvoiceValue table fields along with their corresponding static text labels into your report. After adding these components your report will look similar to the one in the following screenshot:
15. Switch to the Preview tab to see the result of your work, as follows:
16. Notice that in your table in the Preview tab there is a large white space between each record of your table
17. Switch back to the Designer tab. Double-click on the blue line that separates the Detail 1 section and the Column Footer section. This will adjust the band height for the Detail 1 section, as shown in the following screenshot:
18. Switch to the Preview tab to see the result of your work, as shown in the following screenshot:
How it works...
Switch to the XML tab to 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="SimpleTable" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b7db4c77-00d8-4551-b0cf-f401d8a3bb7a">
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 *
8: FROM "public"."CustomerInvoices"]]>
9: </queryString>
10: <field name="InvoiceID" class="java.lang.Integer"/>
11: <field name="CustomerName" class="java.lang.String"/>
12: <field name="ProductName" class="java.lang.String"/>
13: <field name="InvoicePeriod" class="java.lang.String"/>
14: <field name="InvoiceValue" class="java.lang.Float"/>
15: <variable name="CustomerName_1" class="java.lang.Integer" resetType="Column" calculation="Count">
16: <variableExpression><![CDATA[$F{CustomerName}]]></variableExpression>
17: </variable>
18: <variable name="CustomerName_2" class="java.lang.Integer" resetType="Column" calculation="DistinctCount">
19: <variableExpression><![CDATA[$F{CustomerName}]]></variableExpression>
20: </variable>
21: <background>
22: <band splitType="Stretch"/>
23: </background>
24: <title>
25: <band height="79" splitType="Stretch">
26: <staticText>
27: <reportElement uuid="5b61e82c-eb8b-448d-9296-88d688405d04" x="99" y="29" width="357" height="31"/>
28: <textElement>
29: <font fontName="Verdana" size="24" isBold="true"/>
30: </textElement>
31: <text><![CDATA[Māsika Customer Invoices]]></text>
32: </staticText>
33: </band>
34: </title>
35: <pageHeader>
36: <band height="35" splitType="Stretch"/>
37: </pageHeader>
38: <columnHeader>
39: <band height="20" splitType="Stretch">
40: <staticText>
41: <reportElement uuid="7e8d8434-00fa-4d90-9e85-e9c1e25d79f9" x="0" y="0" width="100" height="20"/>
42: <textElement/>
43: <text><![CDATA[Invoice Number]]></text>
44: </staticText>
45: <staticText>
46: <reportElement uuid="46edbbea-48cb-4dac-b44d-15be5652c067" x="99" y="0" width="100" height="20"/>
47: <textElement/>
48: <text><![CDATA[CustomerName]]></text>
49: </staticText>
50: <staticText>
51: <reportElement uuid="38b26b45-a350-48f7-ae69-db68fd5d9d7a" x="200" y="0" width="100" height="20"/>
52: <textElement/>
53: <text><![CDATA[ProductName]]></text>
54: </staticText>
55: <staticText>
56: <reportElement uuid="c10d03b9-70fa-432c-b130-f8d97d1678ae" x="300" y="0" width="100" height="20"/>
57: <textElement/>
58: <text><![CDATA[InvoicePeriod]]></text>
59: </staticText>
60: <staticText>
61: <reportElement uuid="3730b4fe-58a6-4bfe-b87f-60e4a788d4a2" x="400" y="0" width="100" height="20"/>
62: <textElement/>
63: <text><![CDATA[InvoiceValue]]></text>
64: </staticText>
65: </band>
66: </columnHeader>
67: <detail>
68: <band height="20" splitType="Stretch">
69: <textField>
70: <reportElement uuid="72b3391b-5d23-46bc-acc2-49e921884dd0" x="0" y="0" width="100" height="20"/>
71: <textElement/>
72: <textFieldExpression><![CDATA[$F{InvoiceID}]]></textFieldExpression>
73: </textField>
74: <textField>
75: <reportElement uuid="c98f6ecd-8e37-411f-b9bd-617f19d9ba55" x="100" y="0" width="100" height="20"/>
76: <textElement/>
77: <textFieldExpression><![CDATA[$F{CustomerName}]]></textFieldExpression>
78: </textField>
79: <textField>
80: <reportElement uuid="42a77347-0929-4bce-84e9-2b4263997d7e" x="200" y="0" width="100" height="20"/>
81: <textElement/>
82: <textFieldExpression><![CDATA[$F{ProductName}]]></textFieldExpression>
83: </textField>
84: <textField>
85: <reportElement uuid="289e5ed7-f732-447e-b21e-cf571e111b7d" x="300" y="0" width="100" height="20"/>
86: <textElement/>
87: <textFieldExpression><![CDATA[$F{InvoicePeriod}]]></textFieldExpression>
88: </textField>
89: <textField>
90: <reportElement uuid="51713b35-304d-4e06-8967-4c00a8be9e26" x="400" y="0" width="100" height="20"/>
91: <textElement/>
92: <textFieldExpression><![CDATA[$F{InvoiceValue}]]></textFieldExpression>
93: </textField>
94: </band>
95: </detail>
96: <columnFooter>
97: <band height="45" splitType="Stretch"/>
98: </columnFooter>
99: <pageFooter>
100: <band height="54" splitType="Stretch"/>
101: </pageFooter>
102: <summary>
103: <band height="42" splitType="Stretch"/>
104: </summary>
105: </jasperReport>
You can see the <columnHeader>, <detail>, and <columnFooter> tags in this code. These three tags form the report body (the table) that you designed in this recipe. I have omitted other tags for clarity.
Note that when you executed steps 2 to 5 of the recipe, iReport authored the <columnHeader>tag. Similarly, when you executed steps 10 to 12, iReport authored the <detail>tag.
As the recipe did not do anything with the column footer, the <columnFooter>tag is empty.
Comments
Post a Comment