views:

35

answers:

2

I've used jasper without using xml (I must change this still) but I have an implementation of a JasperDesign that i make use of ... I create a JRDesignTextField with a JRDesignExpression,

I would like to setup the expression so that I can get jasper to call a method on each element within a JRBeanCollectionDataSource with a integer parameter... currently i can only call methods that returns a value and take in no arguments. The expression for this is shown below:

final JRDesignExpression exp = new JRDesignExpression(); exp.setValueClass(String.class); exp.setValueClassName(String.class.getName()); exp.addFieldChunk("column0");

A: 

You can use setText instead of adding chunks yourself. Jasper will parse the text, adding chunks as needed - $P{param} for parameters, $F{field} for fields, $V{var} for variable, etc. For example:

exp.setText( "$F{column0}.myMethod($V{someIntVar})" )
yshalbar
So if I have a class with a method: public String getName(int columnIndex){...}, will the $F{column0} refer to that class? I give jasper a JRBeanCollectionDataSource with a list of these classes (one per column) in its constructor.
Craig
Previously I had methods public String getColumn0(){...} public String getColumn1(){...} etc and now would like just one method called public String getName(int columnIndex){...}
Craig
+1  A: 

You should consider using a different data-source, other than JRBeanCollectionDataSource. The JRBeanCollectionDataSource calls getter methods for each field, which is what you want to avoid.

You can use JRMapCollectionDataSource, which is created from a collection of maps. You can call the keys of your maps as you wish, like 'column0', 'column1' (or simply '0', '1', etc.)

Another option is to directly implement the JRDataSource interface, where you can implement the getFieldValue() whichever way you like.

yshalbar