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

How to set the field used for a group by parameter


bob007

Recommended Posts

 

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

Link to comment
Share on other sites

  • Replies 14
  • Created
  • Last Reply

Top Posters In This Topic

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.

Link to comment
Share on other sites

 

 

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>

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 1 month later...

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

Link to comment
Share on other sites

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; }}
Link to comment
Share on other sites

  • 6 years later...

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