views:

2207

answers:

2

Hey! I am working on jrxml to create dynamic re0ports. I have parameterized the columns i.e. the jrxml for that report can be used to generate other reports as well.

However, i have not managed to make the fields flexible. That is, if the user selects 4 columns it would work but if 1 or 2 or 3 columns are selected, it gives an error since the field names are unidentified.

Please post a solution urgently if something like a default expression for fieldname can be created or a for loop/java script can be used.

Moreover, how can jasper designer be exactly used to achieve this?

The jrxml is as follows:

<parameter name="reportTitle" class="java.lang.String"/>
<parameter name="author" class="java.lang.String"/>
<parameter name="startDate" class="java.lang.String"/>

**<parameter name="C1" class="java.lang.String">
    <defaultValueExpression>
        new java.lang.String("")
    </defaultValueExpression>
</parameter>
<parameter name="C2" class="java.lang.String">
    <defaultValueExpression>
        new java.lang.String("")
    </defaultValueExpression>
</parameter>
<parameter name="C3" class="java.lang.String">
    <defaultValueExpression>
        new java.lang.String("")
    </defaultValueExpression>
</parameter>
<parameter name="C4" class="java.lang.String">
    <defaultValueExpression>
        new java.lang.String("default parameter value")
    </defaultValueExpression>
</parameter>**

<field name="COLUMN_1" class="java.lang.Integer"/>
<field name="COLUMN_2" class="java.lang.Integer"/>
<field name="COLUMN_3" class="java.lang.Integer"/>
<field name="COLUMN_4" class="java.lang.Integer"/>

<title>
    <band height="60">
        <textField>
            <reportElement x="0" y="10" width="500" height="40"/>
            <textElement textAlignment="Center">
                <font size="24"/>
            </textElement>
            <textFieldExpression class="java.lang.String">
                <![CDATA[$P{reportTitle}]]>
            </textFieldExpression>
        </textField>
        <textField>
            <reportElement x="0" y="40" width="500" height="20"/>
            <textElement textAlignment="Center"/>
            <textFieldExpression class="java.lang.String">
                <![CDATA["Run by: " + $P{author}
                    + " on " + $P{startDate}]]>
            </textFieldExpression>
        </textField>
    </band>
</title>

        <textField>
            <reportElement x="0" y="5" width="170" height="15"/>
            <textFieldExpression class="java.lang.String">
                <![CDATA[$P{C1}]]>
            </textFieldExpression>
        </textField>

        <textField>
            <reportElement x="70" y="5" width="170" height="15"/>
          <textFieldExpression class="java.lang.String">
                <![CDATA[$P{C2}]]>
            </textFieldExpression>
        </textField>

        <textField>
            <reportElement x="150" y="5" width="150" height="15"/>
          <textFieldExpression class="java.lang.String">
                <![CDATA[$P{C3}]]>
            </textFieldExpression>
        </textField>

        <textField>
            <reportElement x="300" y="5" width="150" height="15"/>
          <textFieldExpression class="java.lang.String">
                <![CDATA[$P{C4}]]>
            </textFieldExpression>

        </textField>


        </band>
</columnHeader>

        <textField>
            <reportElement x="5" y="0" width="50" height="15"/>
            <textElement/>
            <textFieldExpression class="java.lang.Integer">
                <![CDATA[$F{COLUMN_1}]]>
            </textFieldExpression>
        </textField>

        <textField>
            <reportElement x="90" y="0" width="150" height="15"/>
            <textElement/>
            <textFieldExpression class="java.lang.Integer">
                <![CDATA[$F{COLUMN_2}]]>
            </textFieldExpression>
        </textField>

        <textField>
            <reportElement x="170" y="0" width="50" height="15"/>
            <textElement/>
            <textFieldExpression class="java.lang.Integer">
                <![CDATA[$F{COLUMN_3}]]>
            </textFieldExpression>
        </textField>

        <textField>
            <reportElement x="320" y="0" width="150" height="15"/>
            <textElement/>
            <textFieldExpression class="java.lang.Integer">
                <![CDATA[$F{COLUMN_4}]]>
            </textFieldExpression>
        </textField>
    </band>
</detail>

+1  A: 

In case you want it to be dynamic as in hiding/showing new fields, i think the only way would be to modify your jrxml at runtime and compile it.

UPDATE:

To make the JasperReport dynamic at runtime you have three approaches:

1. Create JRXML in iReport
If your report is complicated and have many subreports and subdatasets, its better to go this approach, since iReport will make it quicker and easier for you to design and maintain later.
When following this approach you will create the jrxml file and store in the classpath, at runtime you will load this file, open it, and modify the XML tags you want. I would recommend to start with a jrxml that includes the max number of columns, and then if user select less columns then find those additional columns in the jrxml and delete them.

2. Create whole report in Java
This approach is better if your report is really simple, like a quick tabular report, in this case you dont need a jrxml file, you can create the whole report at runtime from scratch using JasperReport library APIs. This approach will make designing the report harder as you wont see anything until you compile and run. Yet worse its a maintenance nightmare.
Your final Java source code will look something like this: "Fixed Column Width Test"1

3. Use DynamicJasper
Finally, if you are open to new tools, there is always DynamicJasper, you can check the online demo as well, its pretty cool.
I kept DJ for the end to show you all the approaches, DJ actually uses the second one. Creating Java source code of the report on the fly.


1: Fixed Column Width Test Report copyrights are for DynamicJasper and used here just as a sample.

medopal
yes i know that but how to modify it that was my question? ny idea about it?
Sundhas
Thanks medopal I am open to new tools but problem is that i dont know how to use DJ as i want to send attributes dynamically from my front end to the DJ but how i cant figure out that...ever used DJ?
Sundhas
Its pretty easy to use, download DJ, go the Getting Started guide and from there its easy to make your first report. And yes sure you will be able to create the report dynamically from front end attributes, that's exactly why DJ is made :)
medopal
Yes, i did the tutorial but i have already made a servlet which sends selected attributes from the front end gives it to another servlet and that servlet wud be of dynamic jasper ? previously that servlet is of JRXML code thing...and then JRXMl makes report of selected attributes.Can you guide me what will be the flow now?
Sundhas
will answer this on your other question. To make things organized we will keep this answer as is.
medopal