Project Wiki
Views

Filtering on a date range

From Wiki

Jump to: navigation, search

You will often want to prompt users to restrict the data they retrieve based on a date range. This article assumes you design the report in iReport and publish it to JasperReports Server for shared use.

In iReport

  1. Create two parameters in your report.
  2. Name: start_date
    Parameter class: java.util.Date
    Use as prompt: checked
    Default Value Expression: leave blank/null

    Name: end_date
    Parameter class: java.util.Date
    Use as prompt: checked
    Default Value Expression: leave blank/null or you can use new java.util.Date() to populate the parameter with the current date

    Image:DateBetween_p2.png

  3. Add this to your query:
  4.     WHERE <your_table.column> BETWEEN $P{start_date} AND $P{end_date}
    
  5. Save and then test by clicking Preview in iReport.
  6. Assuming it runs well, you can load the file to JasperReports Server. You can stay within iReport to do this.

  7. Open the Repository Navigator and navigate to a folder where you want to save the report.
  8. Right-click the folder and click Add > Report Unit.
  9. Enter the name, label etc. and click Next.
  10. Click the Get source from current opened report button and click Next.
  11. Locate the Data Source from the repository (e.g., Dev01) and click Finish.

When prompted if you want to open the file from the server, click yes.

In JasperReports Server

Here you will add the Input Controls that map run-time values (generally user input) to the report parameters. There are several ways to add input controls. This is just one of them.

Switch to JasperReports Server and login as an administrator (jasperadmin or superuser, for example).

  1. Right-click the report in the Repository browser. Select Edit, then click Controls & Resources on the left sidebar.
  2. Click the Add Control button.
  3. Unless you already have the input control defined, click Locally Defined.
  4. Enter the following for Input Control Details and when done click Next.
    • Parameter Name: start_date [Note: it is important that the parameter name exactly matches the parameter defined in the report definition]
    • Prompt Text: Enter a start date
    • Type: Single Value
    • Mandatory: (optional)
    • Read Only: (optional)
    • Visible: checked
  5. Locate Datatype: Click From the Repository and select one of the sample date types that Jaspersoft includes, e.g., /organizations/organization_1/datatypes/date. Click Next.
  6. Repeat steps 2-4, but enter end_date for the Input Control name.
  7. Click the Always prompt checkbox. You can display the Controls layout on the “Top of page” if you like. Click Finish, then Save.

Test the report

Your report should run with a calendar input control, as shown below.

Image:dateBetween.png