Skip to main content

Post 15: iReport: Implementing groups within groups - a nested hierarchy

Implementing groups within groups - a  nested hierarchy
JasperReports allows you to implement a nested hierarchy of groups in a report. A hierarchy  means that a group will contain subgroups.

Subgroups are useful when you want to design a report with multiple levels of logical grouping. For example, a report showing all customer invoices for a particular month may contain a large number of records. You can segregate the records with customer names; that is, all invoices of a particular customer will be grouped together. This is one level of logical grouping.


Now you can introduce another level of logical grouping by further segregating all invoices of a particular customer into subgroups based on products sold to a customer. This will become the second level of logical grouping.


This post demonstrates how to design a report with two levels of logical grouping.


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 jasperdb2 and copy sample data for this post into the database.

How to do it...
1.  Open the GroupInGroup.jrxml file such that the Designer tab of iReport shows a report with a main title
Māsika Customer Invoices, as shown in the following screenshot: 
 

For more information about creating above GroupInGroup.jrxml file please refer  Post 13: iReport: Inserting a heading for a group of records 

Query Used:
SELECT *
FROM "public"."CustomerInvoices" as a
ORDER BY a."CustomerName"
 

2.  Switch to the Preview tab to see the current preview of your report. You will see a table of records, which are grouped with customer names. The heading of each customer group is displayed before the start of each group. You will also notice that the records within a group are not arranged or grouped.



3.  Now we are about to arrange records within a group using a nested group. Switch  to the Designer tab. Right-click on the top-most top element  in the Report Inspector. A pop-up menu will appear. Select the Add Report Group option from the pop-up menu, as shown in the following screenshot:
 

4.  A New group wizard dialog will appear, as shown in the  following screenshot: 




5.  Type Product in the Group name and select ProductName Field in the Group by the  following report object:option, as shown in the following screenshot:
 

 6.  Click the Next button, and the dialog will change, as in the following screenshot:


7.  Click the Finish button to dismiss the dialog. You will notice that Product Group Header 1 and Product Group Footer 1 sections have been added into your report, as shown in the following screenshot:


8.  Click the Report query button at the top of the report window (on the right side of the Preview tab). You can find it by its icon. It is similar in shape to a standard database icon  . As you click on this button, a Report query dialog will appear, as shown in
the following screenshot:

 

9.  In the query editor bring your cursor to the end of the text ORDER BY a."CustomerName"and type , a. "ProductName". Click the OK button to dismiss the dialog, as shown in the following screenshot:


10) 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:



11. Drag-and-drop the ProductName field from the Fields node into the Product Group Header 1 section of  your report. 

A text field component with the expression $F{ProductName}will appear in the Product Group Header 1 section of your report, as shown in the following screenshot:


12. Switch to the Preview tab to see a preview of your report. You will see a grouping of records based on customer names and inside the customer group a grouping based on product names. A heading for each product group is also used for distinction, as
shown in the following screenshot 


13. Navigate through all the pages. You will notice that all product names are grouped under the main grouping of customer names, as shown in the following screenshot:



How it works...
The nested group that you formed in this recipe is similar to the group described in the 
Post 13: iReport: Inserting a heading for a group of records. The only difference is that  the nested group formed in this recipe is based on product names, whereas grouping in the original (Inserting a heading for a group of records) post is based on customer names. This recipe actually fits product name grouping as a nested group inside customer name grouping. You can learn how the grouping works from the earlier Post 13: iReport: Inserting a heading for a group of 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