0
votes

I have to create a report using Jasper Report library and iReport. In the report is used data grouping. It should be shown in the next way:

g    a_line_in_the_group
r    a_line_in_the_group
o    a_line_in_the_group
u    a_line_in_the_group
p    a_line_in_the_group
1    a_line_in_the_group

g    a_line_in_the_group
r    a_line_in_the_group
o    a_line_in_the_group
u    a_line_in_the_group
p    a_line_in_the_group
2    a_line_in_the_group

So there is a single label for a group. The data is grouped, but the label is shown only in the first line, like this:

group1    a_line_in_the_group
          a_line_in_the_group
          a_line_in_the_group

I've specified the property stretch for the label, but it doesn't help.

2
Where did you place the label? - Alex K
@Alex K - on the detail band. - StKiller

2 Answers

2
votes

You can put the detail rows next to the group label... but you'll only be able to do it using a subreport.

In most cases (and I mean a really overwhelming majority of cases) it is easiest and best to display the group label either into the group header or just displayed in the detail band. In the latter case it can be included on just the first row in the group or in every row. Alex K's example shows these perfectly.

But when you're in a case where you really want the group label to appear next to the details, then you'll need a subreport. Here's an example of how it would look in iReport: report layout

Here's how that report renders when previewed:

enter image description here

In my example I set the group label rotation. This is great in PDF, but not so great in HTML. But that's just a detail; you can render that label however you like.

1
votes

  • The first variant
        <queryString>
            <![CDATA[SELECT
         DOCUMENTID AS POSITIONS_DOCUMENTID,
         POSITIONNO AS POSITIONS_POSITIONNO,
         PRODUCTID AS POSITIONS_PRODUCTID,
         QUANTITY AS POSITIONS_QUANTITY,
         PRICE AS POSITIONS_PRICE
    FROM
         POSITIONS order by PRODUCTID]]>
        </queryString>
        <field name="POSITIONS_DOCUMENTID" class="java.lang.Integer"/>
        <field name="POSITIONS_POSITIONNO" class="java.lang.Integer"/>
        <field name="POSITIONS_PRODUCTID" class="java.lang.Integer"/>
        <field name="POSITIONS_QUANTITY" class="java.lang.Integer"/>
        <field name="POSITIONS_PRICE" class="java.math.BigDecimal"/>
        <group name="productid">
            <groupExpression><![CDATA[$F{POSITIONS_PRODUCTID}]]></groupExpression>
            <groupHeader>
                <band height="21">
                    <textField>
                        <reportElement x="0" y="0" width="100" height="21"/>
                        <textElement/>
                        <textFieldExpression><![CDATA["Grouped by: " + $F{POSITIONS_PRODUCTID}]]></textFieldExpression>
                    </textField>
                </band>
            </groupHeader>
        </group>
        <detail>
            <band height="21" splitType="Stretch">
                <textField>
                    <reportElement x="136" y="0" width="100" height="21"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{POSITIONS_POSITIONNO}]]></textFieldExpression>
                </textField>
                <staticText>
                    <reportElement x="36" y="0" width="100" height="21"/>
                    <textElement/>
                    <text><![CDATA[Static text]]></text>
                </staticText>
            </band>
        </detail>
    

    The result will be:

    enter image description here

  • The second variant
        <queryString>
            <![CDATA[SELECT
         DOCUMENTID AS POSITIONS_DOCUMENTID,
         POSITIONNO AS POSITIONS_POSITIONNO,
         PRODUCTID AS POSITIONS_PRODUCTID,
         QUANTITY AS POSITIONS_QUANTITY,
         PRICE AS POSITIONS_PRICE
    FROM
         POSITIONS order by PRODUCTID]]>
        </queryString>
        <field name="POSITIONS_DOCUMENTID" class="java.lang.Integer"/>
        <field name="POSITIONS_POSITIONNO" class="java.lang.Integer"/>
        <field name="POSITIONS_PRODUCTID" class="java.lang.Integer"/>
        <field name="POSITIONS_QUANTITY" class="java.lang.Integer"/>
        <field name="POSITIONS_PRICE" class="java.math.BigDecimal"/>
        <group name="productid">
            <groupExpression><![CDATA[$F{POSITIONS_PRODUCTID}]]></groupExpression>
            <groupHeader>
                <band height="21"/>
            </groupHeader>
        </group>
        <detail>
            <band height="21" splitType="Stretch">
                <textField>
                    <reportElement x="136" y="0" width="100" height="21"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{POSITIONS_POSITIONNO}]]></textFieldExpression>
                </textField>
                <textField isBlankWhenNull="true">
                    <reportElement x="36" y="1" width="100" height="20">
                        <printWhenExpression><![CDATA[$V{productid_COUNT}==1]]></printWhenExpression>
                    </reportElement>
                    <textElement/>
                    <textFieldExpression><![CDATA["Grouped by: " + $F{POSITIONS_PRODUCTID}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
    

    The result will be: enter image description here