Friday 4 November 2011

iReport: how to add "All values" option inside an input control

This is a simple but effective workaround for iReport that adds the "All values" option inside a "single select query" or "multi select query" input control type.

  • First of all, in iReport we create a new parameter and we give it as default the string "All values" (or anything similar you may want to see in the input control)
  • Now we have to edit the source query of the report like this:
SELECT ... FROM ... 
WHERE ([COLUMN_TO_FILTER] = $P{parameter} OR ${parameter} = "All values")


$P{parameter} is the parameter we have just created, and [COLUMN_TO_FILTER] contains the list of values of the input control.

  • Now we can upload the report to JasperReports Server and start creating our input control like this:
    • Type: Single Select Query (or Multi-Select Query)
    • Check the "Mandatory" and "Visible" options
    • Query Resource: select "Locally defined" and click on "Edit Local Resource"
    • In tab Query write a source query like this:
SELECT DISTINCT [COLUMN_TO_FILTER] FROM ...

UNION
SELECT "All values" FROM DUAL
ORDER BY [COLUMN_TO_FILTER]

  • Complete the parameter definition as usual and save it
  • When you test the report and JasperReports Server, you'll see the option "All values" (because we added it manually in the input control query) and it'll correctly let you see the complete list of available values for the report