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

Jasper Report, parameters with collection


salvuzzo87

Recommended Posts

Hello Boys, sorry for my english. I Have one problem with Jasper Report. I Must print two collection Object, but I have not one basic Database. I have seen that you can insert in one jasper report only JRBeanCollectionDataSource. I Can transfer two list with parameters ? I have see that one parameters can be one java.util.collection, but for iterate one collection of a one parameters how do?

 

Thanks ^^

Link to comment
Share on other sites

  • Replies 11
  • Created
  • Last Reply

Top Posters In This Topic

I don't understood your questions exactly.

 

If you want to display parameter-collection in report, you mast make subreport with DataSource expression like this:

 

new YourClassNameDataSource($P{paramCollectionName})

where $P{paramCollectionName) - parameter of main report

 

and class YourClassNameDataSource must implements JRDataSource interface

Link to comment
Share on other sites

salvuzzo87
Wrote:

I need to spend 3 collections with the parameters, you can not connect to the database?

>> I need to spend 3 collections with the parameters
Use 3 params and 3 subreports :)

you can not connect to the database?
I don't understand. In my reports all information is saved in collections. I am don't need connect to database

Link to comment
Share on other sites

Ok, but i have created 3 subreport. I Mean. I use as programming languages JAVA. In one action i have 3 collections, this colletions i put them one hashmap. Now, in the subreports, how do to work this collections? If use one JRBeanCollectionDataSource in jasper report i create field with name equal the field the collections, but i can use one JRBeanCollectionDataSource, the other collections i put them parameters . In jasper report how do to iterate the other collections?

Link to comment
Share on other sites

In my first message i wrote about DataSource expression for subreport and JRDataSource interface for this class.

 

My example:

for subreport datasource expression in main report:

--- cut

new R1250DataSource($F{ascueGroup}, $P{startDate}, $P{endDate})

--- end cut

where $F{ascueGroup} - field from main report,

$P{startDate}, $P{endDate} - params of main report

In your case you may use map as parameter of constructor for dataSource for subreport

 

And for iterate on collection in subreport you must inmplements JRDataSource interface (realize methods next() and getFieldValue())

(@see net.sf.jasperreports.engine.JRDataSource )

 

--- my code for example

public class R1250DataSource implements JRDataSource {

private PredictedExpense currentPredictedExpense;

private Iterator<PredictedExpense> iterator;

 

// constructor

public R1250DataSource(Group group, Date startDate, Date endDate) throws JRException {

List<PredictedExpense> values = new ArrayList<PredictedExpense>();

try {

....

vaues.add(new PredictedExpense(...));

} catch (NullPointerException e) {

throw new JRException(e);

}

iterator = values.iterator();

}

 

@Override

public boolean next() throws JRException{

 

boolean result = iterator.hasNext();

 

if (result) {

currentPredictedExpense = iterator.next();

}

 

return result;

}

 

 

@Override

public Object getFieldValue(JRField jrf) throws JRException {

 

ExpenseMeasureChannel counterIndication = currentPredictedExpense.getCounterIndication();

 

if (jrf.getName().equals("pointName")) {

return counterIndication.getAccountPointName();

}

 

if (jrf.getName().equals("counterName")) {

return counterIndication.getCounterName();

}

 

...

return null;

}

 

and in subreport define fields with names "pointName", "counterName", ...

 

Good luck

Link to comment
Share on other sites

I am not clear, JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(myList); the compile my report and then fill my report. In my XML (generate with tool jasperreport), i have field whit name equal to those on my list. But for each repoirt i can pass only CollectionsDataSource.  The other list they step paramters just?Ok, in my XML as recovery values ? If I create field with name equal to thos on my list that i pass with paramters does not work because the field work only CollectionDatasource...

Link to comment
Share on other sites

Additional explanations:

You may use even empty dataSource for main report:

JRDataSource dataSource = new JREmptyDataSource();

 

and solve any problems on params and subreports

 

1. Make subreport (subreport.jrxml)

2. Modify main report (main.jrxml), inserting subreport into detail (or summary? band)

3. Define parameter1 as java.util.collection for main report

4. Pass list to parameter1 of main report (in java-code).

5. For element "subreport" of main report define dataSource expression:

new YourClass($P{parameter1})

6. Implements JRDataSource interface in YourClass where getFieldValue returns field as they named (and with this types as in subreport. see 1.Make subreport)

 

 

If this is not clear, please put screenshot of desired report

The way to solution your problems may be various for different "structure" of your report

 

 

 

Link to comment
Share on other sites

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...