JavaScript Cookbook

Alternating colours

To alternate colours in a series of details, declare a count variable in the report header or the group header, if you are grouping.

// header onRenderBegin
count = 0

In the detail, set the colour based on the modulus of the count and increment count:

// component onRenderBegin
if (count%2==0) setBackgroundColor("Yellow")
else setBackgroundColour("White");
++count;

Hiding and showing components

If you have several components to hide based on a value, declare a boolean that you can check in renderIf:

// header onRenderBegin
today = new Date();
weekend = today.getDay()==0 || today.getDay()==6;

Now you can check for a weekend (Sunday==0, Saturday==6) and render accordingly:

// component renderIf
weekend

or for weekdays, use not weekend:

// component renderIf
!weekend

Using parameters to dynamically set values

Dynamic parameters can be passed in to a report at the start of rendering. These can be queried using the Parameters object. For example, to indicate whether a report is a Customer copy or a Sales copy you could specify the label to read a dynamic parameter called "type":

// label onRenderBegin
setText(Parameters.get("type"));

Other parts of the code could show or hide components based on the value of type:

// component renderIf
Parameters.get("type")=="Sales"

Accessing Java classes

Standard Java classes can be accessed by JavaScript. For example:

java.lang.System.out.println("Hello World");

The import mechanism can be used to avoid typing lengthy package names:

importClass(java.util.Vector);
v = new Vector();
importPackage(java.lang);
System.out.print("Hello ");
System.out.println("World");

When importing packages and classes which are not in the standard java runtime, the prefix Packages is required:

importClass(Packages.org.apache.log4j.Logger);
importPackage(Packages.org.apache.log4j);

Typically imports should be done in the FunctionDefinitions script so that they are accessible throughout the report. Any class that is available on the Elixir Report Designer classpath can be loaded and used in this way.