[ACCEPTED]-How to show/hide a column at runtime?-jasper-reports
In later version (v5 or above) of jasper 5 reports you can use the jr:table
component and truly achieve 4 this (without the use of java code as using 3 dynamic-jasper or dynamic-reports).
The method 2 is using a <printWhenExpression/>
under the <jr:column/>
Example
Sample Data
+----------------+--------+
| User | Rep |
+----------------+--------+
| Jon Skeet | 854503 |
| Darin Dimitrov | 652133 |
| BalusC | 639753 |
| Hans Passant | 616871 |
| Me | 6487 |
+----------------+--------+
Sample jrxml
Note: the parameter $P{displayRecordNumber}
and 1 the <printWhenExpression>
under first jr:column
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="reputation" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a88bd694-4f90-41fc-84d0-002b90b2d73e">
<style name="table">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="tableDataset" uuid="7a53770f-0350-4a73-bfc1-48a5f6386594">
<field name="User" class="java.lang.String"/>
<field name="Rep" class="java.math.BigDecimal"/>
</subDataset>
<parameter name="displayRecordNumber" class="java.lang.Boolean">
<defaultValueExpression><![CDATA[true]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<title>
<band height="50">
<componentElement>
<reportElement key="table" style="table" x="0" y="0" width="555" height="47" uuid="76ab08c6-e757-4785-a43d-b65ad4ab1dd5"/>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="tableDataset" uuid="07e5f1c2-af7f-4373-b653-c127c47c9fa4">
<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
</datasetRun>
<jr:column width="90" uuid="918270fe-25c8-4a9b-a872-91299cddbc31">
<printWhenExpression><![CDATA[$P{displayRecordNumber}]]></printWhenExpression>
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="90" height="30" uuid="5cd6da41-01d5-4f74-99c2-06784f891d1e"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Record number]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="30" uuid="5fe48359-0e7e-44b2-93ac-f55404189832"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="7979d8a2-4e3c-42a7-9ff9-86f8e0b164bc">
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="90" height="30" uuid="61d5f1b6-7677-4511-a10c-1fb8a56a4b2a"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Username]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="30" uuid="a3cdb99d-3bf6-4c66-b50c-259b9aabfaef"/>
<box leftPadding="3" rightPadding="3"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{User}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="625e4e5e-5057-4eab-b4a9-c5b22844d25c">
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="90" height="30" uuid="e1c07cb8-a44c-4a8d-8566-5c86d6671282"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Reputation]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="30" rowSpan="1">
<textField pattern="#,##0">
<reportElement x="0" y="0" width="90" height="30" uuid="6be2d79f-be82-4c7b-afd9-0039fb8b3189"/>
<box leftPadding="3" rightPadding="3"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{Rep}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</title>
</jasperReport>
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.
JasperDesign is used to modify the template object (JasperReport) from 2 within the code at runtime. I guess this 1 might fit in your case.
Remove line when blank: This option takes 11 away the vertical space occupied by an object, if 10 it is not visible; the element visibility 9 is determined by the value of the expression 8 contained in the Print when expression attribute. Think 7 of the page as a grid where the elements 6 are placed, with a line being the space 5 the element occupies. Figure 4-17 highlights 4 the element A line; in order to really remove 3 this line, all the elements that share a 2 portion of the line have to be null (that 1 is, they will not be printed).
A slight variation on the "second report" theme 6 that I have used is to isolate the part 5 of the report where you have an optional 4 column into it's own subreport, and then 3 create two subreports, one with and one 2 without the column, and then use conditions 1 to determine which subreport to print.
I recommend to use DynamicReports, it's open source and 3 based on JasperReports. The main benefit 2 of this library is a dynamic report design 1 and no need for a visual report designer.
If it is just one column, is it possible 14 to place this column to the far right, and 13 then use the print when expression. That 12 way there is not a hole in the middle. I 11 know this is not ideal, as I had tried to 10 do what you are currently trying to accomplish 9 in the past, and could not find what I call 8 a good solution.
A second idea would to be 7 create a second report based on the first 6 with out the column, and then when calling 5 the report check the condition, to decide 4 which one to call. Again not ideal, but 3 would work.
I know this is not really the 2 answer you were looking for, but one of 1 these suggestions may work for you.
Check THIS In that tutorial they are using XML 5 template with Velocity framework. This is 4 pretty complex. And to make it simpler you 3 can us DynamicJasper. This library is an open source 2 Java API that works over JasperReports that 1 solves the dynamic columns issue.
I guess this answer comes way too late, but 10 I add it for the record. In my case I could 9 solve it without any additional dependencies 8 or tools. In the JRXML file, I just added 7 the textfields width a dynamic width multiple 6 times. Once per possible width that is. Then 5 on each textfield, I have set that it should 4 only be printed in case of a certain condition.
This 3 might not be as elegant as setting the width 2 dynamically, but it does the trick without 1 any hassle with extra libraries.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.