2016-01-13 45 views
1

我希望根據條件對錶格內容進行着色。我的表存在於子數據源中,並且填充的字段來自相同的。現在我想根據一些條件對內容着色。如何在jasper中爲子數據集中的表設置樣式條件

注意:字段中的所有數據都是字符串類型。所以我開始創建一個樣式並使用我認爲在運行時從java傳遞的參數向它添加條件,它說沒有找到錯誤參數。所以我嘗試在主報告中使用字段,通過將相同的子數據源字段添加到主報告中,它說向數據字段中檢索數據失敗。我想如何檢查風格的條件。

這裏是我的代碼

<?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> 

回答

1

如果定義在一個樣式條件表達式,你只能使用風格,其範圍

在您的例子:

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

$F{dmScore}範圍是當你正在運行的subDataset="table1dataset",因此裏面的<jr:table>標籤。

此樣式只能在此標籤範圍內使用。

解決方案: 刪除超出範圍的所有樣式聲明,例如DM_STYLE。

<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時,關於你以前的問題,因爲你在哪裏使用它裏面你有同樣的問題,但逆轉。解決方法是在subdataset中創建相同的參數,然後傳遞它。

<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> 
+0

如何在subDataset中定義樣式。糾正我,如果我錯了,樣式通常在主要報告中知道,我無法在我的jasperSoft subDataset中找到任何樣式,如何獲取子數據集中的樣式,以便我可以將我的字段用於我的條件檢查。 –

+0

您已經正確定義了樣式(它們只在主報告中),這不是問題...問題是您只能在其上下文中使用樣式! –

+0

通過您傳遞的示例,刪除表標記之外的所有DM_STYLE,它將起作用。 –

相關問題