GaZ
GaZ

Reputation: 2406

Is it possible to refer to a JasperReport field from the master report in a table component which uses a subDataSet?

I'm working with a report which has various fields along with a subDataset:

<subDataset name="mySubDataset">
    <field name="name" class="java.lang.String"/>
    <field name="net" class="java.lang.String"/>
    <field name="tax" class="java.lang.String"/>
    <field name="total" class="java.lang.String"/>
</subDataset>
<parameter name="myDataSource" class="net.sf.jasperreports.engine.JRDataSource" isForPrompting="false"/>
<field name="totalAmount" class="java.lang.String"/>

I iterate over the "mySubDataset" using a table component like so:

<jr:table>
   <datasetRun subDataset="mySubDataset">
      <dataSourceExpression><![CDATA[$P{myDataSource}]]></dataSourceExpression>
   </datasetRun>
...

What I'd like to know is: is it possible to refer to the "totalAmount" field within the table? i.e. I want to add a "total" row to the end of the table which uses the "totalAmount" field. e.g. perhaps by using the columnFooter eleemnt like so:

<jr:column width="110">
    <jr:tableHeader height="25">
        <staticText>
            <reportElement x="0" y="0" width="110" height="25" />
        </staticText>
    </jr:tableHeader>
    <jr:columnHeader height="25">
        <textField>
            <reportElement x="0" y="0" width="110" height="25"/>
            <textFieldExpression><![CDATA[$R{netCol}]]></textFieldExpression>
        </textField>
    </jr:columnHeader>
    <jr:columnFooter height="25">
        <textField>
            <reportElement x="0" y="0" width="110" height="25" />
            <textFieldExpression><![CDATA[$F{totalAmount}]]></textFieldExpression>
        </textField>
    </jr:columnFooter>
    <jr:detailCell height="25">
        <textField>
            <reportElement x="0" y="0" width="110" height="25" />
            <textFieldExpression><![CDATA[$F{net}]]></textFieldExpression>
        </textField>
    </jr:detailCell>
</jr:column>

I get the feeling that I'm barking up the wrong tree and the 'correct' solution would be to store the totalAmount in the subDataset in the first place, however I'm not sure if this is an available option for me.

Update: Question has been cross-posted in jasper forums

Upvotes: 0

Views: 2247

Answers (1)

GaZ
GaZ

Reputation: 2406

The solution was to add the field from my master datasource as a parameter of the subDataset like so:

<subDataset name="mySubDataset">
  <parameter name="totalAmount" class="java.lang.String"/>
  <field name="name" class="java.lang.String"/>
  <field name="net" class="java.lang.String"/>
  <field name="tax" class="java.lang.String"/>
  <field name="total" class="java.lang.String"/>
</subDataset>

And then populate the parameter in the table's datasetRun like so:

<datasetRun subDataset="myDataSet">
    <datasetParameter name="totalAmount">
        <datasetParameterExpression><![CDATA[$F{totalAmount}]]></datasetParameterExpression>
    </datasetParameter>
    <dataSourceExpression><![CDATA[$P{myDataSource}]]></dataSourceExpression>
</datasetRun>

Then I could refer to the totalAmount in the tableFooter like so:

<jr:tableFooter height="25">
    <textField>
        <reportElement x="0" y="0" width="200" height="25" style="SubSummary"/>
        <textFieldExpression><![CDATA[$P{totalAmount}]]></textFieldExpression>
    </textField>
</jr:tableFooter>

Upvotes: 3

Related Questions