How to set the field used for a group by parameter

 

Hi all,

 

I'm new with JasperReports, and I'm asking myself if there any way to set by parameter the field to use for a group ? And I have the same question for a textField.

 

I will always have the same fields but I want to be able to change the grouping and one textfield specifying the field by parameter.

 

Is it doable ?

 

Or I have to change it programmatically when the .jxml if precompiled ? Then what is the best way to change a small part of the .jxml like that ?

 

 

Thanks,

Phil

bob007's picture
534
Joined: Apr 5 2009 - 6:26pm
Last seen: 14 years 6 months ago

14 Answers:

I  tested with parameters values:
- bondetravail_groupe
- $F{bondetravail_groupe}
- <![CDATA[$F{bondetravail_groupe}]]>

The grouping are not working with the specified parameter.
The textField display (in result) the string exactly like the parameter string (ex: $F{bondetravail_groupe} ) not like the value of specified field.


So I guess the only way to modify the report is doing it programmaticly.  To build my report with JasperDesign.


I don't want to have a template (.jasper) for each grouping possibilities.

bob007's picture
534
Joined: Apr 5 2009 - 6:26pm
Last seen: 14 years 6 months ago

Try this: $P{REPORT_SCRIPTLET}.getFieldValue($P{fieldNameParameter}).

HTH,

Lucian

lucianc's picture
87369
Joined: Jul 17 2006 - 1:10am
Last seen: 4 hours 10 min ago

 

 

Hi, i tried that and it didnt work. with the value parameter  value: bondetravail_noBon.

 

   <parameter name="fieldNameParameter" class="java.lang.String">

      <defaultValueExpression><![CDATA[]]></defaultValueExpression>

   </parameter>

...

 

<field name="bondetravail_noBon" class="java.lang.String"/>

 

 

...

 

 

   <group name="groupe">

      <groupExpression><![CDATA[$P{REPORT_SCRIPTLET}.getFieldValue( $P{fieldNameParameter} )]]></groupExpression>

      <groupHeader>

         <band height="31">

            <textField isBlankWhenNull="true">

               <reportElement x="1" y="11" width="187" height="20"/>

               <textElement>

                  <font size="14" isBold="true"/>

               </textElement>

               <textFieldExpression class="java.lang.String"><![CDATA[$P{REPORT_SCRIPTLET}.getFieldValue( $P{fieldNameParameter} )]]></textFieldExpression>

            </textField>

         </band>

      </groupHeader>

      <groupFooter>

         <band height="26"/>

      </groupFooter>

   </group>

 

 

bob007's picture
534
Joined: Apr 5 2009 - 6:26pm
Last seen: 14 years 6 months ago

If you add a text field that displays the same expression (the one that calls getFieldValue), does it show the correct values?

lucianc's picture
87369
Joined: Jul 17 2006 - 1:10am
Last seen: 4 hours 10 min ago

 

No it show:   null

in the textfield

 

* ( I test it in iReport 3.5.0 )



 



Post Edited by Philippe Yelle at 04/14/09 16:55
bob007's picture
534
Joined: Apr 5 2009 - 6:26pm
Last seen: 14 years 6 months ago

Apparently starting from JR 3.1.4 you need to explicitly set a report scriptlet class in order to use $P{REPORT_SCRIPTLET}.  So you should set net.sf.jasperreports.engine.JRDefaultScriptlet as scriptlet class for the report in order to make the expression work.

Regards,

Lucian

lucianc's picture
87369
Joined: Jul 17 2006 - 1:10am
Last seen: 4 hours 10 min ago

 

Ok its near to work.

Just one thing: it don't print a new group header for each group (just for the first group).

 



 

 


 



Post Edited by Philippe Yelle at 04/15/09 13:11
bob007's picture
534
Joined: Apr 5 2009 - 6:26pm
Last seen: 14 years 6 months ago

Yep, that's a problem.  Apparently the expressions does not work as expected when used as group expression.  Let me think if there is any solution for this..

