Search code examples
jasper-reports

How to set the style condtions in jasper for the tables in the sub dataset


I want my table contents to be colored depending on the conditions. My table is present in the sub datasource and the fields that are populated are from the same. Now I want to color the contents depending on some conditions.

NOTE: all the data present in the fields are of type string. So I started creating the a style and adding a condition to it using the parameter which I thought of passing from java at runtime, It says an error parameter not found. So i tried using Fields in the main report by adding the same fields of sub datasource to the main report, its says retrieving data to the field failed. How am I suppose to check for the conditions in the style.

Here's my code

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2016-01-13T14:56:39 -->
<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="360FeedbackJasper" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b2b320e6-05ba-442a-ae37-11e094fd08f1">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="360feedbackAdapter"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="DM_STYLE">
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{dmScore}.equals("12")]]></conditionExpression>
            <style mode="Opaque" forecolor="#292323" backcolor="#D91511"/>
        </conditionalStyle>
    </style>
    <subDataset name="table1dataset" uuid="d9e6ff71-23d5-4eb7-b8ef-fcd1bc653360">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="360feedbackAdapter"/>
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="dmScore" class="java.lang.String">
            <fieldDescription><![CDATA[dmScore]]></fieldDescription>
        </field>
        <field name="shScore" class="java.lang.String">
            <fieldDescription><![CDATA[shScore]]></fieldDescription>
        </field>
        <field name="selfScore" class="java.lang.String">
            <fieldDescription><![CDATA[selfScore]]></fieldDescription>
        </field>
        <field name="maxScore" class="java.lang.String">
            <fieldDescription><![CDATA[maxScore]]></fieldDescription>
        </field>
        <field name="avgScore" class="java.lang.String">
            <fieldDescription><![CDATA[avgScore]]></fieldDescription>
        </field>
        <field name="peScore" class="java.lang.String">
            <fieldDescription><![CDATA[peScore]]></fieldDescription>
        </field>
        <field name="mergeScore" class="java.lang.String">
            <fieldDescription><![CDATA[mergeScore]]></fieldDescription>
        </field>
        <field name="drScore" class="java.lang.String">
            <fieldDescription><![CDATA[drScore]]></fieldDescription>
        </field>
        <field name="driver" class="java.lang.String">
            <fieldDescription><![CDATA[driver]]></fieldDescription>
        </field>
        <field name="ques" class="java.lang.String">
            <fieldDescription><![CDATA[ques]]></fieldDescription>
        </field>
    </subDataset>
    <parameter name="OTHERS" class="java.lang.String" isForPrompting="false"/>
    <parameter name="OTHERS_VALUE" class="java.lang.String" isForPrompting="false"/>
    <parameter name="SHOWDM" class="java.lang.Boolean" isForPrompting="false">
        <defaultValueExpression><![CDATA[new Boolean(false)]]></defaultValueExpression>
    </parameter>
    <parameter name="SHOWDR" class="java.lang.Boolean" isForPrompting="false">
        <defaultValueExpression><![CDATA[new Boolean(false)]]></defaultValueExpression>
    </parameter>
    <parameter name="SHOWPE" class="java.lang.Boolean" isForPrompting="false">
        <defaultValueExpression><![CDATA[new Boolean(false)]]></defaultValueExpression>
    </parameter>
    <parameter name="SHOWSH" class="java.lang.Boolean" isForPrompting="false">
        <defaultValueExpression><![CDATA[new Boolean(false)]]></defaultValueExpression>
    </parameter>
    <parameter name="SHOWOT" class="java.lang.Boolean" isForPrompting="false">
        <defaultValueExpression><![CDATA[new Boolean(false)]]></defaultValueExpression>
    </parameter>
    <parameter name="SHOWAVG" class="java.lang.Boolean" isForPrompting="false">
        <defaultValueExpression><![CDATA[new Boolean(false)]]></defaultValueExpression>
    </parameter>
    <parameter name="DM_STYLE_PARAM" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["RED"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="overAllSummaryReportWithOthers" class="java.util.List">
        <fieldDescription><![CDATA[overAllSummaryReportWithOthers]]></fieldDescription>
    </field>
    <field name="dmScore" class="java.lang.String">
        <fieldDescription><![CDATA[dmScore]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
.
.
.
.

    <band height="261">
            <componentElement>
                <reportElement style="DM_STYLE" x="4" y="130" width="554" height="80" uuid="a9101195-3ec3-4fcd-acdd-f4f0665b9c5b">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                </reportElement>
                <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="table1dataset" uuid="360bff8e-005d-461c-a00f-2c990a894735">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{overAllSummaryReportWithOthers})]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="154" uuid="0b4b40f3-54c6-462a-90a4-d8f32a62be93">
                        <jr:columnHeader style="Table_CH" height="50">
                            <staticText>
                                <reportElement mode="Opaque" x="0" y="0" width="154" height="50" forecolor="#000000" backcolor="#E0E0E0" uuid="a3280d8e-c3ff-4972-91eb-6e8da7f866ee"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle">
                                    <font fontName="SansSerif" isBold="true"/>
                                </textElement>
                                <text><![CDATA[driver]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="154" height="30" uuid="12ab2c78-afbb-4db5-9a01-16b28cf3d9fa"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$F{driver}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="50" uuid="8d57520d-e982-430a-afd3-4121f4bd4600">
                        <jr:columnHeader style="Table_CH" height="50">
                            <staticText>
                                <reportElement mode="Opaque" x="0" y="0" width="50" height="50" forecolor="#000000" backcolor="#E0E0E0" uuid="d7a9ad1e-09b5-4791-8bd4-ca37e904fd51"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle">
                                    <font fontName="SansSerif" isBold="true"/>
                                </textElement>
                                <text><![CDATA[maxScore]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="50" height="30" uuid="d339341c-b0ca-4482-b0ca-651be075fe60"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$F{maxScore}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="50" uuid="1a391443-bc9c-4be0-86cb-a3b8dec388f4">
                        <jr:columnHeader style="Table_CH" height="50">
                            <staticText>
                                <reportElement mode="Opaque" x="0" y="0" width="50" height="50" forecolor="#000000" backcolor="#E0E0E0" uuid="9c73ca1a-1521-48aa-b905-2ecfeb075cbe"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle">
                                    <font fontName="SansSerif" isBold="true"/>
                                </textElement>
                                <text><![CDATA[selfScore]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="50" height="30" uuid="eced711e-cca2-4fd3-9d2c-46dd4787fe7d"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$F{selfScore}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="50" uuid="269e656a-93bf-4f52-bcd1-b3e1c1a6436e">
                        <printWhenExpression><![CDATA[new Boolean($P{SHOWDM})]]></printWhenExpression>
                        <jr:columnHeader style="Table_CH" height="50">
                            <staticText>
                                <reportElement mode="Opaque" x="0" y="0" width="50" height="50" forecolor="#000000" backcolor="#E0E0E0" uuid="26a14484-4368-4cce-b6cf-c9a4280abe0c"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle">
                                    <font fontName="SansSerif" isBold="true"/>
                                </textElement>
                                <text><![CDATA[dmScore]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement style="DM_STYLE" x="0" y="0" width="50" height="30" uuid="d43d838c-4eb7-4dd0-8afb-d7a4590dabff"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$F{dmScore}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="50" uuid="347a38dd-a69f-446a-92ab-3ed4502cbad6">
                        <printWhenExpression><![CDATA[new Boolean($P{SHOWDR})]]></printWhenExpression>
                        <jr:columnHeader style="Table_CH" height="50">
                            <staticText>
                                <reportElement mode="Opaque" x="0" y="0" width="50" height="50" forecolor="#000000" backcolor="#E0E0E0" uuid="ef3a228b-47f3-461c-a2c8-88c16eca5049"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle">
                                    <font fontName="SansSerif" isBold="true"/>
                                </textElement>
                                <text><![CDATA[drScore]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="50" height="30" uuid="a834e7b5-7413-442b-9f8e-aee67590bede"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$F{drScore}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="50" uuid="20c307cd-1e54-4a42-8cc4-1540da53cc57">
                        <printWhenExpression><![CDATA[new Boolean($P{SHOWPE})]]></printWhenExpression>
                        <jr:columnHeader style="Table_CH" height="50">
                            <staticText>
                                <reportElement mode="Opaque" x="0" y="0" width="50" height="50" forecolor="#000000" backcolor="#E0E0E0" uuid="a45e2224-b314-4519-aac4-e1e14500071e"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle">
                                    <font fontName="SansSerif" isBold="true"/>
                                </textElement>
                                <text><![CDATA[peScore]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="50" height="30" uuid="c0d60f60-20b6-414f-9c5e-9b5033386d5f"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$F{peScore}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="50" uuid="0e710e32-ff08-4ce4-96c3-b505e3ecb3ed">
                        <printWhenExpression><![CDATA[new Boolean($P{SHOWSH})]]></printWhenExpression>
                        <jr:columnHeader style="Table_CH" height="50">
                            <staticText>
                                <reportElement mode="Opaque" x="0" y="0" width="50" height="50" forecolor="#000000" backcolor="#E0E0E0" uuid="6eb72492-9b2a-4822-b306-903c220c40c1"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle">
                                    <font fontName="SansSerif" isBold="true"/>
                                </textElement>
                                <text><![CDATA[shScore]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="50" height="30" uuid="dff321ae-a25f-4224-94a6-096bcc232ce1"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$F{shScore}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="50" uuid="fb2ee2a9-3156-42a1-95ee-aa35fe630fc5">
                        <printWhenExpression><![CDATA[new Boolean($P{SHOWOT})]]></printWhenExpression>
                        <jr:columnHeader style="Table_CH" height="50">
                            <staticText>
                                <reportElement mode="Opaque" x="0" y="0" width="50" height="50" forecolor="#000000" backcolor="#E0E0E0" uuid="48bee6a2-8f4b-4280-9fa0-24a2dad19c06"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle">
                                    <font fontName="SansSerif" isBold="true"/>
                                </textElement>
                                <text><![CDATA[mergeScore]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="50" height="30" uuid="72b1c817-d6be-470d-8243-514e77b67d26"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$F{mergeScore}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="50" uuid="f292fb3e-0f95-4e24-8d27-763d15f390af">
                        <printWhenExpression><![CDATA[new Boolean($P{SHOWAVG})]]></printWhenExpression>
                        <jr:columnHeader style="Table_CH" height="50">
                            <staticText>
                                <reportElement mode="Opaque" x="0" y="0" width="50" height="50" forecolor="#000000" backcolor="#E0E0E0" uuid="027dbe79-5e7c-47ec-a658-5c4a1bd53699"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle">
                                    <font fontName="SansSerif" isBold="true"/>
                                </textElement>
                                <text><![CDATA[avgScore]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="50" height="30" uuid="c7ac4bbb-1543-4d48-a524-0dc75fc0e6b5"/>
                                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                                <textFieldExpression><![CDATA[$F{avgScore}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
            <staticText>
                <reportElement x="-1" y="50" width="225" height="20" uuid="5c083db9-dcc4-48e4-b72d-764490baaf03"/>
                <textElement>
                    <font fontName="SansSerif"/>
                </textElement>
                <text><![CDATA[Detailed Bahavior-wise Report]]></text>
            </staticText>
            <textField>
                <reportElement x="12" y="91" width="100" height="30" uuid="30a8aeab-8307-42e6-b375-da90350b6790"/>
                <textFieldExpression><![CDATA[$P{DM_STYLE_PARAM}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

Solution

  • If you define a conditional expression on a style, you can only use that style in its scope.

    In your example:

    <conditionExpression><![CDATA[$F{dmScore}.equals("12")]]></conditionExpression>
    

    The scope of $F{dmScore} is when you are running the subDataset="table1dataset", hence inside of the <jr:table> tag.

    This style should only be used inside of this tag, scope.

    Solution: Remove all style declaration of DM_STYLE that are out of scope as for example.

    <reportElement style="DM_STYLE" x="4" y="130" width="554" height="80" uuid="a9101195-3ec3-4fcd-acdd-f4f0665b9c5b">
       <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
    </reportElement> 
    

    Regarding your previous problem when using parameter you probably forgot to pass this parameter to the jr:table and since you where using it inside you had same problem but reversed. The solution of this would have been to create same parameter in subdataset and then pass it.

    <datasetRun subDataset="table1dataset" uuid="93792e42-02d5-4121-84a6-312310e190a0">
        <datasetParameter name="yourParam">
            <datasetParameterExpression><![CDATA[$P{yourParam}]]></datasetParameterExpression>
        </datasetParameter>
        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{overAllSummaryReportWithOthers})]]></dataSourceExpression>
    </datasetRun>