1

In JasperReport, I have a list with all the orders of a customer, for every customer. I would like to calculate the total of all the items' prices in every list.

This is my list:

<jr:list>
    <datasetRun subDataset="listDataset">
    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{products})]]>  </dataSourceExpression>
    </datasetRun>
    <jr:listContents height="20">
       <textField>
        <reportElement style="table_TD" x="100" y="0" width="100" height="20"/>
        <textElement/>
        <textFieldExpression class="java.lang.String"><![CDATA[$F{color}]]></textFieldExpression>
    </textField>
    <textField>
        <reportElement style="table_TD" x="0" y="0" width="100" height="20"/>
        <textElement/>
        <textFieldExpression class="java.lang.Integer"><![CDATA[$F{articleNo}]]></textFieldExpression>
    </textField>
    <textField>
        <reportElement style="table_TD" x="200" y="0" width="100" height="20"/>
        <textElement/>
        <textFieldExpression class="java.lang.Double"><![CDATA[$F{price}]]></textFieldExpression>
    </textField>
</jr:listContents>

The sub-dataset I use to fill it (I use a JRBeanCollectionDataSource for the whole report):

<subDataset name="listDataset">
        <field name="color" class="java.lang.String"/>
        <field name="articleNo" class="java.lang.Integer"/>
        <field name="price" class="java.lang.Double"/>

    </subDataset>

In the master report I defined a variable with the subdataset's field "price" as expression:

<variable name="totalPrice" class="java.lang.Double"calculation="Sum">
        <variableExpression><![CDATA[$F{price}]]></variableExpression>
    </variable>

But this is always null. If I define the same variable in the list's dataset, the total is calculated correctly, but I cannot use it in the master report.

So my question is: Is there any way I can achieve this with a list? Or do I have to use a subreport (because it can return a value)?

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.