Ronaldo Lanhellas
Ronaldo Lanhellas

Reputation: 3336

How to group inside group?

I have the following situation:

Each row in my database have a GROUP LETTER (can be any letter choose by user), like this:

   ROW1 - A 
   ROW2 - A
   ROW3 - F
   ROW4 - F
   ROW5 - K

So, in the exemple above the report should show the rows in DETAIL BAND and GROUPS LETTERS in COLUMN FOOTER with COUNT, like this:

  IN DETAIL: 
     ROW1
     ROW2
     ROW3
     ROW4
     ROW5

   IN FOOTER BAND:
     A - 2
     F - 2
     K - 1

How can i do it ?

Upvotes: 0

Views: 2997

Answers (1)

Petter Friberg
Petter Friberg

Reputation: 21710

In the footer band you can include a subreport this is how the grouping could be done in the subreport.

jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="subreport" pageWidth="555" pageHeight="842" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="20" bottomMargin="20" uuid="c1d9b4b7-6162-4b17-b871-3cf3b867d1ef">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="row" class="java.lang.String"/>
    <field name="letter" class="java.lang.String"/>
    <variable name="letter_cnt" class="java.lang.Integer" resetType="Group" resetGroup="testGroup" calculation="Count">
        <variableExpression><![CDATA[$F{letter}]]></variableExpression>
        <initialValueExpression><![CDATA[0]]></initialValueExpression>
    </variable>
    <group name="testGroup">
        <groupExpression><![CDATA[$F{letter}]]></groupExpression>
        <groupHeader>
            <band height="20">
                <textField>
                    <reportElement x="0" y="0" width="100" height="20" uuid="f626bfda-d44c-491e-915b-d4b078e6f5cf"/>
                    <textFieldExpression><![CDATA[$F{letter}]]></textFieldExpression>
                </textField>
                <textField evaluationTime="Group" evaluationGroup="testGroup">
                    <reportElement x="100" y="0" width="100" height="20" uuid="6181956b-527f-4dca-8144-1846b4b8ef99"/>
                    <textFieldExpression><![CDATA[$V{letter_cnt}]]></textFieldExpression>
                </textField>
            </band>
        </groupHeader>
    </group>
</jasperReport>

As output gives

result

However, you can not use the main report datasource directly since this has already been iterated in the main report detail band, hence it is at last record.

If you are using a connection to provide data, I would suggest to query directly to have desired result (sum and group by).

If you are using a JRDataSource as the JRBeanCollectionDataSource you can re-initialized the datasource by using:

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{REPORT_DATA_SOURCE}.getData()) 

hence this is the datasource to pass to subreport

Upvotes: 2

Related Questions