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

Baduel picture Baduel · May 23, 2012 · Viewed 11.5k times · Source

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:

  1. A = first, B = second, C = third

  2. 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: tab

Answer

user845279 picture user845279 · May 25, 2012

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:

  1. 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.
  2. Choose your sub dataset and select "Use datasource expression" from Connection / Datasource expression menu.
  3. Insert the following expression:

((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/root[c!='']")

Good Luck.