Tuesday, October 30, 2012

Jasper Report: Gracefully Hide Columns On Request

For some reports you might want to offer the end users some more flexibility in terms of which columns they actually want to see in the report.
Documentation about this feature is scarce. When I was looking at a solution for this request some time ago I coincidentally stumble across Nestor's blog post (Some years ago, when I was living in Miami, Nestor and I used to work for the same company), which I found very useful.

Our requirement is that when a column is hidden it must not consume any space, meaning all columns to the right of it move gracefully to the left. This can be achieved using the table component. 
Our initial report looks something like this:


Define a parameter (i.e. SHOW_CONTINENT) and set its type to Boolean. 
Set the default value to:


new Boolean(true) 

and tick Use as a prompt.

Note: This parameter does not have to be passed to the subdata set!

Screenshot: iReport - Parameter Properties
Screenshot: JasperStudio - Parameter Properties
In the table mark the header column element for the column which you want to hide on request. It is best to select this from the Document Inspector - make sure it is the column and not the element within the cell: 
Note: It does not really matter on which level (table header, column header, detail etc) you select the column.

In the Properties set the Column Print When condition to:

new Boolean($P{PARAMETERNAME})

Screenshot: iReport - Column Properties
Screenshot: JasperStudio - Column Properties
Let's now preview the report. When asked if we want the continent column displayed, we say yes:
Now let's preview the report with the continent column disabled:
As you can see from the screenshot the continent column is not shown and the country, city and sales columns all nicely moved to the left.

1 comment: