1

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.

Alex K
  • 22,315
  • 19
  • 108
  • 236
spierepf
  • 2,774
  • 2
  • 30
  • 52
  • Possible duplicate of [How to show/hide a column at runtime?](http://stackoverflow.com/questions/116053/how-to-show-hide-a-column-at-runtime) – Petter Friberg Jul 15 '16 at 16:29

2 Answers2

0

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);
Jukka
  • 406
  • 4
  • 6
  • I am trying to use the column's printWhenExpression to hide columns. However, the field that I am using doesn't appear to be available to me, and I'm not sure why. And I really have no idea what you mean by modifying the jasper design at runtime. – spierepf Jun 10 '13 at 11:07
0

For future readers

You should use this
<printWhenExpression><![CDATA[$F{Total1_header} != null]]></printWhenExpression>

Instead of
<printWhenExpression>$F{Total1_header} != null</printWhenExpression>

Hobas Matius
  • 199
  • 1
  • 2
  • 14