We help IT Professionals succeed at work.

Help with Jasper Reports

pothios
pothios asked
on
406 Views
Last Modified: 2008-08-11
hello. im trying to create a dynamic report by allowing users to select the data in the database as the headers of the report. from my understanding, the field parameter in the jrxml will map to the datasource. in order to achieve my goal i  got to substitue the field name as the input user selected. is that right? whats the way of doing it? please help. thanks.
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Could you rephrase your question? I'm not sure if I understood it correctly.

Author

Commented:
hi brunoguimaraes,

im trying to substitute the field with user selected data. in the given example below, i want to replace the fields with db data and when the report is run, it will populate the report with all the resultset .

field1 field2 field3 field4 field5 field6 field7   field8      field9 field10

user selects id, name, age, job, tel, add, hobby and income.
id       name    age     job   tel     add   hobby  income    -           -
xx     xxx        xxx    xxx  xx    xxx   xxx       xxx
xx     xxx        xxx    xxx  xx    xxx   xxx       xxx
xx     xxx        xxx    xxx  xx    xxx   xxx       xxx
xx     xxx        xxx    xxx  xx    xxx   xxx       xxx
xx     xxx        xxx    xxx  xx    xxx   xxx       xxx

i think i nid to modify the field tag in order to do this, right?
You have to create fields for each resultset column. Then you reference those fields later in the report.

For example, for the columns ID and NAME you would do:

<field name="id" class="java.lang.Integer">
      <fieldDescription><![CDATA[User ID]]></fieldDescription>
</field>
<field name="name" class="java.lang.String">
      <fieldDescription><![CDATA[User Name]]></fieldDescription>
</field>

Then, to reference them, you do:

<textField ... >
      <reportElement ... />
      <textElement>
            <font fontName="Times New Roman" pdfFontName="Times-Roman"/>
      </textElement>
      <textFieldExpression class="java.lang.String"><![CDATA[$F{id}]]></textFieldExpression>
</textField>

<textField ... >
      <reportElement ... />
      <textElement>
            <font fontName="Times New Roman" pdfFontName="Times-Roman"/>
      </textElement>
      <textFieldExpression class="java.lang.String"><![CDATA[$F{name}]]></textFieldExpression>
</textField>
If you're using iReport to design your report, you create the fields in the menu View -> Fields. Then you click on the New button and you're set.

To reference a field you create, just click the F icon in the toolbar. Put the field somewhere in the report, then right-click it and choose "Edit expression...".

On the list of fields that appear, double-click the one you want. The expression will be automatically generated.

I hope that's clear for you!

Author

Commented:
will i be able to do somethuing like this?

params.put("field1", "test");
params.put("field1", "xxx");

the goal is to allow dynamic selection of fields to display.


<field name="id" class="java.lang.Integer">
      <fieldDescription><![CDATA[field1]]></fieldDescription>
</field>
<field name="name" class="java.lang.String">
      <fieldDescription><![CDATA[field2]]></fieldDescription>
</field>
.....
 
<textField ... >
      <reportElement ... />
      <textElement>
            <font fontName="Times New Roman" pdfFontName="Times-Roman"/>
      </textElement>
       //i assume dat the textfield here should be the same name as the field
      <textFieldExpression class="java.lang.String"><![CDATA[$F{field1}]]></textFieldExpression>
</textField>
 
<textField ... >
      <reportElement ... />
      <textElement>
            <font fontName="Times New Roman" pdfFontName="Times-Roman"/>
      </textElement>
      <textFieldExpression class="java.lang.String"><![CDATA[$F{field2}]]></textFieldExpression>
</textField>
 
thanks.

Open in new window

Author

Commented:
opps it shld be

params.put("field1", "test");
params.put("field2", "xxx");
What you wanna do is dynamically decide whether to display a field?

If so, there is a better way of doing it.

For example, your report has 4 fields: field1, field2, field3 and field4. You have to put all of them on the report.

Then you pass parameters to indicate whether the fields are visible.

params.put("isField1Visible", true);
params.put("isField2Visible", false);
params.put("isField3Visible", true);
params.put("isField4Visible", true);

Then, on the report, you can say that field1 is only visible if the parameter isField1Visible is true, and so on.

Is that what you wanna do?

Author

Commented:
sry for the misunderstanding. im trying to achieve dynamic columns, meaning columns will only be generated on runtime. i've done some research and notice dynamicjasper api can do the job. have u done dynamic columns before?
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.