Regards,

Lucian

lucianc's picture
87369
Joined: Jul 17 2006 - 1:10am
Last seen: 4 hours 10 min ago

There is a solution, which is more of a hack and not very pretty.  See this thread, the part about the EvaluationScriptlet.  Note that there would be a (hopefully) small performance penalty due to the fact that some time will be spent filling the exception stacktrace; perform some tests to verify that the performance does not significantly alter.

HTH,

Lucian



Post Edited by Lucian Chiriţă at 04/17/09 16:11
lucianc's picture
87369
Joined: Jul 17 2006 - 1:10am
Last seen: 4 hours 10 min ago

Hi,

I would like to have a dynamic group on a report but the group headers are not presented. I have developed my report in the iReports. I did the following steps:

1.define a parameter P_GROUP_COLUMN

2.modify sql statement    SELECT a.name, a,surname, a.countryId, b.post FROM person a, post b WHERE a.PostId=b.PostId ORDER BY  a.$P!{P_GROUP_COLUMN}     (SQL is not the real one, here is just a simple example used to present table alias in order by)

3.Define a new group GENERIC_GROUP  <group name="GENERIC_GROUP">
  <groupExpression><![CDATA[$P{REPORT_SCRIPTLET}.getFieldValue($P{P_GROUP_COLUMN})]]></groupExpression>

4.Define Scriptlet class EvaluationScriptlet ( I copy the script from http://jasperforge.org/plugins/espforum/view.php?group_id=102&forumid=103&topicid=41760)

Group headers are not presented in the report (only the first one). Do you have any hint what should I do? EvaluationScriptlet did not help me? Should I define something more?

Regards,

Primoz

primoz's picture
198
Joined: Feb 24 2008 - 2:32am
Last seen: 1 year 2 months ago

Hi,

I spent a few more hours today but I did not find out the way to solve the problem with dynamic groups. The simple report is in the attachment. Below is java code for EvaluationScriptlet .

Regards,

Primoz

Code:
public class EvaluationScriptlet extends JRDefaultScriptlet {
 public Object evaluateField(String fieldName) throws JRScriptletException {
  JRFillField field = (JRFillField)this.fieldsMap.get(fieldName);
  if (field == null) {
   throw new JRScriptletException("Field not found : " + fieldName);
  }
 
  StackTraceElement[] stack = new Exception().getStackTrace();
  if (stack.length < 2) {
   throw new JRScriptletException("Unable to obtain current call stack");
  }
 
  String evaluateMethod = stack[1].getMethodName();
  Object value;
  if (evaluateMethod.startsWith("evaluateOld")) {
   value = field.getOldValue();
  }
  else if (evaluateMethod.startsWith("evaluate")) {
   value = field.getValue();
  }
  else {
   throw new JRScriptletException("Unable to determine evaluation type from method name " + evaluateMethod);
  }
  return value;
 }
 
 public String priorityDescription() throws JRScriptletException {
     String ls_artikelId = (String) this.getFieldValue("artikelId");
 
     return "Does it work"+ls_artikelId;
 }
}
</td></tr></tbody></table>
primoz's picture
198
Joined: Feb 24 2008 - 2:32am
Last seen: 1 year 2 months ago

You need to use $P{REPORT_SCRIPTLET}.evaluateField($P{P_GROUP_COLUMN}) as group expression (instead of getFieldValue).

Regards,

Lucian

lucianc's picture
87369
Joined: Jul 17 2006 - 1:10am
Last seen: 4 hours 10 min ago

Hi Lucian!

It works perfectly now. Thank you very much!

Regards,

Primoz

primoz's picture
198
Joined: Feb 24 2008 - 2:32am
Last seen: 1 year 2 months ago

Hi, 6 years on, does this apply to Studio?

martin.clarke's picture
Joined: Apr 20 2015 - 2:28am
Last seen: 7 years 7 months ago
Feedback