How to pass parameter to the table component?

依然范特西╮ 提交于 2020-02-21 12:37:41

问题


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

Report layout and 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


回答1:


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.



来源:https://stackoverflow.com/questions/35300072/how-to-pass-parameter-to-the-table-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!