Jump to content
  • Passing variable from subreport to main report for conditional styling


    Jim W
    • Edited on:
    • Features: User Interface Version: v9.0.0 Product: JasperReports® Server

    Problem Description

    If you are trying to pass a variable from a subreport to the main report and try to use it to trigger conditional styling, e.g. border line width.

    It may not work because of evaluation time of the script variable.  If you hard code the local variable, or get a value from a query field, it should work.

    Here is an example that does not work:

        <style name="Style1" isBlankWhenNull="false">
            <box>
                <pen lineWidth="6.25"/>
                <topPen lineWidth="6.25" lineStyle="Solid" lineColor="#000000"/>
                <leftPen lineWidth="6.25" lineStyle="Solid" lineColor="#000000"/>
                <bottomPen lineWidth="6.25" lineStyle="Solid" lineColor="#000000"/>
                <rightPen lineWidth="6.25" lineStyle="Solid" lineColor="#000000"/>
            </box>
            <conditionalStyle>
                <conditionExpression><![CDATA[$V{sxROW_COUNT} == 88]]></conditionExpression>
                <style radius="0" isBlankWhenNull="true">
                    <box>
                        <pen lineWidth="1.25"/>
                        <topPen lineWidth="1.25" lineStyle="Solid" lineColor="#000000"/>
                        <leftPen lineWidth="1.25" lineStyle="Solid" lineColor="#000000"/>
                        <bottomPen lineWidth="1.25" lineStyle="Solid" lineColor="#000000"/>
                        <rightPen lineWidth="1.25" lineStyle="Solid" lineColor="#000000"/>
                    </box>
                </style>
            </conditionalStyle>
        </style>

        <variable name="sxROW_COUNT" class="java.lang.Integer">
            <variableExpression><![CDATA[]]></variableExpression>
        </variable>

    <returnValue subreportVariable="sxROW_COUNT" toVariable="sxROW_COUNT"/>

    Workaround:

    You might want to by pass conditional styling and change the property directly, e.g.

       <style name="Style1" isBlankWhenNull="false">
            <box>
                <pen lineStyle="Solid" lineColor="#000000"/>
            </box>
        </style>

    <textField evaluationTime="Report">
        <reportElement style="Style1" x="0" y="0" width="817" height="10" stretchType="ElementGroupHeight">
            <propertyExpression name="net.sf.jasperreports.style.box.pen.lineWidth">IF(EQUALS($V{sxROW_COUNT}, 88), "1.25", "6.25")</propertyExpression>
        </reportElement>
        <textElement>
            <font size="5"/>
        </textElement>
        <textFieldExpression>""</textFieldExpression>
    </textField>

    Caveat: 

    This workaround may not work for other output formats than PDF.   Furthermore, there may be more complications for reports with multiple pages.

     

     


    User Feedback

    Recommended Comments

    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...