I am generating a Jasper Report, which has 5 fixed columns. The client has requested the ability to select the number of columns at run time.
For example if he checks 2 columns from a displayed JTable then there should be 2 columns in Jasper report, if he checks 4 then there should be four columns in jasper report. To try to solve this I am thinking of adding condition in jasper report. If the client selects column A and B I will set that as a parameter in the report and use that to determine whether to display the column.
In order to do these, I need to use conditions in the Jasper XML. Where should I set the conditions in the report.
You can try the following
Create a HashMap
and set the required properties in it
Map params = new HashMap(); params.put("DISPLAY_COLUMN_ONE", "Y")
Pass this params
map to Jasper in the method
JasperFillManager.fillReport
In your JRXML, create a parameter corresponding to each property set in the hashmap above:
<parameter name="DISPLAY_COLUMN_ONE" class="java.lang.String"/>
Display the columns based on the parameter value
<printWhenExpression><![CDATA[$P{DISPLAY_COLUMN_ONE}.equals("Y")]]></printWhenExpression>
you should pass columnCount parameter to jasper and check condition in each column's Print when Expression.
first column Print when Expression is
$P{columnCount} >=1
Second column Print when Expression is
$P{columnCount} >=2
Third column Print when Expression is
$P{columnCount} >=3
Fourth column Print when Expression is
$P{columnCount} >=4
Hope this help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With