Table shift to left when one column hide in jasper table? - jasper-reports

I am using jasper report and trying to make RTL report. My problem is that when I am hiding my column in table using printWhenExpression based on some condition my table shift to left. I am using below code to hide column in table.
<jr:column width="160" uuid="f9b26e92-337e-4fb9-a091-4a81469c6c49">
<printWhenExpression><![CDATA[System.getProperty("vehicle.event.uid.enabled").equals("true")]]></printWhenExpression>
<jr:columnHeader style="ColumnHeader" height="30" rowSpan="1">
<textField>
<reportElement style="ColumnHeader" x="0" y="0" width="160" height="30" uuid="b4043ada-2580-4602-9262-3791ce3c0807"/>
<textElement>
<font size="8" isBold="true" isItalic="false"/>
</textElement>
<textFieldExpression><![CDATA[$R{target.external.id}]]></textFieldExpression>
</textField>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1">
<textField isBlankWhenNull="true">
<reportElement style="TableTextContent" x="0" y="0" width="160" height="20" isPrintWhenDetailOverflows="true" uuid="9dc36daa-d5e5-417c-831d-ff1b92ef1b79"/>
<textElement>
<font size="8" isBold="false" isItalic="false"/>
</textElement>
<textFieldExpression><![CDATA[$F{personExternalId}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
is there is any way so that my table should not shift to left when one of its column which is on left in that table is dynamically hide?

If you want to keep the column and does not want text, remove print when condition on column and add it to text expression :
<textFieldExpression><![CDATA[System.getProperty("vehicle.event.uid.enabled").equals("true")?$R{target.external.id} : ""]]></textFieldExpression>

Related

How to combine two textFields into one box in RTF generated by Jasper reports?

I use Jasper Reports to generate RTF file.
In RTF each textField is represented as separate box:
I tried to use one frame for textFields:
<frame>
<reportElement positionType="Float" stretchType="RelativeToTallestObject" x="220" y="45" width="406" height="75"/>
<textField>
<reportElement positionType="Float" x="0" y="0" width="406" height="60"/>
<textElement>
<font fontName="Arial" size="8"/>
</textElement>
<textFieldExpression><![CDATA["Address:"]]></textFieldExpression>
</textField>
<textField hyperlinkType="Reference" hyperlinkTarget="Blank">
<reportElement x="0" y="57" width="200" height="15" key="textWithStyle" style="LinkField"/>
<textElement>
<font fontName="Arial" size="8" isUnderline="true"/>
</textElement>
<textFieldExpression><![CDATA["here"]]></textFieldExpression>
<hyperlinkReferenceExpression><![CDATA["http:adress"]]></hyperlinkReferenceExpression>
</textField>
</frame>
But textFields also in the different boxes.
Is it possible to combine several textFields into one box like the following?
I know that I can add each text to one textField, but sometimes I need two separate textFields that I can manage inside one box.

jasper reports grouping not working as expected

I am running a jasper report on an select query, I am grouping the result by few values and using the same fields in my header
Below is my jasper group expression and header.
<groupExpression><![CDATA[$F{tray_code} + $F{card_id} + $F{card_position}+$F{card_number}+ $F{card_from_date}+ $F{card_to_date} + $F{week_number}+$F{patient_id} + $F{patient_full_name}]]></groupExpression>
<groupHeader>
<band height="15">
<textField isBlankWhenNull="true">
<reportElement x="5" y="0" width="20" height="14" uuid="97fd5e2c-50e4-46e4-9c1b-0453f5cd05b5"/>
<textElement>
<font fontName="DejaVu Sans" size="8" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{tray_code}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="26" y="0" width="44" height="14" uuid="d16ea2fb-367a-4b5e-986e-bc19669c3091"/>
<textElement>
<font fontName="DejaVu Sans" size="8" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{card_id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="95" y="0" width="25" height="14" uuid="b7eb1f75-44d4-467e-8463-29f6f6f4555b"/>
<textElement textAlignment="Center">
<font fontName="DejaVu Sans" size="8" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{card_position}]]></textFieldExpression>
</textField>
<textField pattern="">
<reportElement x="120" y="0" width="55" height="14" uuid="10e4bc1b-cfbb-478d-bd10-a6284bd3bac1"/>
<textElement>
<font fontName="DejaVu Sans" size="8" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[new SimpleDateFormat("dd/MM/yyyy").format(new SimpleDateFormat("yyyy-MM-dd").parse($F{card_from_date}))]]></textFieldExpression>
</textField>
<textField pattern="dd/MM/yyyy">
<reportElement x="175" y="0" width="55" height="14" uuid="a7d34de5-c4de-4093-9da7-96119450c3ab"/>
<textElement>
<font fontName="DejaVu Sans" size="8" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[new SimpleDateFormat("dd/MM/yyyy").format(new SimpleDateFormat("yyyy-MM-dd").parse($F{card_to_date}))]]></textFieldExpression>
</textField>
<textField>
<reportElement x="230" y="0" width="24" height="14" uuid="9cf3bce2-cb70-45e7-8199-0d6c8003ce53"/>
<textElement>
<font fontName="DejaVu Sans" size="8" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{week_number}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="270" y="0" width="67" height="14" uuid="d9e50bb7-1616-4d84-b92f-246abd05ae7b"/>
<textElement>
<font fontName="DejaVu Sans" size="8" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{patient_id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="337" y="0" width="250" height="14" uuid="b06f0d0d-739b-4e2a-a2bc-1686c9d5cbfe"/>
<textElement>
<font fontName="DejaVu Sans" size="8" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{patient_full_name}]]></textFieldExpression>
</textField>
<line>
<reportElement x="0" y="14" width="752" height="1" uuid="968c2e25-462d-4b0a-90e2-34570db25d0f"/>
<graphicElement>
<pen lineWidth="1.0" lineStyle="Dashed" lineColor="#999999"/>
</graphicElement>
</line>
<textField>
<reportElement x="70" y="0" width="25" height="14" uuid="7f4ba299-706f-4ba6-aafc-ffa47b0e4374"/>
<textElement textAlignment="Center">
<font fontName="DejaVu Sans" size="8" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{card_number}]]></textFieldExpression>
</textField>
</band>
</groupHeader>
According to the above group expression, I expect only one header for a group, But I see the group header for the same group is repeated in my output file ! Please let me know what I am doing wrong.
Thanks in Advance.
So there is an issue that happens when you use multiple group headers. The first header behaves like expected ordered by Column Value A only on unique values.
The 2nd header using Column Value B will print on every row despite being non-unique values.
In theory you should be able to use:
1.
ORDER BY ValueA, ValueB
to properly display the report assuming you are using sql, plsql, etc...
However, in my case that did not happen, though for others it seems to work.
2.
Use subreports to attach the minor differences via unique reports.
You create a root report with empty details.
Then you create Unique reports to serve as sub reprots.
Finally, you use the subreport element to link the values into the root report. Though that is a good bit of work and may cause repeat code.
A hacky way I used was:
A mixture of "Print When Expressions" with logical boolean like below
$F{QUERY}.equals(Query1) && ($P{P_Typequery}.equalsIgnoreCase("QueryA") && $P{P_parameter} == null)
and
$F{QUERY}.equals(Query1) && ($P{P_Typequery}.equalsIgnoreCase("P_QueryA") && $P{P_parameter} != null)
with two group headers and a column header. The column header will not repeat for every row so you assign one of the booleans expressions to it's "print when" so it does not always print. The first group header will not repeat for every row and works. The 2nd group header is used for the times you DO want it to repeat for every unique value since it always prints for every row, and you use the other boolean on it's "print when".
Hope it helps

