Using parameters to filter records during report processing:
JasperReports allows you to filter records during report processing and show only those of interest in a particular report. This recipe teaches you how to use this feature of filtering of records during report processing.
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...
Following simple steps demonstrate how you can filter records to be shown in a report:
1. Create the FilteringReportRecords.jrxml file as the Designer tab of iReport shows a report with a simple table in its body, as shown in the following screenshot:
2. Switch to the Preview tab, and Parameter prompt dialogs will appear, which will ask you for a CustomerName and InvoicePeriod parameters, as shown in the following screenshot:
3. Type Packt Publishingand Mar09 as the value for the customer name and invoice month parameters, respectively. You will see a report of invoices for the particular customer for a specific invoicing month, as follows:

4. Switch back to the Designer tab. Right-click on the Parameters node in the Report Inspector window on the left of the Designer tab, as shown next. A pop-up menu will appear. Choose the Add Parameter option from the pop-up menu.

5. The Parameters node will expand to show the newly added parameter named parameter1at the end of the parameters list. Select parameter1from the list; its properties will appear in the Propertieswindow below the palette of components on the right of your iReport main window.
6. Click on the Name property of the parameter and type MinInvoiceValue as its value. Now click the Parameter Class property and change its value to java.lang.Integer. Leave the rest of the parameter properties at their default values.
7. Click inside the Detail 1 section; its properties will appear below the palette of components. Click the button beside the Print When Expressionproperty.
A Print When Expression window will open, as shown in the following screenshot:
8. Add the expression text $F{InvoiceValue}.intValue() >=
$P{MinInvoiceValue}in the editor window and click the OK button, as shown in the following screenshot:

9. Switch to the Preview tab; Parameter prompt dialogs will appear, which will ask you for a customer name, invoicing month, and minInvoice value parameters, as shown in the following screenshot:

10. Type Packt Publishing, Mar09, and 5000as values for the customer name, invoicing month, and min invoice value parameters, respectively and click OK. You will see a report of invoices whose values are greater than 5000for the particular
customer for the specific invoicing month. Note that this report is a subset of the report shown in the preview of step 3.

How it works...
You have used a combination of parameters and Print When expression features of JasperReports in this post. You use parameters to allow users of your report to enter data just before report processing. For example, you included a parameter named MinInvoiceValue in step 6 of the post.
Then you used the MinInvoiceValue parameter in the $F{InvoiceValue}.intValue() >= $P{MinInvoiceValue} expression in step 8. This expression prints a record only when the InvoiceValuefield in a record is greater than or equal to the value that a user enters as the MinInvoiceValueparameter. You can see this by comparing the previews of steps 3 and 10.
JasperReports allows you to filter records during report processing and show only those of interest in a particular report. This recipe teaches you how to use this feature of filtering of records during report processing.
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...
Following simple steps demonstrate how you can filter records to be shown in a report:
1. Create the FilteringReportRecords.jrxml file as the Designer tab of iReport shows a report with a simple table in its body, as shown in the following screenshot:
2. Switch to the Preview tab, and Parameter prompt dialogs will appear, which will ask you for a CustomerName and InvoicePeriod parameters, as shown in the following screenshot:
3. Type Packt Publishingand Mar09 as the value for the customer name and invoice month parameters, respectively. You will see a report of invoices for the particular customer for a specific invoicing month, as follows:
4. Switch back to the Designer tab. Right-click on the Parameters node in the Report Inspector window on the left of the Designer tab, as shown next. A pop-up menu will appear. Choose the Add Parameter option from the pop-up menu.
5. The Parameters node will expand to show the newly added parameter named parameter1at the end of the parameters list. Select parameter1from the list; its properties will appear in the Propertieswindow below the palette of components on the right of your iReport main window.
6. Click on the Name property of the parameter and type MinInvoiceValue as its value. Now click the Parameter Class property and change its value to java.lang.Integer. Leave the rest of the parameter properties at their default values.
7. Click inside the Detail 1 section; its properties will appear below the palette of components. Click the button beside the Print When Expressionproperty.
A Print When Expression window will open, as shown in the following screenshot:
8. Add the expression text $F{InvoiceValue}.intValue() >=
$P{MinInvoiceValue}in the editor window and click the OK button, as shown in the following screenshot:
9. Switch to the Preview tab; Parameter prompt dialogs will appear, which will ask you for a customer name, invoicing month, and minInvoice value parameters, as shown in the following screenshot:
10. Type Packt Publishing, Mar09, and 5000as values for the customer name, invoicing month, and min invoice value parameters, respectively and click OK. You will see a report of invoices whose values are greater than 5000for the particular
customer for the specific invoicing month. Note that this report is a subset of the report shown in the preview of step 3.
How it works...
You have used a combination of parameters and Print When expression features of JasperReports in this post. You use parameters to allow users of your report to enter data just before report processing. For example, you included a parameter named MinInvoiceValue in step 6 of the post.
Then you used the MinInvoiceValue parameter in the $F{InvoiceValue}.intValue() >= $P{MinInvoiceValue} expression in step 8. This expression prints a record only when the InvoiceValuefield in a record is greater than or equal to the value that a user enters as the MinInvoiceValueparameter. You can see this by comparing the previews of steps 3 and 10.
Comments
Post a Comment