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.

Getting ready

Refer to the installPostgreSQL.txt file included in the source code download for this chapter to install and run PostgreSQL, which should be up and running before you proceed. The source code for this chapter also includes a file named copySampleDataIntoPGS.txt, which helps you create a database named jasperdb2 and copy sample data for this recipe into the database.

How to do it...

Following simple steps demonstrate how you can filter records to be shown in a report:

  1. Open the FilteringReportRecords.jrxml file from the Task4 folder of the source code for this chapter. The Designer tab of iReport shows a report with a simple table in its body, as shown in the following screenshot:
    How to do it...
  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:
    How to do it...
  3. Type Packt Publishing and 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:
    How to do it...
  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.
    How to do it...
  5. The Parameters node will expand to show the newly added parameter named parameter1 at the end of the parameters list. Select parameter1 from the list; its properties will appear in the Properties window 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 Expression property. A Print When Expression window will open, as shown in the following screenshot:
    How to do it...
  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:
    How to do it...
  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:
    How to do it...
  10. Type Packt Publishing, Mar09, and 5000 as 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 5000 for 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 to do it...

How it works...

You have used a combination of parameters and Print When expression features of JasperReports in this recipe. 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 recipe.

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 InvoiceValue field in a record is greater than or equal to the value that a user enters as the MinInvoiceValue parameter. You can see this by comparing the previews of steps 3 and 10.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.106.7