using conditions to print values in ireport

I am trying to use conditions to print value in table
I have three columns A B C
A is string
B is integer
C is integer
my condition is If column A == "user"
then print value in column B or make it null
($F{A}.equals("USER")?null:$F{B})
this gives type casting error
<jr:column width="15" uuid="733459e9-1ed8-4ce2-a74d-87dc078b1382">
<jr:tableFooter height="29" rowSpan="1"/>
<jr:columnFooter style="table 8_CH" height="30" rowSpan="1"/>
<jr:detailCell style="table 8_TD" height="20" rowSpan="1">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="15" height="20" uuid="bd3860a4-999a-4121-a701-3481a4fdf98e"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="8"/>
</textElement>
<textFieldExpression><![CDATA[$F{TOP}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="15" uuid="2e7fa82c-7f28-4af6-a63d-ae468afeea32">
<jr:tableFooter height="29" rowSpan="1"/>
<jr:columnFooter style="table 8_CH" height="30" rowSpan="1"/>
<jr:detailCell style="table 8_TD" height="20" rowSpan="1">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="15" height="20" uuid="33707346-9121-4010-8c12-981eb9f4de77">
<printWhenExpression><![CDATA[$F{TOP}.endsWith("NN")?null:$F{COP}]]></printWhenExpression>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="8"/>
</textElement>
<textFieldExpression><![CDATA[$F{COP}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
$F{TOP}.endsWith("NN")?null:$F{COP} this should not be in the "Print When Expression" field, for this is a boolean expression field: like print when expression if this = that. Like for instance if I never want to see that field I would put 0==1 in the print when expression field. So remove that line from the print when expression field.
You want to put the line in the field itself. So right-click the field TOP (I am assuming this is the field you want to change if it ends with NN) and select Edit Expression and put this line in replacement of $F{TOP}

How do I hide a table column based on its content?

I have a report which contains a table. I would like to hide the columns of that table based on the result set of the query that backs the table. As an example, here is the XML for one of the columns:
<jr:column width="80">
<printWhenExpression>$F{Total1_header} != null</printWhenExpression>
<jr:columnHeader height="30" rowSpan="1">
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement x="0" y="0" width="80" height="30" style="table_CH"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression>$F{Total1_header}</textFieldExpression>
</textField>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1">
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement x="0" y="0" width="80" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression>$F{Total1}</textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
For some reason, my report is complaining about the printWhenExpression. It claims that $F{Total1_header} does not exist. It does not complain however about the instance of $F{Total1_header} in the textFieldExpression.
I can't figure out why the field is avaiable for the textFieldExpression, but not the printWhenExpression.
Column has 'Column print when' property to hide/show the column by an expression.
Another way to delete columns is to modify the jasper design at runtime, then report compilation and then procees as usually. This method makes it possible to distribute unused width to remaining columns.
Roughly goes like this:
JasperDesign design = ...
JRDesignComponentElement tableElement = (JRDesignComponentElement) design.getTitle().getElementByKey(tableKey);
StandardTable tableComponent = (StandardTable) tableElement.getComponent();
col = tableComponent.getColumns().get(0);
tableComponent.removeColumn(col);
// Then compile the JasperDesign
JasperReport result=JasperCompileManager.compileReport(design);
For future readers
You should use this
<printWhenExpression><![CDATA[$F{Total1_header} != null]]></printWhenExpression>
Instead of
<printWhenExpression>$F{Total1_header} != null</printWhenExpression>

How to set x, y for a textElement in JasperReports depending on if above textElement is empty. And if non-empty show it down to the that textelement

Requirement:
If firstName and lastName is non empty. Show this at x=125, y=3 coordinates.
And show address down to it at x="125" y="26" coordinate. This is working fine.
Problem: If firstName/lastName are empty, Then we need to move the address element up and show it at x=125, y=3 coordinate itself.
This is where I am stuck. Please help me.
Please refer my jrxml file below:
<frame>
<reportElement key="frame-462" positionType="Float" mode="Transparent" x="0" y="50" width="390" height="70"/>
<textField isStretchWithOverflow="true" isBlankWhenNull="true" evaluationTime="Now">
<reportElement key="textField-844" positionType="Float" mode="Transparent" x="125" y="3" width="264" height="23"
isPrintWhenDetailOverflows="true" isRemoveLineWhenBlank="true">
<printWhenExpression><![CDATA[(!$F{firstName}.isEmpty() && !$F{lastName}.isEmpty())]]></printWhenExpression>
</reportElement>
<textElement markup="styled">
<font fontName="Verdana" size="12"/>
</textElement>
<textFieldExpression>
<![CDATA[$F{firstName} + "<style pdfFontName='Helvetica-Oblique' size='11'>" + $F{lastName} + "</style>"]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" isBlankWhenNull="true" evaluationTime="Now">
<reportElement key="textField-843" mode="Transparent" positionType="Float" x="125" y="26" width="264"
height="42" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true"/>
<textElement markup="html">
<font fontName="Verdana" size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
</textField>
<staticText>
<reportElement key="staticText-1249" positionType="Float" x="2" y="2" width="108" height="21"
isRemoveLineWhenBlank="true"/>
<textElement textAlignment="Left">
<font fontName="Verdana" size="12" isBold="false" pdfFontName="Helvetica-Bold"/>
</textElement>
<text><![CDATA[ Address:]]></text>
</staticText>
</frame>
You have already set the isRemoveLineWhenBlank attribute, which will remove the empty space left by the textField. However, JasperReports cannot collapse vertical space unless that space is completely clear. If there is another element occupying the same line (in your case the staticText element), the empty space will be preserved. This is a bit like playing Tetris in reverse; You have to get rid of all of the blocks before the line will disappear.
Putting report elements into frames modifies this behaviour slightly. It is only the width of the parent frame that needs to be clear, instead of the width of the whole report. Therefore, by putting your name and address fields into a separate frame, the space occupied by the name field can collapse despite being aligned horizontally with the static text. I have modified your code snippet to demonstrate this:
<frame>
<reportElement key="frame-462" positionType="Float" mode="Transparent" x="0" y="50" width="390" height="70"/>
<frame>
<reportElement x="126" y="0" width="264" height="70"/>
<textField isStretchWithOverflow="true" isBlankWhenNull="true" evaluationTime="Now">
<reportElement key="textField-844" positionType="Float" mode="Transparent" x="0" y="3" width="264" height="23" isPrintWhenDetailOverflows="true" isRemoveLineWhenBlank="true">
<printWhenExpression><![CDATA[(!$F{firstName}.isEmpty() && !$F{lastName}.isEmpty())]]></printWhenExpression>
</reportElement>
<textElement markup="styled">
<font fontName="Verdana" size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{firstName} + "<style pdfFontName='Helvetica-Oblique' size='11'>" + $F{lastName} + "</style>"]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" isBlankWhenNull="true" evaluationTime="Now">
<reportElement key="textField-843" mode="Transparent" positionType="Float" x="0" y="26" width="264" height="42" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true"/>
<textElement markup="html">
<font fontName="Verdana" size="12"/>
</textElement>
<textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
</textField>
</frame>
<staticText>
<reportElement key="staticText-1249" positionType="Float" x="2" y="2" width="108" height="21" isRemoveLineWhenBlank="true"/>
<textElement textAlignment="Left">
<font fontName="Verdana" size="12" isBold="false" pdfFontName="Helvetica-Bold"/>
</textElement>
<text><![CDATA[ Address:]]></text>
</staticText>
</frame>