How to Check param from iReport with database? - jasper-reports

Currently I'm doing report using iReport 3.0.0
I have this one field which is Indicator (A, B and C).
I group by this field so that All records will print in different page based on Indicator. (I use iReport Group By function)
However, I have certain time that when it query my sql, it will return only A and C.. How if I wanna generate Ind = B too with text NO RECORD FOUND
This is my current expression:
($F{IND}.equals("A")) ? "SMS MESSAGE NOT FOUND" : (
($F{IND}.equals("B")) ? "CW MESSAGE NOT FOUND" : (
($F{IND}.equals("C")) ? "STATUS MIS-MATCHED" : null
)
)
How can I check if IND = B not exist in my database, then print NO RECORD FOUND ?
Thanks in advance for your help !

You can use a special variable to count the rows in the group B.
Either use the built-in variable ${YOUGROUP_COUNT} (source)
Or increment a variable yourself : see more information here
When you have this variable set up, you can have a textField containing NO RECORD FOUND. Use the attribute printWhenExpression to display this textField only when the group count is equal to zero.
<staticText>
<reportElement x="234" y="10" width="100" height="30" uuid="05895bf2-3ce1-4d88-82fe-ff3fd650eaf6">
<printWhenExpression><![CDATA[${YOURGROUP_COUNT} == 0]]></printWhenExpression>
</reportElement>
<text><![CDATA[NO RECORD FOUND]]></text>
</staticText>
Don't display this static text element in a detail band, because it won't be displayed if there is no record. Use any other band to display it.

Related

Jaspersoft studio adjust position of fields relative to height of other fields

I am trying to create a report in Jaspersoft Studio 6.15. and I hit a wall that I can't seem to break.
The thing is I need to align my Static and Text fields depending on Stretch/NoStretch of the previous one.
In my report I want to add few text fields along with their (static text)labels aligned horizontally like
ID : $F{ID}
Name: $F{Name}
Address: $F{Address}
But I'm unable to align them. This is what I tried
Position Type: float (for all static text and fields)
Stretch Type: no stretch (for all static text and fields)
Stretch With Overflow: True (for all dynamic text fields)
The image shows what I get and what I want. Moreover, my text field's content is dynamic i.e. content size could vary.
This is what I get:
This is what it should be:
I've read many forums but could not find a solution, please suggest.
Thanks
EDIT:
Now I am getting this:
Nest each static and text field pair into a frame element:
<frame>
<reportElement ... positionType="Float"/>
<staticText>
...
</staticText>
<textField isStretchWithOverflow="true">
...
</textField>
</frame>
<frame>
...
</frame>
...
If the elements are to be included in the detail section, you can also create a separate band for each pair:
<detail>
<band>
<staticText>
...
</staticText>
<textField isStretchWithOverflow="true">
...
</textField>
</band>
<band>
...
</band>
...
</detail>

Jasper subreport arent receiving any F{}

In my master report, I have a subreport tag:
<subreport>
<subreportParameter name="quotaSanitarySeal">
<subreportParameterExpression><![CDATA[$F{quotaSanitarySeal}]]>
</subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
<subreportExpression class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "subReportTest.jasper"]]>
</subreportExpression>
</subreport>
But when I generate it, It doesn't show up the value of the field...
<textFieldExpression class="java.lang.String"><![CDATA[$F{quotaSanitarySeal} == 0.0 ? "" : $F{quotaSanitarySeal}]]></textFieldExpression>
What I'm missing? I must do something in my subReportTest?
When you pass a field or Variable or Parameter to sub-report. Those values will be stored inside Parameter section of subreport.
So when you are passing a field, you have to create a respective Parameter field inside the sub-report, in your case create a parameter called "quotaSanitarySeal" in your sub report. Once you added the parameter, then you can pass a value to the parameter from a master report like below,
<subreport>
<subreportParameter name="quotaSanitarySeal">
<subreportParameterExpression><![CDATA[$F{quotaSanitarySeal}]]>
</subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]</dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subReportTest.jasper"]]></subreportExpression>
</subreport>
Once you have successfully passed the value then you can use the parameter like below inside the subreport,
<textFieldExpression class="java.lang.String"><![CDATA[$P{quotaSanitarySeal} == 0.0 ? "" : $P{quotaSanitarySeal}]]></textFieldExpression>
Note: For "subreportExpression", you need not set the type class="java.lang.String".
Hope this helps.

