Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Global Functions - Useful JavaScript Functions (BIRT)"

(Creating Global Functions)
Line 12: Line 12:
 
In order for these functions to be globally accessible the function needs to be added as a global.  The recommended procedure here is to use the reportContext.setPersistentGlobalVariable(name, value) function.  So lets imagine that we want to create a global logging function.  We would want to add the following code to any Script handler that has access to the reportContext.
 
In order for these functions to be globally accessible the function needs to be added as a global.  The recommended procedure here is to use the reportContext.setPersistentGlobalVariable(name, value) function.  So lets imagine that we want to create a global logging function.  We would want to add the following code to any Script handler that has access to the reportContext.
 
  function log ( str ){
 
  function log ( str ){
              Logger.getAnonymousLogger().info(str);
+
    Logger.getAnonymousLogger().info(str);
 
  }
 
  }
 
 
 
 
Line 27: Line 27:
 
=== UCWord (upper case first letter)===
 
=== UCWord (upper case first letter)===
 
Sometimes you want to be able to have just the first letter of word capitalized, at the start of a sentence for example.  This function does that for you. <br>
 
Sometimes you want to be able to have just the first letter of word capitalized, at the start of a sentence for example.  This function does that for you. <br>
<tt><blockquote>
+
function UCWord(str){
function UCWord(str){<br>
+
    // split string
<blockquote>
+
    firstChar = str.substring(0,1);
 
+
    remainChar = str.substring(1);
// split string <br>
+
firstChar = str.substring(0,1);<br>
+
    // convert case
remainChar = str.substring(1);<br>
+
    firstChar = firstChar.toUpperCase();  
<br>
+
    remainChar = remainChar.toLowerCase();
// convert case<br>
+
firstChar = firstChar.toUpperCase();<br>
+
    return firstChar + remainChar<br>
remainChar = remainChar.toLowerCase();<br>
+
}
<br>
+
return firstChar + remainChar<br>
+
    // Make function globally available
</blockquote>
+
    reportContext.setPersistentGlobalVariable("UCWord", UCWord);
}<br>
+
<br>
+
    USAGE<br>
// Make function globally available
+
    UCWord("ALLCAPS");  = gives => Allcaps<br>
reportContext.setPersistentGlobalVariable("UCWord", UCWord);
+
    UCWord("alllower"); = gives => Alllower<br>
 
+
    UCWord("MiXeDuP");  = gives => Mixedup<br>
  
USAGE<br>
 
UCWord("ALLCAPS");  = gives => Allcaps<br>
 
UCWord("alllower"); = gives => Alllower<br>
 
UCWord("MiXeDuP");  = gives => Mixedup<br>
 
</blockquote></tt>
 
  
 
== Comments ==
 
== Comments ==

Revision as of 12:54, 29 March 2007

< To: Report Developer Examples (BIRT)

Global Functions - Useful JavaScript Functions (BIRT) This example does not have a Bugzilla ID all code entered on the page.

Introduction

Basic JavaScript is a good language but there are significant bits of function that are not supported. This page contains examples of useful JavaScript functions that can assist with report development.

BIRT Version Compatibility

This example was built using version 2.2 M5.

Example Files

Rather than enter these examples as functions. Each example is just pasted here in the code.

Creating Global Functions

In order for these functions to be globally accessible the function needs to be added as a global. The recommended procedure here is to use the reportContext.setPersistentGlobalVariable(name, value) function. So lets imagine that we want to create a global logging function. We would want to add the following code to any Script handler that has access to the reportContext.

function log ( str ){
    Logger.getAnonymousLogger().info(str);
}
	
reportContext.setPersistentGlobalVariable("log", log);

From that point forward, any other expression or script can access logging by just calling log("message");

The most logical place to add the creation of your global functions is the initialize method of the Report component (the top component in the outline view). Alternately, you may want to add the function to a template.

The following is the collection of useful global functions.

Functions

UCWord (upper case first letter)

Sometimes you want to be able to have just the first letter of word capitalized, at the start of a sentence for example. This function does that for you.

function UCWord(str){
   // split string
   firstChar = str.substring(0,1);
   remainChar = str.substring(1);

   // convert case
   firstChar = firstChar.toUpperCase(); 
   remainChar = remainChar.toLowerCase();

   return firstChar + remainChar
} // Make function globally available reportContext.setPersistentGlobalVariable("UCWord", UCWord); USAGE
UCWord("ALLCAPS"); = gives => Allcaps
UCWord("alllower"); = gives => Alllower
UCWord("MiXeDuP"); = gives => Mixedup


Comments

Please enter comments below by selecting the edit icon to the right. You will need a Bugzilla account to add comments.

Back to the top