【问题标题】:How to remove frame with empty textFields?如何删除带有空文本字段的框架?
【发布时间】:2016-12-16 05:55:17
【问题描述】:

我有一个框架,我正在使用该框架来显示性别字段。我在该框架中添加了两个textfields,一个用于显示性别标签,另一个用于显示性别字段值。当性别字段为空时,Jasper 正在渲染空行,我想删除这个空行。

<frame>
            <reportElement key="govi" positionType="Float" stretchType="RelativeToTallestObject" x="124" y="154" width="403" height="17" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true" uuid="ff3e33f8-cc51-45c3-a3ed-513832d6e180"/>
            <staticText>
                <reportElement x="0" y="0" width="141" height="15" uuid="9700c67a-9385-4dd5-8626-23a8261bc2b5"/>
                <text><![CDATA[Person with Disability ]]></text>
            </staticText>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="140" y="0" width="245" height="15" isPrintWhenDetailOverflows="true" uuid="69f221ef-ae6b-40c6-98fd-dec7b91a6269"/>
                <textElement textAlignment="Justified">
                    <font size="12" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[($F{is_pd} ? "Yes" : "No")]]></textFieldExpression>
            </textField>
        </frame>
        <frame>
            <reportElement key="genderField" positionType="Float" stretchType="RelativeToTallestObject" x="124" y="114" width="393" height="15" isPrintWhenDetailOverflows="true" uuid="464b5c89-e158-4783-8f21-7b05f567e55e"/>
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="104" height="15" isRemoveLineWhenBlank="true" uuid="0a77a60f-19ef-4634-84df-7a7f7f94ed14"/>
                <textElement>
                    <font size="10" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[($F{country_id} != 1 ? "Gender" : null )]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="140" y="0" width="231" height="15" isRemoveLineWhenBlank="true" uuid="884fe0ef-654c-46e0-8399-d9fed8722a76"/>
                <textElement>
                    <font size="12" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[($F{country_id} != 1 ? $F{gender} : null )]]></textFieldExpression>
            </textField>
        </frame>
        <frame>
            <reportElement key="govi" positionType="Float" stretchType="RelativeToTallestObject" x="124" y="134" width="402" height="17" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true" uuid="a4cbd87c-ea2d-4952-8bc4-0a7e6e655e28"/>
            <staticText>
                <reportElement x="0" y="0" width="69" height="15" uuid="e289c5be-91c4-4a74-b3d6-57c800401d0a"/>
                <text><![CDATA[Category]]></text>
            </staticText>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="140" y="0" width="231" height="15" isRemoveLineWhenBlank="true" uuid="c01465ce-fb3d-47ba-9c29-bdfd7be7424f"/>
                <textElement>
                    <font size="12" isBold="false"/>
                </textElement>
                <textFieldExpression><![CDATA[($F{category_id} == 1 ? "GEN" : ($F{category_id} == 2 ? "OBC" : ($F{category_id}==3 ? "SC" : "ST")))]]></textFieldExpression>
            </textField>
        </frame>

我已经在框架和两个文本字段上应用了isRemoveLineWhenBlank="true"isBlankWhenNull="true" 在两个文本字段上都应用了仍然没有成功。我错过了什么吗?