How to have a constant records from array in subreport?

How to do in order to have a constant limit records for each value in subreport ?
When you define the datasource expresionof your subreport you can apply the sublist function:
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{list}.subList(0,100))
in your xml, you will have something like:
<subreport>
<reportElement x="x" y="x" width="xx" height="xx" isPrintInFirstWholeBand="xx" uuid="xxx"/>
<dataSourceExpression><![CDATA[net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{list}.subList(0,100))]]></dataSourceExpression>
<subreportExpression>xxxxx</subreportExpression>
</subreport>

Jasper - How to remove complete row when 1 perticular field is null?

I want to print pdf something like this
Name Class RollNo
------- ---------- -----------
John 5 <null>
Mark 5 103
Robert 6 104
I need to add condition if RollNo is null then remove that row in 'detail' band.
You can use the report's filter expression or the detail band's print when expression. The filter expression completely skips the record, which is not counted and does not participate in aggregations, while the band's print when expression simply inhibits the band from printing.
<filterExpression>$F{RollNo} != null</filterExpression>
...OR...
<detail>
<band height="x">
<printWhenExpression>$F{RollNo} != null</printWhenExpression>
<textField>
...

How to erase rows in a table if an expression is valid in iReport

I have a table in iReport with 3 fields (A, B, C). I would to print the row iff field C is not null. For example if I have 2 records in my data source:
A = first, B = second, C = third
A = up, B = down, C = NULL
the table must have only the first row.
I have tried inserting this expression in each cell (in "Print when expression" property):
!$F{C}.equals(null)
but in this way the result is that the second row is empty (but visible).
Edit: after the first answer (now erased) the columns in the table are something like:
<jr:column ...>
<jr:columnHeader ...>
<staticText>
<reportElement .../>
<text><![CDATA[ID]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell ...>
<textField isBlankWhenNull="false">
<reportElement ... isRemoveLineWhenBlank="true">
<printWhenExpression><![CDATA[$F{ID}!=null]]></printWhenExpression>
</reportElement>
<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column ...>
<jr:columnHeader ...>
<staticText>
<reportElement .../>
<text><![CDATA[CITY]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell ...>
<textField isBlankWhenNull="false">
<reportElement ... isRemoveLineWhenBlank="true">
<printWhenExpression><![CDATA[$F{ID}!=null]]></printWhenExpression>
</reportElement>
<textFieldExpression><![CDATA[$F{CITY}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
The data source is a xml file. I've tried also with isBlankWhenNull="true" but with no change. Here a screen of the result:
When you put the print when expression on the field, only the field will be removed. Hence, the space will remain. Put the same expression on the detail band and try again.
Edit:
Looking at the problem further, I've noticed there is no option to omit records (Print When Expression) at the detail level of the table element. That option doesn't exist as you can see in iReport and also in the schema definition. Furthermore, the reason isBlankWhenNull="true" isn't working is because, even though the textfield is empty, the detail row still takes up the allocated height. Also, the PrintWhenExpression you tried to modify applies to the whole table and not the row. So it doesn't seem like it is possible to do the way you were hoping.
Here I will give you these steps to solve your problem:
Update the XPath query to your dataset run property (right click > edit table datasource from table designer view) so that the records where C is null are omitted.
Choose your sub dataset and select "Use datasource expression" from Connection / Datasource expression menu.
Insert the following expression:
((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/root[c!='']")
Good Luck.
I found another solution to this issue:
For the dataset used for the table, adding a Filter Expression, e.g. $F{dateRemoved}==null.
This way the empty rows will be removed.
Directly answering to the problem:
After using -> Print When Expression
Go just 1 inch above and check the option "Remove Line When Blank"
100% working