Set background color in textfield from condition u

2020-04-14 11:54发布

问题:

I have a problem with conditionalStyle in iReport. I have the following textFields (${nameField} = field value):

${field1}=30      ${field2}=40      ${field3}=50
${field4}=23      ${field5}=1       ${field6}=45
${field7}=34       ${field8}=20     ${field9}=0

I need set difference background in every textField depend of the value of the field. I create a new Style and set the conditions:

<style name="ColoredField">
   <conditionalStyle>
     <conditionExpression><![CDATA[$F{field1} == 300]]></conditionExpression>
    <style mode="Opaque" backcolor="#FCFF00"/>
    </conditionalStyle>
</style>

As you can see, the Style use only $F{field1}, and I need make a dynamic style (or something), that apply to every textField.

回答1:

Your question is not clear, can you be more explicit ?

  1. From my understanding , each value will mean a specific background for a textfield, in that case your conditional style value should depend on variable. Maybe using Variables in your style can suit your need.

  2. Add multiple conditional style under each style as below:

each textField can now have different background :

<style name="ColoredField">
   <conditionalStyle>
     <conditionExpression><![CDATA[$F{field1} == 100]]></conditionExpression>
    <style mode="Opaque" backcolor="#FCFFFF"/>
    </conditionalStyle>
    <conditionalStyle>
     <conditionExpression><![CDATA[$F{fiCeld1} == 200]]></conditionExpression>
    <style mode="Opaque" backcolor="#00FF00"/>
    </conditionalStyle>
    <conditionalStyle>
     <conditionExpression><![CDATA[$F{field1} == 300]]></conditionExpression>
    <style mode="Opaque" backcolor="#FCFF00"/>
    </conditionalStyle>
</style>


回答2:

I could not find a direct way to accomplish this task - you have to create a separate style for each field.

Considering this you can concentrate your effort on creating a small script that replicate the XML with the same stile for the fields you have. Then you can just copy/paste the XML into the report source file. Each time you need to change the style you will have to go back to your XML generator.



回答3:

Try ths one :

<style name="myStyle" fontName="Arial">
    <conditionalStyle>
        <conditionExpression><![CDATA[new Boolean($V{SANCTION_AMOUNT_MEASURE}.intValue() == 100)]]></conditionExpression>
        <style forecolor="#FF0000" isBold="true"/>
    </conditionalStyle>
    <conditionalStyle>
        <conditionExpression><![CDATA[new Boolean($V{SANCTION_AMOUNT_MEASURE}.intValue() == 200)]]></conditionExpression>
        <style forecolor="#00FF00" isBold="true"/>
    </conditionalStyle>
    <conditionalStyle>
        <conditionExpression><![CDATA[new Boolean($V{SANCTION_AMOUNT_MEASURE}.intValue() == 300)]]></conditionExpression>
        <style forecolor="#0000FF" isBold="true"/>
    </conditionalStyle>
</style>