如何拉伸子报表以适合父级高度?

问题描述 投票:0回答:1

我刚刚开始使用 JasperReports,我偶然发现了一个我似乎找不到解决方案的问题。

我有一个包含子报告的报告,用于创建一个包含 2 列的表格。我需要拉伸子报表中的文本字段以适合第一列的高度。

基本上这就是我得到的

这就是我想要的(我使用静态高度来实现它)

这是报告的代码

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.0.final using JasperReports Library version 6.0.0  -->
<!-- 2023-08-21T21:38:25 -->
<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="monitoringBulletinRainSensors" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="dc5d5b62-3121-47f6-bc9c-a0c86a6de260">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="test"/>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["C:\\Users\\<user>\\JaspersoftWorkspace\\MyReports\\"]]></defaultValueExpression>
    </parameter>
    <queryString language="json">
        <![CDATA[]]>
    </queryString>
    <field name="text" class="java.lang.String"/>
    <detail>
        <band height="10" splitType="Prevent">
            <property name="local_mesure_unitheight" value="pixel"/>
            <property name="com.jaspersoft.studio.unit.height" value="px"/>
            <textField isStretchWithOverflow="true">
                <reportElement key="" stretchType="RelativeToBandHeight" x="0" y="0" width="50" height="10" uuid="17091a86-28eb-4f4b-814e-2049fc13b754">
                    <property name="com.jaspersoft.studio.unit.height" value="px"/>
                    <property name="local_mesure_unitwidth" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.width" value="px"/>
                </reportElement>
                <box>
                    <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font size="16"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{text}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement stretchType="RelativeToTallestObject" x="50" y="0" width="100" height="10" isPrintInFirstWholeBand="true" backcolor="#FFFFFF" uuid="d8d61ef2-ee12-437a-be7d-9514dc16438d">
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    <property name="local_mesure_unitheight" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.height" value="px"/>
                    <property name="local_mesure_unitwidth" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.width" value="px"/>
                </reportElement>
                <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("children")]]></dataSourceExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "test_child.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

这是子报表的代码

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.0.final using JasperReports Library version 6.0.0  -->
<!-- 2023-08-21T21:38:06 -->
<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="test_child" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="6ee7b94c-7f06-43f8-a02a-461787c5be45">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <property name="com.jaspersoft.studio.unit." value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="text" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="30" splitType="Prevent">
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" stretchType="RelativeToBandHeight" x="0" y="0" width="160" height="30" uuid="bcfd27d0-fc93-42bd-82fb-74a8d95ed706">
                    <property name="local_mesure_unitheight" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.height" value="px"/>
                    <property name="local_mesure_unity" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.y" value="px"/>
                </reportElement>
                <box>
                    <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font size="16"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{text}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

最后这是我正在使用的 JSON 数据源

{
    "text": "This is a very very very very very very very long text",
    "children": [
        {
            "text": "This is a short text"
        },
        {
            "text": "This is a short text"
        },
        {
            "text": "This is a short text"
        }
    ]
}

请帮助我!我真的对任何解决方案都很满意,即使是最糟糕的解决方案,只要它有效:) 谢谢!

jasper-reports
1个回答
0
投票

这里可以使用(listtable)元素。并在其中(listtable)使用另一个(listtable)。

根据您的 JSON 结构,您可以做到这一点。

这里我提到了列表,但你也可以使用表格。

  1. 首先创建一个报告,无需添加任何字段或查询。
  2. 创建一个 dataset1,其中有两个字段 textchildren
  3. 创建dataset2,并在查询部分提及children。他们只有一个text文本文件,即儿童文本数据
  4. 使用 dataset1 添加一个 list,并在该列表上添加 text 文本字段,并在右侧添加另一个 list 用于显示 children text 数据
  5. 在第二个列表上使用 dataset2 并在该列表上添加子项 text 文本字段
  6. 然后根据您的要求选择位置类型和拉伸类型。

示例输出: Sample Output

这是报告的代码

    <?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="c" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c9ca78e9-09e3-4234-bcd8-8cc5941a495b">
    <subDataset name="Dataset1" uuid="6a2c5d32-30c9-4945-9ee6-c2ef7b27f2c8">
        <queryString language="json">
            <![CDATA[]]>
        </queryString>
        <field name="text" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="text"/>
            <fieldDescription><![CDATA[text]]></fieldDescription>
        </field>
        <field name="children" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="children"/>
            <fieldDescription><![CDATA[children]]></fieldDescription>
        </field>
    </subDataset>
    <subDataset name="Dataset2" uuid="e938c415-ac02-43c5-a218-dcfcf7d80232">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="c"/>
        <queryString language="json">
            <![CDATA[children]]>
        </queryString>
        <field name="text" class="java.lang.String">
            <property name="net.sf.jasperreports.json.field.expression" value="text"/>
            <fieldDescription><![CDATA[text]]></fieldDescription>
        </field>
    </subDataset>
    <queryString language="jsonql">
        <![CDATA[]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="20" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="21" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="19" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="30" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="0" width="440" height="30" uuid="92f28b3e-ddc6-48c6-8aff-e44bf28feab0">
                    <property name="com.jaspersoft.studio.unit.CONTENTS.height" value="px"/>
                </reportElement>
                <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="Dataset1" uuid="dd4e597b-4228-41ba-b0d0-72fdbf85a82e">
                        <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource()]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="33" width="440">
                        <textField>
                            <reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="240" height="30" uuid="ca2fbeb2-c057-4b6c-afb0-80f5e87e4530"/>
                            <box>
                                <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                            </box>
                            <textElement verticalAlignment="Middle"/>
                            <textFieldExpression><![CDATA[$F{text}]]></textFieldExpression>
                        </textField>
                        <componentElement>
                            <reportElement stretchType="RelativeToTallestObject" x="240" y="0" width="200" height="30" uuid="274af469-c856-477e-8687-abcbe6008b77"/>
                            <jr:list printOrder="Vertical">
                                <datasetRun subDataset="Dataset2" uuid="512eb811-336f-401c-ac81-b08e05fa7017">
                                    <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("children")]]></dataSourceExpression>
                                </datasetRun>
                                <jr:listContents height="30" width="200">
                                    <textField>
                                        <reportElement x="0" y="0" width="200" height="30" uuid="3c04f2b1-3287-4a18-afa6-b735af62000a"/>
                                        <box>
                                            <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                            <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                            <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                            <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                        </box>
                                        <textElement verticalAlignment="Middle"/>
                                        <textFieldExpression><![CDATA[$F{text}]]></textFieldExpression>
                                    </textField>
                                </jr:listContents>
                            </jr:list>
                        </componentElement>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </band>
    </detail>
    <columnFooter>
        <band height="17" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="22" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="14" splitType="Stretch"/>
    </summary>
</jasperReport>

最后这是我正在使用的JSON数据源

[{
    "text": "This is a very very very very very very very long text",
    "children": [
        {
            "text": "This is a short text"
        },
        {
            "text": "This is a short text"
        },
        {
            "text": "This is a short text"
        }
    ]
},
{
    "text": "This is a Second very very very very very very very long text",
    "children": [
        {
            "text": "This is a second short text"
        },
        {
            "text": "This is a second short text"
        },
        {
            "text": "This is a second short text"
        }
    ]
}]
© www.soinside.com 2019 - 2024. All rights reserved.