使用表组件:如何避免显示相同数据的多个副本

时间:2012-09-04 09:30:28

标签: jasper-reports ireport

iReport 为我提交的查询生成多个(相同的)表。

我正在尝试将漫画数据库表中的所有数据提取到组件中。我成功地这样做了,但是创建了具有相同数据的多个表。

 <?xml version="1.0" encoding="UTF-8"?><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="mangaReport" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="43bb2d5b-ad44-48ca-bcc9-bec319a36466">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<style name="table">
    <box>
        <pen lineWidth="1.0" lineColor="#000000"/>
    </box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="table 1">
    <box>
        <pen lineWidth="1.0" lineColor="#000000"/>
    </box>
</style>
<style name="table 1_TH" mode="Opaque" backcolor="#F0F8FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="table 1_CH" mode="Opaque" backcolor="#BFE1FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<subDataset name="New Dataset 1" uuid="820ac89b-615e-453d-9593-9deeb8c44a87">
    <queryString language="SQL">
        <![CDATA[select * from manga]]>
    </queryString>
    <field name="name" class="java.lang.String"/>
    <field name="author" class="java.lang.String"/>
</subDataset>
<queryString language="SQL">
    <![CDATA[select * from manga]]>
</queryString>
<field name="name" class="java.lang.String"/>
<field name="author" class="java.lang.String"/>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="39" splitType="Stretch"/>
</title>
<pageHeader>
    <band height="18" splitType="Stretch"/>
</pageHeader>
<columnHeader>
    <band height="31" splitType="Stretch"/>
</columnHeader>
<detail>
    <band height="125" splitType="Stretch">
        <componentElement>
            <reportElement uuid="4f76c19e-e304-45b3-a521-b30aa4ecb03e" key="table 1" style="table 1" x="185" y="34" width="360" height="50"/>
            <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="New Dataset 1" uuid="9f479053-64b3-49a7-a986-2415ac20385f">
                    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                </datasetRun>
                <jr:column uuid="5b10c367-6de0-4799-8408-ad8c7dadd888" width="90">
                    <jr:columnHeader style="table 1_CH" height="30">
                        <staticText>
                            <reportElement uuid="7470c697-d500-46e0-88c1-d42883576ef1" x="0" y="0" width="90" height="30"/>
                            <textElement/>
                            <text><![CDATA[name]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="table 1_TD" height="20">
                        <textField>
                            <reportElement uuid="32a30162-dca0-4bda-8ef2-ae8f41d4340a" x="0" y="0" width="90" height="20"/>
                            <textElement/>
                            <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column uuid="3f6fac9d-5a87-4fee-ac81-51170b6d7481" width="90">
                    <jr:columnHeader style="table 1_CH" height="30">
                        <staticText>
                            <reportElement uuid="21335524-5b94-464b-83fc-36d7734596e4" x="0" y="0" width="90" height="30"/>
                            <textElement/>
                            <text><![CDATA[author]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="table 1_TD" height="20">
                        <textField>
                            <reportElement uuid="9765da67-b578-4bc3-b0f4-5878c64b9e66" x="0" y="0" width="90" height="20"/>
                            <textElement/>
                            <textFieldExpression><![CDATA[$F{author}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
            </jr:table>
        </componentElement>
    </band>
</detail>
<columnFooter>
    <band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
    <band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
    <band height="42" splitType="Stretch"/>
</summary>

我试图搜索论坛但找不到解决方案。

请咨询

1 个答案:

答案 0 :(得分:2)

此问题的原因是 JR 引擎迭代表两次 - 首次使用主数据源(报告的数据源)和 详细信息 波段第二次使用 Table 组件的数据源。

  • 您可以删除主数据源,并将 Table 组件放入 摘要 频段,例如。

在这种情况下,您的模板将如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport .. whenNoDataType="AllSectionsNoDetail" ..>
    <style name="table">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="New Dataset 1" uuid="820ac89b-615e-453d-9593-9deeb8c44a87">
        <queryString language="SQL">
            <![CDATA[select name, author from address]]>
        </queryString>
        <field name="name" class="java.lang.String"/>
        <field name="author" class="java.lang.String"/>
    </subDataset>
    <title>
        <band height="39" splitType="Stretch"/>
    </title>
    <detail>
        <band height="125" splitType="Stretch"/>
    </detail>
    <summary>
        <band height="63" splitType="Stretch">
            <componentElement>
                <reportElement uuid="4f76c19e-e304-45b3-a521-b30aa4ecb03e" key="table 1" style="table 1" x="131" y="0" width="360" height="50"/>
                <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="New Dataset 1" uuid="9f479053-64b3-49a7-a986-2415ac20385f">
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                    <jr:column uuid="5b10c367-6de0-4799-8408-ad8c7dadd888" width="90">
                        <jr:columnHeader style="table 1_CH" height="30">
                            <staticText>
                                <reportElement uuid="7470c697-d500-46e0-88c1-d42883576ef1" x="0" y="0" width="90" height="30"/>
                                <textElement/>
                                <text><![CDATA[name]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table 1_TD" height="20">
                            <textField>
                                <reportElement uuid="32a30162-dca0-4bda-8ef2-ae8f41d4340a" x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column uuid="3f6fac9d-5a87-4fee-ac81-51170b6d7481" width="90">
                        <jr:columnHeader style="table 1_CH" height="30">
                            <staticText>
                                <reportElement uuid="21335524-5b94-464b-83fc-36d7734596e4" x="0" y="0" width="90" height="30"/>
                                <textElement/>
                                <text><![CDATA[author]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table 1_TD" height="20">
                            <textField>
                                <reportElement uuid="9765da67-b578-4bc3-b0f4-5878c64b9e66" x="0" y="0" width="90" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{author}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </summary>
</jasperReport>

在这种情况下,不要忘记将 whenNoDataType 报告的属性设置为 AllSectionsNoDetail - 以避免隐藏 摘要< / em> 乐队。


  • 另一种方法 - 您可以删除 Table 组件并使用 详细信息 频段。

在这种情况下,模板将如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
    <style name="table">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <queryString language="SQL">
        <![CDATA[select name, author from address]]>
    </queryString>
    <field name="name" class="java.lang.String"/>
    <field name="author" class="java.lang.String"/>
    <title>
        <band height="39" splitType="Stretch"/>
    </title>
    <columnHeader>
        <band height="30">
            <staticText>
                <reportElement uuid="21335524-5b94-464b-83fc-36d7734596e4" style="table 1_CH" x="90" y="0" width="90" height="30"/>
                <textElement/>
                <text><![CDATA[author]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="7470c697-d500-46e0-88c1-d42883576ef1" style="table 1_CH" x="0" y="0" width="90" height="30"/>
                <textElement/>
                <text><![CDATA[name]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="9765da67-b578-4bc3-b0f4-5878c64b9e66" style="table 1_TD" x="90" y="0" width="90" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="32a30162-dca0-4bda-8ef2-ae8f41d4340a" style="table 1_TD" x="0" y="0" width="90" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{author}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>