Jump to content
We've recently updated our Privacy Statement, available here ×

Subreport with different datasource


2004 IR Help

Recommended Posts

By: Simon Lee - cecid_kylee

Subreport with different datasource

2005-07-21 00:57

I am a newbie to jasper report. Currently I am testing with the feature of subreport. Below are the snippnet of my report design.

 

main.jrxml

...

<parameter name="subdatasource" isForPrompting="false" class="net.sf.jasperreports.engine.data.JRMapArrayDataSource"/>

<parameter name="subreport" isForPrompting="false" class="net.sf.jasperreports.engine.JasperReport"/>

...

<subreport isUsingCache="true">

<reportElement

mode="Opaque"

x="93"

y="57"

width="334"

height="75"

forecolor="#000000"

backcolor="#FFFFFF"

key="subreport-3"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<dataSourceExpression><![CDATA[$P{subdatasource}]]></dataSourceExpression>

<subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[$P{subreport}]]></subreportExpression>

</subreport>

 

The code snippnet to generate the report is as follows:

...

JasperReport sub = JasperCompileManager.compileReport("subreport.jrxml");

 

Map map1 = new HashMap();

map1.put("Name", "Chan Tai Man");

map1.put("Age", new Integer(12));

 

Map map2 = new HashMap();

map2.put("Name", "Chan Siu Mei");

map2.put("Age", new Integer(18));

 

Map parameters = new HashMap();

JRMapArrayDataSource subDataSource = new JRMapArrayDataSource(

new Object[] { map1, map2 });

 

parameters.put("subdatasource", subDataSource);

parameters.put("subreport", sub);

JasperReport mainReport = JasperCompileManager

.compileReport("main.jrxml");

 

JasperPrint print = JasperFillManager.fillReport(mainReport,

parameters, new JREmptyDataSource());

...

 

I have tested with the subreport alone, which data could be generated. However by using the code above, the subreport shows blank in my main report output. I am not sure what I am doing wrong. Could anyone gives me some help? Thanks in advance!

 

 

 

 

 

 

By: James Render - james_render

RE: Subreport with different datasource

2005-07-21 09:13

I would also like to use a different datasource with a sub-report.

 

My main report runs a query, one of the fields returned is in xml format. I want to display the xml, so I guess I need to pass it to a sub-report that uses a JRXmlDataSource ?

 

Has anyone done this ? thanks

 

 

 

 

By: evgenykravchenko - ekravche

RE: Subreport with different datasource

2005-07-21 10:09

yes, this is how you do it. After linking your multiple datasources via a key, you can overwrite the JRDataSource implementation.

 

This worked for me.

 

 

<----------------------begin code -------------------------->

public class SampleDS implements JRDataSource {

 

/* (non-Javadoc)

* @see net.sf.jasperreports.engine.JRDataSource#next()

*/

int counter = 0;

public boolean next() throws JRException

{

// TODO Auto-generated method stub

//assert replaceWithCondition: "replace with message To Display If Condition Fails";

if(counter < 100) {

counter++;

return true;

} else {

counter = 0;

return false;

}

}

 

/* (non-Javadoc)

* @see net.sf.jasperreports.engine.JRDataSource#getFieldValue(net.sf.jasperreports.engine.JRField)

*/

public Object getFieldValue(JRField arg0) throws JRException

{

// TODO Auto-generated method stub

//assert replaceWithCondition: "replace with message To Display If Condition Fails";

return "test" + counter;

}

 

 

}

 

 

<----------------------end code -------------------------->

 

 

Best regards.

 

--Evgeny

 

 

 

 

By: James Render - james_render

RE: Subreport with different datasource

2005-07-22 01:08

Thanks for the reply Evgeny.

 

Apologies but I don't understand your solution. Could you demonstrate how you link the multiple datasources via a key, and also why you would want to overwrite with your own Datasource implementation. Your example code - was that just a demonstration of a custom Datasource as I can't see the relevancy of what it does.

 

Basically, can you show more of your solution..

 

thanks, James.

 

 

 

 

By: James Render - james_render

RE: Subreport with different datasource

2005-07-25 02:52

Managed to figure it out myself by reviewing the samples directory, subreports and xmldatasource!

 

Basically this will do the trick!

 

<subreport isUsingCache="false">

<reportElement positionType="Float" x="308" y="6" width="262" height="278" isRemoveLineWhenBlank="true"/>

<dataSourceExpression><![CDATA[ new net.sf.jasperreports.engine.data.JRXmlDataSource(new java.io.BufferedInputStream(new java.io.ByteArrayInputStream($F{VEE_DATABEAN_MSG_TX}.getBytes())), "//ROW") ]]></dataSourceExpression>

<subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[$P{AuditSubreport}]]></subreportExpression>

</subreport>

 

Replace the "//ROW" with your xpath expression. Replace AuditSubreport with the name of the JasperReport parameter that represents your subreport. Then in the subreport, just create fields that correspond to the xml elements..

 

hope this helps someone!

 

James

Hope that this helps

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...