如何使用json获取动态no表 [英] How to get dynamic no of tables using json

查看:94
本文介绍了如何使用json获取动态no表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用动态编号表创建Jasper报告。例如:

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


  • 用户1 - 5表

  • 用户2 - 3桌

  • 用户3 - 6桌

我尝试使用list选项,但我的表格不可见,页面数量也在增加。

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 是我的报告数据集, listData 是列表的数据, tableData 是我要在我的数据中显示的那个表格。

注意

如果我删除了

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>

并且放入

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

然后用空值创建两个表。

Then Two tables are created with null values.

推荐答案

对主报表的查询让我们留空(并保留mainDataSet节点)

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

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

CompanyName 字段定义为

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

传递给<$的 dataSourceExpression c $ c> jr:list

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>

因此指向 mainDataSet.listData

传递给表的 dataSourceExpression (我们现在在 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>

这篇关于如何使用json获取动态no表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