Search code examples
jasper-reportsreportexpression

Merging cells in jasper using expression


If I have table like this:

ID   |  details
___________________
1    |  A
2    |  B
3    |  C
4    |  D
5    |  E
6    |  F
6    |  G

and I want to show the table in jasper like this:

ID   |  details
___________________
1    |  A
2    |  B
3    |  C
4    |  D
5    |  E
6    |  F,G

if the detail have same id then the detail cells is merged

What kind of expression can I use to accomplish that in jasper (not in query)? I am using jasper 4.5


Solution

  • This can be achieved by grouping on $F{ID}, using a JRAbstractScriptlet to concat the $F{detail} strings and displaying the result in the groupFooter band

    Example of Scriptlet

    public class Scriptlet extends JRDefaultScriptlet {
    
      public void afterDetailEval() throws JRScriptletException
      {
        String details = (String)this.getVariableValue("detailsWithId");
        String detail = (String)this.getFieldValue("detail");
        StringBuffer sbuffer = new StringBuffer();
        if (details != null)
        {
            sbuffer.append(details);
            sbuffer.append(", ");
        }
        sbuffer.append(detail);
        this.setVariableValue("detailsWithId", sbuffer.toString());
      }
    }
    

    Example of jrxml, report, note the scriptletClass="Scriptlet" in jasperReport tag (the class Scriptlet needs to be in class path)

    <?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="Example2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" scriptletClass="Scriptlet" uuid="ca579c38-1e4f-4993-a020-efcea9d1096e">
    <queryString language="xPath">
        <![CDATA[/report/entry]]>
    </queryString>
    <field name="id" class="java.lang.String">
        <fieldDescription><![CDATA[id]]></fieldDescription>
    </field>
    <field name="detail" class="java.lang.String">
        <fieldDescription><![CDATA[detail]]></fieldDescription>
    </field>
    <variable name="detailsWithId" class="java.lang.String" resetType="Group" resetGroup="id" calculation="System">
        <variableExpression><![CDATA[]]></variableExpression>
    </variable>
    <group name="id">
        <groupExpression><![CDATA[$F{id}]]></groupExpression>
        <groupFooter>
            <band height="20">
                <textField>
                    <reportElement x="100" y="0" width="100" height="20" uuid="19ec4996-dee5-461e-bc61-2bf967632a9e"/>
                    <textFieldExpression><![CDATA[$V{detailsWithId}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="0" y="0" width="100" height="20" uuid="f0f77e03-d040-4628-94b3-e1506e713399"/>
                    <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
                </textField>
            </band>
        </groupFooter>
    </group>
    <columnHeader>
        <band height="20">
            <staticText>
                <reportElement x="0" y="0" width="100" height="20" uuid="bee3aa66-130d-4f05-8bb2-55e57c068ecc"/>
                <text><![CDATA[ID]]></text>
            </staticText>
            <staticText>
                <reportElement x="100" y="0" width="100" height="20" uuid="9b4d86d1-4904-4854-b723-6fe62df170ce"/>
                <text><![CDATA[details]]></text>
            </staticText>
        </band>
    </columnHeader>
    </jasperReport>
    

    Example of datasource

    <report>
    <entry><id>1</id><detail>A</detail></entry>
    <entry><id>2</id><detail>B</detail></entry>
    <entry><id>3</id><detail>C</detail></entry>
    <entry><id>4</id><detail>D</detail></entry>
    <entry><id>5</id><detail>E</detail></entry>
    <entry><id>6</id><detail>F</detail></entry>
    <entry><id>6</id><detail>G</detail></entry>
    </report>
    

    Result:

    PDF RESULT