frontGeek
frontGeek

Reputation: 43

JasperReports: Error creating table trying to use CrossTab without column group

I'm wondering if there is a way to make a table like this enter image description here using the JapserReports CrossTab or in any other way where the row group is the field value7.

The DataSet used is a JRBeanCollectionDataSource(list of SubViews).

Where SubView is:

public class SubView{

  //...

  private String value1;
  private String value2;
  private String value3;
  private String value4;
  private String value5;
  private String value6;
  private String value7;

  //...getters and setters...
}

Note that there is no need of a column group, but jasper doesn't compile successfully if I don't configure a column group (error message: Crosstab should have at least one column group).

Upvotes: 0

Views: 1761

Answers (1)

Petter Friberg
Petter Friberg

Reputation: 21710

This to me does not seem like a crosstab problem. Crosstab's are used when the columns are dynamic, hence your error message

Crosstab should have at least one column group.

since there is "no need of a column group" the result is achived using the normal detail band and grouping on value7.

So after you put the labels in the columnHeader and the values 1-6 in the detail band you add a grouping on value7 with a groupHeader band

Example (I have only included until value 3 in detail band to sorten the answer

<?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="report2" printOrder="Horizontal" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isFloatColumnFooter="true" uuid="8944f542-2955-4224-933c-5a87fd36f527">
<queryString>
    <![CDATA[]]>
</queryString>
<field name="value1" class="java.lang.String"/>
<field name="value2" class="java.lang.String"/>
<field name="value3" class="java.lang.String"/>
<field name="value7" class="java.lang.String"/>
<group name="Value7">
    <groupExpression><![CDATA[$F{value7}]]></groupExpression>
    <groupHeader>
        <band height="21">
            <textField>
                <reportElement x="0" y="0" width="300" height="20" uuid="208ca6e5-b396-47cb-858b-71039f5bdf7a"/>
                <textFieldExpression><![CDATA["Value 7: " + $F{value7}]]></textFieldExpression>
            </textField>
        </band>
    </groupHeader>
</group>
<background>
    <band/>
</background>
<columnHeader>
    <band height="50">
        <staticText>
            <reportElement x="0" y="30" width="100" height="20" uuid="edf0f0f1-c979-4d20-987e-e8decb0b584a"/>
            <text><![CDATA[value1]]></text>
        </staticText>
        <staticText>
            <reportElement x="100" y="30" width="100" height="20" uuid="4287323f-e6d3-40d8-a2d4-44981bfa5c59"/>
            <text><![CDATA[value2]]></text>
        </staticText>
        <staticText>
            <reportElement x="200" y="30" width="100" height="20" uuid="b9aa7dfd-edd8-439b-b4c7-12916a740da8"/>
            <text><![CDATA[value3]]></text>
        </staticText>
    </band>
</columnHeader>
<detail>
    <band height="24">
        <textField>
            <reportElement x="0" y="0" width="100" height="20" uuid="2bcc436d-4c3a-4db1-a2ea-87edf92af98f"/>
            <textFieldExpression><![CDATA[$F{value1}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="100" y="0" width="100" height="20" uuid="f893721e-1aa6-44ca-9a9a-ac795ff63a0a"/>
            <textFieldExpression><![CDATA[$F{value2}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="200" y="0" width="100" height="20" uuid="36ad9612-620b-4eec-9608-f26b5306b01a"/>
            <textFieldExpression><![CDATA[$F{value3}]]></textFieldExpression>
        </textField>
    </band>
</detail>
</jasperReport>

If you like to use crosstab anyway, you need to setup your dataset differently (since also columns become dynamic) check out this for examples: crosstab example

Upvotes: 1

Related Questions