Jasper Reports: How to conditionally set the textbox style? - jasper-reports

Is it possible in Jasper Reports to conditionally set a textbox style? If yes, how?
Please note that I'm aware of conditional styles, but I do not need a style which varies on a condition, but set the proper style using a different condition for each textbox (of course I could create a conditional style for each textbox, but that would be a real PITA...).
I'm using Jasper Reports 3.7.6 and the Jasper Studio Eclipse plugin.
Thanks
Use case example pseudocode:
bean1 {
f1
f2
}
bean2 {
cond1
cond2
}
<textbox1 style="(bean2.cond1 ? style1 : style2)">
bean1.f1
</textbox1>
<textbox2 style="(bean2.cond2 ? style1 : style2)">
bean1.f2
</textbox2>

Unfortunately you can't define a generic style. See page 135 of the iReport Ultimate Guide:
http://community.jaspersoft.com/documentation/ireport-ultimate-guide:
Please note that the conditions cannot be generic,
for instance, you cannot set a condition like “if the number is positive” or “if the string is
null
.” You must be very specific,
specifying, for example, that a
particular value (field, parameter, variable or
any expression involving them) must be positive
or
null, and so on.

Answering myself: it turns out that it is not possible to set conditional style the way I needed. I ended up with duplicating each text fields (a copy for each style), then setting the visibility upon the condition. Boring and time consuming, but it works.

Related

Kentico 9 macro for form field visibility

I have a custom page type, and the editor will have the option to enter the following
Image (from media library)
Video (from media library)
YouTube video ID
The field names are as follows
SlideImage
SlideVideo
YouTubeVideoID
So, if an editor ads a SlideImage, SlideVideo and YouTubeVideoID should not be usable. Same for SlideVideo and YouTubeVideoID.
Within the Visibility Condition fields, i'm going to assume a macro is needed for this. My logic is:
This field visible if Field A or B have data.
A possible approach can be to add an additional field, which determines the field that should be used.
Create a text field (let's say, SlideType) and use a radio button form control with your options:
image;Image
video;Video
youtube;YouTube
Tick the "Has depending fields" checkbox for this field, and tick the "Depends on another field" checkbox for the SlideImage, SlideVideo and YouTubeVideoID fields.
Your visibility conditions would then be simplified, instead of checking the values of multiple fields.
For example, the visibility condition for the SlideVideo field would be:
SlideType == "video"
This has a few benefits:
Easy to add new fields and configure the visibility conditions
Easy to check what needs to be rendered in the front-end - in your repeaters and other webparts, you can simply have conditional statements on the SlideType field to determine which field to use
Intuitive for the end user - the interface makes it clear which field is being used
Add this to Visibility condition in Page type field edit:
Fields.SlideImage.Value == String.Empty
Do not forget to set proper Has depending fields and Depends on another field properties depending on your needs. You can learn more about these properties here.
Let's say the column name on which this value of your depending field is "FirstName", so you can write in the dependent field -> Visibility Condition as
FirstName.value != ""
or
FirstName.value
You can twist the conditions for as many conditions as possible and can club more than one condition too.
I am also sharing links with you having a lot of examples from Kentico support
Dependency fields in Kentico
Using dependency fields in forms
Cheers,
Chetan

Override language specific pattern in iReport

How can I override the language specific pattern in iReport? I have set the pattern #,##0.00 on a field with a Double value.
If the report is in english, I get the following example output.:
10,000.00
If the report is in german, I get the following example output.:
10.000,00
I need the output 10.000,00 for the english and the german report.
How can I realize that?
Do not set any pattern (make sure to remove it). Instead, in the TextFieldExpression field use
new java.text.DecimalFormat("#,##0.00", new java.text.DecimalFormatSymbols(java.util.Locale.GERMANY)).format($P{parameter1}).
(This is a string. If your textfield expression class is double, you can parse the result.)
This will format the number as in German locale for all languages.
The latest versions of iReport has a feature called Pattern Expression. In that you only need to specify a pattern string, and leave the rest of the field calculation the same. This way you can separate the data from the format, I think it keeps things cleaner, but it also gives you more control over the format at runtime.
Edit. An example was asked for, here it is:
https://gist.github.com/ilopez/9369809#file-so22141769-jrxml
The magic happens in this expression:
new DecimalFormat().toPattern()
You can specify report locale via the REPORT_LOCALE parameter see: Setting REPORT_LOCALE in IReport?

iReport: localized default values for parameters

How to use localized default values for parameters in iReport?
Using a $R{message.key} as the parameter default value has negative consequences in the "Read fields" functionality of iReport's SQL editor. More precisely, the following error is shown (after pressing the Read fields button in Report query dialog):
Sourced file: inline evaluation of: ``$R{message.key}'' : Attempt to access property on undefined variable or class name
Any way around this iReport problem?
Here is the image to illustrate the problem:
I think this is a iReport's bug.
You can temporary comment the defaultValueExpression expression and add the fields via the Report query -> Read fields button.
Or you can manually add the fields declaration.
This is iReport bug ! To solve this issue please follow the below steps.
Open XML File
Remove defaultValueExpression tag value.

Suppressing (Hiding) data when exporting in Crystal Reports 2011

I am using the Crystal Reports 2011 designer just to test it out. I need to be able to hide certain components, e.g. a sub-report, when exporting to different formats like Excel or .pdf. I'm guessing this can be achieved through the Suppress field with a formula, but I can't find the right syntax. Needs to be something like this:
If ExportFormat="Excel" then Suppress
Else if ExportFormat="PDF" then Show
It doesn't seem like there is a variable to represent the ExportFormat type when using expressions in Crystal Reports.
It is possible to overcome this problem by setting a parameter programmatically during an export event i.e. create a parameter #ExportFormat and during the export event set this parameter depending on the type of export e.g. if the export taking place is to Excel, set #ExportFormat="Excel". Then the following expression can be used to suppress a control:
{?#ExportFormat}='Excel'
You got it almost completely right:
If ExportFormat='Excel' then TRUE
Else if ExportFormat='PDF' then FALSE
This formula goes into the suppress formula. (You need to click this small button with the "x-2" and pencil on it...)

JasperReports: default value instead of 'null'

Is there any way to set a default value to a field in a report? I have a lot of String fields in a report and would like them to display "0,00" when they're null.
Supposing the field name is "value", in the "Text Field Expression", write:
($F{value} != null) ? $F{value} : "0.00"
You can also select "Blank when null" in the properties of the text field if you want that. Other options are more flexible but this does the trick very quick and easy.
medopal's answer is good, but 2 additions:
1) You can make the syntax shorter:
($F{field_name}) ? $F{field_name} : "0.00"
2) Make sure your "else" data is of the same class as the field's value, otherwise you'll get errors when it tries to coerce numbers into string, etc. etc. This was something that, as I started out, I mixed up.
Did you try set a pattern in the text field?
If you are using iReport this can be found in the properties for the text field in the Text Field Properties section.
Try something along the lines of ###0.00 to represent 1234.56, which would always display 0.00 even if it is null.
This is the easiest way is to use Coalesce() or NVL() function of database in your data-source query to restrict null data on your report.
But it depends on if you are allowed to change the datasource query or not. If not then you can go for other solutions provided in previous answers.