I'd like to show/hide a column at runtime based on a particular condition. I'm using "Print when expression" to conditionally show/hide this column (and it's header) in my report. When the column is hidden, the space it would have occupied is left blank, which is not particularly attractive.
I would prefer if the extra space was used in a more effective manner, possibilities include:
- the width of the report is reduced by the width of the hidden column
- the extra space is distributed among the remaining columns
In theory, I could achieve the first by setting the width of the column (and header) to 0, but also indicate that the column should resize to fit its contents. But JasperReports does not provide a "resize width to fit contents" option.
Another possibility is to generate reports using the Jasper API instead of defining the report template in XML. But that seems like a lot of effort for such a simple requirement.
Best Answer
In later version (v5 or above) of jasper reports you can use the
jr:table
component and truly achieve this (without the use of java code as using dynamic-jasper or dynamic-reports).The method is using a
<printWhenExpression/>
under the<jr:column/>
Example
Sample Data
Sample jrxml
Note: the parameter
$P{displayRecordNumber}
and the<printWhenExpression>
under firstjr:column
Output with $P{displayRecordNumber}=true
Output with $P{displayRecordNumber}=false
As you can see the columns adapts nicely on the basis of which are displayed.