Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter to the table component?

I am using Jaspersoft Studio to create the reports and I found a problem passing the parameters to a table.

Report layout and result

Report layout Result

As you can see in these pictures, when I try to use the parameters to set the table headers I get null.

In order to get the values, I created the same parameter names in both, the report parameters list and the tables dataset parameters list. Obviously, it didn't work at all.

I tried to follow these instructions: jaspersoft-passing-parameter-to-tables

But my datasource is from a net.sf.jasperreports.engine.data.JRBeanCollectionDataSource parameter and I couldn't find the way to complete the tutorial successfully.

Relevant code:

<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="subreport" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="19165c65-293a-42f1-9c92-0f4ceacaaa39">
  .....
  <subDataset name="ATableDataset" uuid="f7f9da25-c513-46bb-92c2-5a97ee76eb5e">
    <parameter name="tableHeadA" class="java.lang.String"/>
    <parameter name="column1AHead" class="java.lang.String"/>
    <parameter name="column2AHead" class="java.lang.String"/>
    <parameter name="column3AHead" class="java.lang.String"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="column1A" class="java.lang.String"/>
    <field name="column2A" class="java.lang.String"/>
    <field name="column3A" class="java.lang.String"/>
  </subDataset>
   .....
  <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
    <datasetRun subDataset="ATableDataset" uuid="8ac10f33-c9e3-4678-8280-2830a76f72c6">
    <dataSourceExpression><![CDATA[$P{AtableDatasource}]]></dataSourceExpression>
   </datasetRun>
    ....
</jr:table>
......
</jasperReport>

Full code see gitHub, sample\src\main\resources\subreport.jrxml

like image 240
Michael Knight Avatar asked Feb 09 '16 18:02

Michael Knight


1 Answers

To pass a parameter to a table component you need to:

1. Define the parameter in the datasource with desired class

Example

<subDataset name="TableDataset" uuid="da383fc2-e830-42d2-a822-6a65972efe4c">
    <parameter name="myParameter" class="java.lang.String"/>
     .......
</subDataset>

2. Pass a report parameter, variable or field using the datasetParameter tag

Example

<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
   <datasetRun subDataset="TableDataset" uuid="5c37a139-a9dc-4f1b-b231-5dd82794ae4f">
       <datasetParameter name="myParameter">
             <datasetParameterExpression><![CDATA[$P{reportParameter}]]></datasetParameterExpression>
       </datasetParameter>
        <dataSourceExpression><![CDATA[....]]></dataSourceExpression>
   </datasetRun>
    ......
</jr:table>

In your code I found 1 but not 2.

like image 132
Petter Friberg Avatar answered Nov 06 '22 07:11

Petter Friberg