【问题讨论】:

    标签: jasper-reports


    【解决方案1】:

    解决您的问题很简单 - 您应该为第一个 textField 设置isRemoveLineWhenBlank="true"(使用($F{country_id} != 1 ? "Gender" : null epxression)。在这种情况下,一切都会好起来的。

    <textField isStretchWithOverflow="true" isBlankWhenNull="true">
        <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="104" height="15" isRemoveLineWhenBlank="true"/>
        <textElement>
            <font size="10" isBold="false"/>
        </textElement>
        <textFieldExpression><![CDATA[($f{country_id} != 1 ? "Gender" : null )]]></textFieldExpression>
    </textField>
    

    我修改了你的模板以检查为

    <?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="Hide frame" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <parameter name="country_id" class="java.lang.Integer"/>
        <parameter name="gender" class="java.lang.String"/>
        <title>
            <band height="742">
                <textField>
                    <reportElement positionType="Float" x="0" y="30" width="430" height="15" />
                    <textFieldExpression><![CDATA["First line"]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement positionType="Float" x="0" y="60" width="430" height="15" />
                    <textFieldExpression><![CDATA["Third line"]]></textFieldExpression>
                </textField>
                <frame>
                    <reportElement key="genderField" positionType="Float" stretchType="RelativeToTallestObject" x="0" y="45" width="393" height="15" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true" />
                    <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                        <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="104" height="15" isRemoveLineWhenBlank="true" />
                        <textElement>
                            <font size="10" isBold="false"/>
                        </textElement>
                        <textFieldExpression><![CDATA[($P{country_id} != 1 ? "Gender" : null )]]></textFieldExpression>
                    </textField>
                    <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                        <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="140" y="0" width="231" height="15" isRemoveLineWhenBlank="true" />
                        <textElement>
                            <font size="12" isBold="false"/>
                        </textElement>
                        <textFieldExpression><![CDATA[($P{country_id} != 1 ? $P{gender} : null )]]></textFieldExpression>
                    </textField>
                </frame>
            </band>
        </title>
    </jasperReport>
    

    它就像一个魅力:

    例如,您可以使用以下参数组合获得上述结果:

    • country_id=1
    • gender="M"

    如果使用此值:

    • country_id=3
    • gender="F"

    结果将是:

    你也可以像这个例子那样简化这个片段:

    <?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="Hide frame" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <parameter name="country_id" class="java.lang.Integer"/>
        <parameter name="gender" class="java.lang.String"/>
        <title>
            <band height="742">
                <textField>
                    <reportElement positionType="Float" x="0" y="30" width="430" height="15" />
                    <textFieldExpression><![CDATA["First line"]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement positionType="Float" x="0" y="60" width="430" height="15" />
                    <textFieldExpression><![CDATA["Third line"]]></textFieldExpression>
                </textField>
                <frame>
                    <reportElement positionType="Float" x="0" y="45" width="430" height="15" isRemoveLineWhenBlank="true" >
                        <printWhenExpression><![CDATA[$P{country_id} != 1 && $P{gender} != null]]></printWhenExpression>
                    </reportElement>
                    <textField isBlankWhenNull="true">
                        <reportElement x="0" y="0" width="100" height="15" />
                        <textFieldExpression><![CDATA[$P{country_id} != 1 ? "Gender" : null]]></textFieldExpression>
                    </textField>
                    <textField isBlankWhenNull="true">
                        <reportElement x="100" y="0" width="100" height="15" />
                        <textFieldExpression><![CDATA[$P{country_id} != 1 ? $P{gender} : null]]></textFieldExpression>
                    </textField>
                </frame>
            </band>
        </title>
    </jasperReport>
    

    在此示例中,isRemoveLineWhenBlankprintWhenExpression 仅用于框架元素。

    它的工作原理与此示例相同:

    <?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="Hide frame" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <parameter name="country_id" class="java.lang.Integer"/>
        <parameter name="gender" class="java.lang.String"/>
        <title>
            <band height="742">
                <textField>
                    <reportElement positionType="Float" x="0" y="30" width="430" height="15"/>
                    <textFieldExpression><![CDATA["First line"]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement positionType="Float" x="0" y="60" width="430" height="15"/>
                    <textFieldExpression><![CDATA["Third line"]]></textFieldExpression>
                </textField>
                <frame>
                    <reportElement positionType="Float" x="0" y="45" width="430" height="15" isRemoveLineWhenBlank="true"/>
                    <textField isBlankWhenNull="true">
                        <reportElement x="0" y="0" width="100" height="15" isRemoveLineWhenBlank="true"/>
                        <textFieldExpression><![CDATA[$P{country_id} != 1 ? "Gender" : null]]></textFieldExpression>
                    </textField>
                    <textField isBlankWhenNull="true">
                        <reportElement x="100" y="0" width="100" height="15" isRemoveLineWhenBlank="true"/>
                        <textFieldExpression><![CDATA[$P{country_id} != 1 ? $P{gender} : null]]></textFieldExpression>
                    </textField>
                </frame>
            </band>
        </title>
    </jasperReport>
    

    结果是一样的,例如country_id == 1:

    更新

    您忘记在新的 sn-p 中为带有性别的框架设置 isRemoveLineWhenBlank="true"。进行此修复后,您的新示例在 iReport 5.6.0 中也能正常工作。为了测试目的,我用参数替换了字段,并更改了帧的顺序以隐藏带有性别的帧。

    jrxml 文件:

    <?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="Hide frame" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <parameter name="country_id" class="java.lang.Integer"/>
        <parameter name="gender" class="java.lang.String"/>
        <parameter name="is_pd" class="java.lang.Boolean">
            <defaultValueExpression><![CDATA[true]]></defaultValueExpression>
        </parameter>
        <parameter name="category_id" class="java.lang.Integer">
            <defaultValueExpression><![CDATA[1]]></defaultValueExpression>
        </parameter>
        <title>
            <band height="742">
                <frame>
                    <reportElement key="govi" positionType="Float" stretchType="RelativeToTallestObject" x="124" y="129" width="403" height="17" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true"/>
                    <staticText>
                        <reportElement x="0" y="0" width="141" height="15"/>
                        <text><![CDATA[Person with Disability ]]></text>
                    </staticText>
                    <textField isStretchWithOverflow="true">
                        <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="140" y="0" width="245" height="15" isPrintWhenDetailOverflows="true"/>
                        <textElement textAlignment="Justified">
                            <font size="12" isBold="false"/>
                        </textElement>
                        <textFieldExpression><![CDATA[$P{is_pd} ? "Yes" : "No"]]></textFieldExpression>
                    </textField>
                </frame>
                <frame>
                    <reportElement key="genderField" positionType="Float" stretchType="RelativeToTallestObject" x="124" y="114" width="393" height="15" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true"/>
                    <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                        <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="0" y="0" width="104" height="15" isRemoveLineWhenBlank="true"/>
                        <textElement>
                            <font size="10" isBold="false"/>
                        </textElement>
                        <textFieldExpression><![CDATA[($P{country_id} != 1 ? "Gender" : null )]]></textFieldExpression>
                    </textField>
                    <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                        <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="140" y="0" width="231" height="15" isRemoveLineWhenBlank="true"/>
                        <textElement>
                            <font size="12" isBold="false"/>
                        </textElement>
                        <textFieldExpression><![CDATA[($P{country_id} != 1 ? $P{gender} : null )]]></textFieldExpression>
                    </textField>
                </frame>
                <frame>
                    <reportElement key="govi" positionType="Float" stretchType="RelativeToTallestObject" x="124" y="97" width="402" height="17" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true"/>
                    <staticText>
                        <reportElement x="0" y="0" width="69" height="15"/>
                        <text><![CDATA[Category]]></text>
                    </staticText>
                    <textField isStretchWithOverflow="true">
                        <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="140" y="0" width="231" height="15" isRemoveLineWhenBlank="true"/>
                        <textElement>
                            <font size="12" isBold="false"/>
                        </textElement>
                        <textFieldExpression><![CDATA[($P{category_id} == 1 ? "GEN" : ($P{category_id} == 2 ? "OBC" : ($P{category_id}==3 ? "SC" : "ST")))]]></textFieldExpression>
                    </textField>
                </frame>
            </band>
        </title>
    </jasperReport>
    

    iReport 中的设计如下:

    我这样设置参数:

    • country_id = 1
    • 性别 - 空
    • is_pd - 默认值 (= true)
    • category_id - 默认值 (=1)

    为此参数生成的报告如下所示:

    如果使用这个集合

    • country_id = 2
    • 性别 = "M"
    • is_pd - 默认值 (= true)
    • category_id - 默认值 (=1)

    结果将是:

    【讨论】:

    • 不工作,仍然显示空白行。我已经更新了有问题的代码 sn-p(添加了包装我们元素的元素)。 PS:我使用的是 iReport 5.6.0
    • Not working - 你是什么意思?你在你的版本上检查了我的样本吗?有用吗?
    • 你提供的两个版本都试过了,没有一个是删除空格。
    • 看起来很奇怪。好的,我今晚用 iReport 5.6 检查一下。
    • 所有版本都在 iReport 5.6.0 中运行良好。您是否尝试设置country_id =1 并清空gender
    猜你喜欢
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多