Search code examples
jasper-reports

How to handle multiple group details in Jasper?


I have this set of records:

colors table.

color_group | color_name | color_no
Primary     | Red        | 1
Primary     | Blue       | 3
Primary     | Yellow     | 2
Secondary   | Green      | 8
Secondary   | Violet     | 1
Secondary   | Orange     | 7
Others      | Pink       | 6
Others      | White      | 4
Others      | Black      | 5

I want to generate report like this.

Primary
  Red        1
  Blue       3  
  Yellow     2
Secondary
  Green      8
  Violet     1
  Orange     7
Others
  Pink       6
  White      5
  Black      4

how can i display it like this in jasper report?


Solution

  • This is solved by using group on the color_group field and adding a groupHeader band to display its value.

    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">
        <field name="color_group" class="java.lang.String"/>
        <field name="color_name" class="java.lang.String"/>
        <field name="color_no" class="java.lang.Integer"/>
        <group name="colorGroup">
            <groupExpression><![CDATA[$F{color_group}]]></groupExpression>
            <groupHeader>
                <band height="20">
                    <textField>
                        <reportElement x="0" y="0" width="100" height="20" uuid="e98d6fc1-1ecd-4af4-8250-d8aaa497011e"/>
                        <textFieldExpression><![CDATA[$F{color_group}]]></textFieldExpression>
                    </textField>
                </band>
            </groupHeader>
        </group>
        <detail>
            <band height="20" splitType="Stretch">
                <textField>
                    <reportElement x="20" y="0" width="100" height="20" uuid="7ca1ac35-6249-4ba6-ac87-031f8d410d2e"/>
                    <textFieldExpression><![CDATA[$F{color_name}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="120" y="0" width="100" height="20" uuid="395b0cdd-4d2c-4d0b-93cd-7cad6daf3a4b"/>
                    <textFieldExpression><![CDATA[$F{color_no}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
    </jasperReport>
    

    Result

    Result