Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add group band on combination of multiple fields?

I have this data and I want in iReport is group this by number group and color group.

color | number_group | color_group |
red   |    1         |  primary    |
blue  |    1         |  primary    |
yellow|    2         |  primary    |
orange|    2         |  secondary  |
violet|    1         |  secondary  |
green |    1         |  secondary  |

I want in my report

Color Group: Primary   Number: 1
red
blue

Color Group: Primary   Number: 2
yellow

Color Group: Secondary Number: 1
violet
green

Color Group: Secondary Number: 2
orange

But in I can only manage to do Add Group > Group Criteria > Group by following expression > choose color_group field.

And the output is this which is not the one that I'm hoping for.

Color Group: Primary Number: 1
red
blue
yellow

Color Group: Secondary Number: 2
orange
violet
green

I think the right solution is using Add Group > Group Criteria > Group by following expression > group expression with text area but I don't know what to put there. Anyone?

like image 475
Jomar Gregorio Avatar asked Mar 22 '16 03:03

Jomar Gregorio


People also ask

How do I add a group band in Jasper report?

Grouping in ReportRight click on the report and click on add group, create group by giving the expression or selecting the fields.

How do I create a group header in Jasper?

A group can even have more than one header\footer band in the designer, for example to add an header to a group it's sufficent right click on the header element in the outline view (it dosen't matter if it has black color or lightgray because at the moment there aren't other headers) and select the option "Create Band" ...


1 Answers

Group on the concatenation of the two values:

<groupExpression><![CDATA[$F{number_group} + $F{color_group}]]></groupExpression>

Example

<?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="group" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c1d9b4b7-6162-4b17-b871-3cf3b867d1ef">     
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="color" class="java.lang.String"/>
    <field name="number_group" class="java.lang.String"/>
    <field name="color_group" class="java.lang.String"/>
    <group name="myGroup">
        <groupExpression><![CDATA[$F{number_group} + $F{color_group}]]></groupExpression>
        <groupHeader>
            <band height="20">
                <textField>
                    <reportElement mode="Transparent" x="0" y="0" width="300" height="20" forecolor="#3333FF" uuid="b3f3381f-26c1-48d5-953e-ddd017fbf7cf"/>
                    <textElement verticalAlignment="Middle"/>
                    <textFieldExpression><![CDATA["Color Group: " + $F{color_group} + " Number: " + $F{number_group}]]></textFieldExpression>
                </textField>
            </band>
        </groupHeader>
    </group>
    <detail>
        <band height="15" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="300" height="15" uuid="7337168a-363f-4438-a38e-e4859fb6fdd1"/>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{color}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

Output

Result

Note: The get exact same order as your expected output the orange record need to be last (order data)

like image 75
Petter Friberg Avatar answered Sep 18 '22 13:09

Petter Friberg