0
votes

Hi I am new to the Jasper Reports. I am using jasper report for exporting the data to PDF format it working fine. But the problem is I want the column to be Dynamic i.e Based on the user input selection the report columns may vary (user is selected one Trip only column will be present, if he selected two trips two columns will be present like that) . one input means table will look like below

Date    Trip1
11/04/15    5
12/04/15    8
13/04/15    9

two input means table will look like below

Date    Trip1   Trip2
11/04/15    5   56
12/04/15    8   34
13/04/15    9   22

So any one help he how to do this things. Here my jrxml file:

<?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="all_dto" language="groovy" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="89b85722-ba33-45f9-8706-74b3dacad933">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="title" class="java.lang.String">
        <defaultValueExpression><![CDATA[DDDDD]]></defaultValueExpression>
    </parameter>
    <parameter name="reportId" class="java.lang.String"/>
    <parameter name="operationTime" class="java.lang.String"/>
    <field name="Date" class="java.lang.String"/>
    <field name="Distance" class="java.lang.String"/>

    <variable name="agencyName" class="java.lang.String"/>
    <background>
        <band/>
    </background>
    <title>
        <band height="30">
            <frame>
                <reportElement uuid="85d468b6-12c0-4042-8566-b50602873de0" mode="Opaque" x="0" y="0" width="500" height="24" backcolor="#006699"/>
                <textField>
                    <reportElement uuid="5769f1cf-6378-4bb0-b172-4716e2eb79fd" x="18" y="0" width="450" height="18" forecolor="#FFFFFF"/>
                    <textElement markup="none">
                        <font size="12" isBold="true"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
                </textField>

            </frame>
        </band>
    </title>
    <columnHeader>
        <band height="21">
            <staticText>
                <reportElement uuid="6fe065c4-169e-4505-b362-ac4251c8b1f6" mode="Opaque" x="0" y="0" width="100" height="21" forecolor="#006699" backcolor="#E6E6E6"/>
                <textElement>
                    <font size="9" isBold="true"/>
                </textElement>
<text><![CDATA[Date]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="6fe065c4-169e-4505-b362-ac4251c8b1f6" mode="Opaque" x="100" y="1" width="83" height="20" forecolor="#006699" backcolor="#E6E6E6"/>
                <textElement>
                    <font size="9" isBold="true"/>
                </textElement>
                        <text><![CDATA[Distance]]></text>

            </staticText>

        </band>
    </columnHeader>
    <detail>
        <band height="21">
                <textField>
                <reportElement uuid="8d449f27-3ff5-4f72-9da6-c0f816e48c9e" x="0" y="0" width="100" height="21" forecolor="#666666"/>
                <textElement>
                    <font size="8" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{Date}]]></textFieldExpression>

            </textField>
            <textField>
                <reportElement uuid="8d449f27-3ff5-4f72-9da6-c0f816e48c9e" x="100" y="1" width="83" height="20" forecolor="#666666"/>
                <textElement>
                    <font size="8" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{Distance}]]></textFieldExpression>
            </textField>

        </band>
    </detail>
    <pageFooter>
        <band height="30">
            <textField>
                <reportElement uuid="df503057-dcfe-47a4-ba3c-240611bdf374" mode="Opaque" x="0" y="17" width="515" height="13" backcolor="#E6E6E6"/>
                <textElement textAlignment="Right">
                    <font size="6"/>
                </textElement>
                <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement uuid="361f0bb0-014d-402f-80f3-fe748ced00a9" mode="Opaque" x="515" y="17" width="40" height="13" backcolor="#E6E6E6"/>
                <textElement>
                    <font size="6"/>
                </textElement>
                <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
            <textField pattern="EEEEE dd MMMMM yyyy">
                <reportElement uuid="98d5cae2-2532-4834-8de0-73b47852ac1f" x="0" y="17" width="100" height="13"/>
                <textElement/>
                <textFieldExpression><![CDATA[$P{operationTime}]]></textFieldExpression>
            </textField>
        </band>
    </pageFooter>
</jasperReport>
1
You can do it in at least 2 different ways. Use a crosstab (see the Jasper help although this is fairly obvious) or create multiple columns.kayakpim

1 Answers

0
votes

You can do it in at least 2 different ways. Use a crosstab (see the Jasper help although this is fairly obvious) or create multiple columns.

In the report properties select number of columns as the maximum you'd ever want to display. Then create a group and break on the data you want to group by (in this case trip). Create a column header and footer and put the trip field in the header. In the detail field add a column break from the palette.

Note you'll need to pass in a collection parameter containing the trips you want to display, matching with the $X IN operator in the query.

See code below which will work with a sqlserver datasource:

<?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="report12" language="groovy" columnCount="3" pageWidth="595" pageHeight="842" columnWidth="185" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="427aadaf-d91a-41be-8ab6-4c8b67934889">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString>
        <![CDATA[select getdate() date,
       1 as trip_no,
       11 as val
union
select getdate() date,
       2 as trip_no,
       12 as val
union
select getdate() date,
       3 as trip_no,
       15 as val
union
select getdate() date,
       1 as trip_no,
       25 as val
union
select getdate() date,
       2 as trip_no,
       10 as val
union
select getdate() date,
       3 as trip_no,
       5 as val
]]>
    </queryString>
    <field name="date" class="java.sql.Timestamp"/>
    <field name="trip_no" class="java.lang.Integer"/>
    <field name="val" class="java.lang.Integer"/>
    <group name="x">
        <groupExpression><![CDATA[$F{trip_no}]]></groupExpression>
    </group>
    <group name="trip">
        <groupExpression><![CDATA[$F{trip_no}]]></groupExpression>
        <groupHeader>
            <band height="20">
                <textField>
                    <reportElement uuid="531f49e8-78a7-4786-a788-7e7588263e48" x="0" y="0" width="183" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{trip_no}]]></textFieldExpression>
                </textField>
            </band>
        </groupHeader>
        <groupFooter>
            <band height="1">
                <break type="Column">
                    <reportElement uuid="a224f69e-5d76-4cd2-8959-373bdaa51b78" x="0" y="0" width="100" height="1"/>
                </break>
            </band>
        </groupFooter>
    </group>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="8f94c9e6-e5bf-4904-a529-923acca4dc2a" x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="9997bf64-2fe8-47fd-9b76-b501d210d309" x="100" y="0" width="83" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{val}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>