Chris
Chris

Reputation: 4446

Displaying an ArrayList parameter with a certain format in JasperSoft Studio report

I'm new to Jaspersoft Studio, so I'd like to know if it's possible to do the following:

The problem

For my report in Jaspersoft Studio, I'm passing in parameters from a java class, and one of the parameters is an ArrayList that contains information such as this:

ArrayList<String> benefits = new ArrayList<String>();
benefits.add("benefit_1");
benefits.add("benefit_2");
benefits.add("benefit_3");
benefits.add("benefit_4");

In my report, I need to display the information like this:

- Benefit: benefit_1
- Benefit: benefit_2
- Benefit: benefit_3
- Benefit: benefit_4

This ArrayList has a variable amount of items, so sometimes there are 4, sometimes only 2, etc.

How can I iterate through this list in the report and output it in this format?

What I've tried

From reading other stackoverflow questions, I've tried adding a subreport with the ArrayList as the data source:

<subreport>
  <reportElement ... />
  <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{benefits})]]></dataSourceExpression>
  <subreportExpression><![CDATA["benefits.jasper"]]></subreportExpression>
</subreport>

But in the subreport, how would I display the ArrayList elements?

Or is it possible to do what I'm trying to do without the subreport?

Upvotes: 2

Views: 2503

Answers (1)

Chris
Chris

Reputation: 4446

Well, I couldn't figure out how to get it to work with the ArrayList I mentioned in my question. I ended up having to change it from an ArrayList of Strings to an ArrayList of HashMaps:

ArrayList<HashMap> benefits = new ArrayList<HashMap>();
// quick and easy, though not optimal, way of adding HashMaps for testing
benefits.add(new HashMap<String, String>(){{put("benefit","benefit_1");}});
benefits.add(new HashMap<String, String>(){{put("benefit","benefit_2");}});
benefits.add(new HashMap<String, String>(){{put("benefit","benefit_3");}});
benefits.add(new HashMap<String, String>(){{put("benefit","benefit_4");}});

Then I updated the code in my main report like this:

<subreport>
  <reportElement ... />
  <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRMapCollectionDataSource($P{benefits})]]></dataSourceExpression>
  <subreportExpression><![CDATA[$P{TEMPLATE_DIR}+"benefits.jasper"]]></subreportExpression>
</subreport>

Basically, instead of using JRBeanCollectionDataSource I had to use JRMapCollectionDataSource.

Finally I created the subreport named "benefits", using only the detail band like this:

<detail>
  <band height="14" splitType="Stretch">
    <textField>
      <reportElement ... />
      <textElement>...</textElement>
      <textFieldExpression><![CDATA["- Benefit: "+$F{benefit}]]></textFieldExpression>
    </textField>
  </band>
</detail>

The $F{benefit} uses the key from the HashMap inside the ArrayList to get the correct value. That's why I couldn't use a simple ArrayList of Strings.

Upvotes: 2

Related Questions