Jump to content

Background color depending on condition - Conditional Styling


shetty.rohit010482

Recommended Posts

Hi thr! I have a question related to highlighting data in my generated PDF from Jasper. I want to highlight field with yellow background color based on certain condition. I googled it and found that we can use conditional styling for this. Something like this.
<style name="alternateStyle" backcolor="#FFFFFF" fontName="Times New Roman">
      <conditionalStyle>
                   <conditionExpression><![CDATA[new Boolean(<Some condition>)]]></conditionExpression>
                   <style mode="Opaque" backcolor="#FFFF33"/>
      </conditionalStyle>
</style>
Now, I want to apply this style on more then 1 item ( propably 100 for me) but the input condition(<some condition>) is different for every field. So wanted to check if I need to create separate style tag for every field or I could reuse same styletag. 
Appreciate your help on this!

Thanks, Rohit

 

Link to comment
Share on other sites

  • Replies 7
  • Created
  • Last Reply

Top Posters In This Topic

Thanks for the reply!

Actually, the input condition to enable the conditional formatting is a List. If specific name is present in the list I need to highlight particular field. Now another question, can I pass fieldName(Just like Java reflection??) to style check in the condition if that name is present. If yes I will hightlight. That way I will have only one style and one conditionalExpression (not sure???)

Appreciate all your help on this!

Link to comment
Share on other sites

So What I have understood is you will have a List (java.util.List) as a parameter. And in the report you have certain fields. If the report field values are there in the list the bg color of the field should change.

For this,

Decalre a Parameter as List of type java.util.List.

Add Style and conditional style to this.

Give the conditional expression as paramList.contains of F{fieldName}.

If this is not your use case please reply. If this has helped you pls check this as Answer :)

Link to comment
Share on other sites

Thanks Ann again for your response.YOU really are giving some very good suggestions.

However, my senerio is little bit different here - Actually my List contains all the field Name whose bg color needs to be changed in the PDF. So lets suppose there are 50 fields I am displaying on the report, based on the list I just need to highlight the one which are present in the List.

 

So what I understood as of now is -

> I will define a style for every field

> include Conditional style, this will have conditional expression as paramList.contains(" ")

> include this style to the field.

 

so I will have as many style present as no of fields, SO EVERY field will have different style. I cannt use multiple conditional tag in same style as the list contains name which I am comparing it against my hardCode Field name.

 

 

But, I wanted a way out if I can limit the style tag definition to one and with only one conditional tag and all the field use that.

Please let me know if the problem statement is still not clear?

Thanks for looking into this!

Link to comment
Share on other sites

I still feel, you need to define only one Style say mainStyle and 50 conditions to this and in condition 1 you will have to write paramList.contains(field1). now set the bg color for that. then add Condition 2 and write paramList.contains(field2), set bg color for that and so on.

Now in all the fields in the report set style as mainStyle. If my answer as helped you please vote :)

Link to comment
Share on other sites

I wanted to avoid writing conditional styles, so while researching I found I can use scriptlets. Its a very powerful feature of Jasper report wherein we can call Java code from Jasper report.

In the function I passed on necessary parameters and the best thing was I can put style tag and hence when jasper report is rendered all the field for which I have called the function gets highlighted based on my business scenario.

I would like to thank you Ann for providing some wonderful suggestion that helped to resolve this!!

 

Link to comment
Share on other sites

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