How to get dynamic no of tables using json

2019-01-29 04:15发布

问题:

I want to create Jasper Report with Dynamic no of tables.
FOR EXAMPLE:

  • User 1 - 5 tables
  • User 2 - 3 tables
  • User 3 - 6 tables

I tried with list option but my tables are not visible and number of pages are getting increased.

<subDataset name="tableDataSet" uuid="c72d8dbd-d63e-411b-862d-a0d6e1e25d37">
        <queryString language="json">
            <![CDATA[tableData]]>
        </queryString>
        <field name="description" class="java.lang.String">
            <fieldDescription><![CDATA[description]]></fieldDescription>
        </field>
</subDataset>
<subDataset name="listDataSet" uuid="c72d8dbd-d63e-411b-862d-a0d6e1e25d37">
        <queryString language="json">
            <![CDATA[listData]]>
        </queryString>
        <field name="tableData" class="java.lang.String">
        <fieldDescription><![CDATA[tableData]]></fieldDescription>
    </field>
</subDataset>
<queryString language="json">
        <![CDATA[mainDataSet]]>
</queryString>
<field name="listData" class="java.lang.String">
    <fieldDescription><![CDATA[listData]]></fieldDescription>
</field>
<componentElement>
<reportElement x="-1" y="170" width="830" height="150" uuid="ab0272db-d740-42c9-94e5-238a2cc4a63e"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="listDataSet" uuid="f80306fb-80fc-405b-b90d-f9fe6b6ee2e1">
    <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("listData")]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="150" width="830">
    <componentElement>
<reportElement positionType="Float" x="0" y="67" width="825" height="76" uuid="6a8e89c4-d4ea-4d8f-b932-8fbb24d00725"/>
<jr:table>
    <datasetRun subDataset="tableDataSet" uuid="6ab7d349-34c8-4db7-bf3c-4e638ca36f5c">
        <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("tableData")]]></dataSourceExpression>
    </datasetRun>
    <jr:column width="100" uuid="aae649c4-6a69-485f-8a0d-87022df793ee">
    <jr:columnHeader style="colBotLeftLine" height="30">
        <staticText>
        <reportElement x="0" y="0" width="100" height="30" uuid="795dac43-0ff0-482c-89a0-7dac3b27d513"/>
        <textElement>
            <font isBold="true"/>
        </textElement>
        <text><![CDATA[DESCRIPTION]]></text>
        </staticText>
    </jr:columnHeader>
    <jr:columnFooter style="footerBotLeftLine" height="20">
        <staticText>
        <reportElement x="0" y="0" width="100" height="20" uuid="795dac43-0ff0-482c-89a0-7dac3b27d513"/>
        <textElement>
            <font isBold="true"/>
        </textElement>
        <text><![CDATA[Grand Total]]></text>
        </staticText>
    </jr:columnFooter>
    <jr:detailCell height="20" rowSpan="1">
        <textField>
        <reportElement x="0" y="0" width="100" height="20" uuid="734834e8-5896-4355-9454-6b188e86bfff"/>
        <textFieldExpression><![CDATA[$F{description}]]></textFieldExpression>
        </textField>
        </jr:detailCell>
    </jr:column>
</jr:table>
</componentElement>
</jr:listContents>
</jr:list>
</componentElement>

Json Data.

{mainDataSet :
 [
  {CompanyName : "XYZ",
   listData:
    [
     {tableId:1,
      tableName:"Table One",
      tableData:
        [
        {description:"Table One Row Data One "},
        {description:"Table One Row Data Two"}
        ]
     },
     {tableId:2,
      tableName:"Table Two",
      tableData:
        [
        {description:"Table Two Row Data One "},
        {description:"Table Two Row Data Two"}
        ]
     }
    ]
  }
 ]
}

mainDataSet is my Report Data Set, listData is the data for list and tableData is the one i want to display in my tables.
NOTE
If I remove the

<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("tableData")]]></dataSourceExpression>

and put

<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>

Then Two tables are created with null values.

回答1:

The query to main report let's leave it blank (and keep the mainDataSet node)

<queryString language="json"><![CDATA[]]></queryString>

Define the CompanyName field as

<field name="CompanyName" class="java.lang.String">
    <fieldDescription><![CDATA[mainDataSet[0].CompanyName]]></fieldDescription>
</field>

The dataSourceExpression to pass to the jr:list

<datasetRun subDataset="listDataSet" uuid="71276e30-7777-44ae-b6d9-2087a4c51ca3">
   <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("mainDataSet.listData")]]></dataSourceExpression>
</datasetRun>

Hence point to the mainDataSet.listData

The dataSourceExpression to pass to the table (we are now in the listData)

<datasetRun subDataset="tableDataSet" uuid="5bca90cb-1473-4ff6-82fc-1da5ae4fb44c">
    <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("tableData")]]></dataSourceExpression>
</datasetRun>