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

From Eclipsepedia

Jump to: navigation, search
(UCFirst (upper case first letter))
 
(12 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[Examples (BIRT)]]
+
{{Backlink|Report Developer Examples (BIRT)}}
  
 
Global Functions - Useful JavaScript Functions (BIRT)
 
Global Functions - Useful JavaScript Functions (BIRT)
Line 11: Line 11:
 
== Creating Global Functions==
 
== 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.
 
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.
<tt><blockquote>
+
function log ( str ){
function log ( str ){
+
    Logger.getAnonymousLogger().info(str);
: Logger.getAnonymousLogger().info(str);
+
}
}
+
<br>
+
reportContext.setPersistentGlobalVariable("log", log);
reportContext.setPersistentGlobalVariable("log", log);
+
 
</blockquote></tt>
+
 
From that point forward, any other expression or script can access logging by just calling log("message");
 
From that point forward, any other expression or script can access logging by just calling log("message");
  
Line 23: Line 22:
  
 
The following is the collection of useful global functions.
 
The following is the collection of useful global functions.
 +
 
== Functions==
 
== Functions==
  
=== UCWord (upper case first letter)===
+
=== UCFirst (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 UCFirst(str){
function UCWord(str){<br>
+
    // split string
<blockquote>
+
    firstChar = str.substring(0,1);
 +
    remainChar = str.substring(1);
 +
 +
    // convert case
 +
    firstChar = firstChar.toUpperCase();
 +
    remainChar = remainChar.toLowerCase();
 +
 +
    return firstChar + remainChar<br>
 +
}
 +
 +
    // Make function globally available
 +
    reportContext.setPersistentGlobalVariable("UCFirst", UCFirst);
 +
 +
    USAGE<br>
 +
    UCFirst("ALLCAPS");  = gives => Allcaps<br>
 +
    UCFirst("alllower"); = gives => Alllower<br>
 +
    UCFirst("MiXeDuP");  = gives => Mixedup<br>
  
// split string <br>
+
=== UCWords (upper case the first character of each word in a string)===
firstChar = str.substring(0,1);<br>
+
remainChar = str.substring(1);<br>
+
<br>
+
// convert case<br>
+
firstChar = firstChar.toUpperCase();<br>
+
remainChar = remainChar.toLowerCase();<br>
+
<br>
+
return firstChar + remainChar<br>
+
</blockquote>
+
}<br>
+
<br>
+
// Make function globally available
+
reportContext.setPersistentGlobalVariable("UCWord", UCWord);
+
  
 +
function UCWords(str) {
 +
    // split string on spaces
 +
    arrStr = str.split(" ");
 +
 +
    var strOut = '';
 +
 +
    for (i=0;i<arrStr.length;i++)
 +
    {
 +
        // split string
 +
        firstChar = arrStr[i].substring(0,1);
 +
        remainChar = arrStr[i].substring(1);
 +
 +
        // convert case
 +
        firstChar = firstChar.toUpperCase();
 +
        remainChar = remainChar.toLowerCase();
 +
 +
        strOut += firstChar + remainChar + ' ';
 +
    }
 +
 +
    // return string, but drop the last space
 +
    return strOut.substring(0, strOut.length - 1);
 +
}
 +
 +
    // Make function globally available
 +
    reportContext.setPersistentGlobalVariable("UCWords", UCWords);
 +
 +
    USAGE<br>
 +
    UCWords("HELLO WORLD");  = gives => Hello World<br>
 +
    UCWords("hello world");  = gives => Hello World<br>
 +
    UCWords("hElLo WoRlD");  = gives => Hello World<br>
  
USAGE<br>
 
UCWord("ALLCAPS");  = gives => Allcaps<br>
 
UCWord("alllower"); = gives => Alllower<br>
 
UCWord("MiXeDuP");  = gives => Mixedup<br>
 
</blockquote></tt>
 
  
 
== Comments ==
 
== Comments ==
 
Please enter comments below by selecting the edit icon to the right.
 
Please enter comments below by selecting the edit icon to the right.
 
You will need a Bugzilla account to add comments.
 
You will need a Bugzilla account to add comments.
 +
 +
 +
[[Category:BIRT]]
 +
[[Category:BIRT Example]]
 +
[[Category:BIRT Example Report]]
 +
 +
 +
Actually, I had issues with the for loop in function UCWords.  It caused my eclipse environment to either run out of memory or to only return the first cell of a multiple cell table. 
 +
 +
<b>Instead, I re-coded to use:</b><br>
 +
 
 +
function UCWords(str){
 +
  var arrStr = str.split(" ");
 +
  var strOut = "";
 +
  var i = 0;
 +
  while (i < arrStr.length) {
 +
      firstChar  = arrStr[i].substring(0,1);
 +
      remainChar = arrStr[i].substring(1);
 +
      firstChar  = firstChar.toUpperCase();
 +
      remainChar = remainChar.toLowerCase();
 +
      strOut += firstChar + remainChar + ' ';
 +
      i++;
 +
  }
 +
  return strOut.substr(0,strOut.length - 1);
 +
}
 +
 +
--[[User:Sstrickland.costco.com|Sstrickland.costco.com]] 19:49, 4 October 2007 (EDT)
 +
 +
<b>Here's a modified version of the function UCWords that takes a set of word delimiters instead of just spaces:</b><br>
 +
 +
<source lang="javascript">function UCWords(str) {
 +
var doneStr = '';
 +
var len = str.length;
 +
var wordIdx = 0;
 +
var char;
 +
for (var i = 0;i < len;i++) {
 +
char = str.substring(i,i + 1);
 +
if (' -/#$&'.indexOf(char) > -1) {
 +
wordIdx = -1;
 +
}
 +
if (wordIdx == 0) {
 +
char = char.toUpperCase();
 +
} else if (wordIdx > 0) {
 +
char = char.toLowerCase();
 +
}
 +
doneStr += char;
 +
wordIdx++;
 +
}
 +
return doneStr;
 +
}
 +
</source>
 +
-- niik

Latest revision as of 05:57, 31 January 2011

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

Contents

[edit] 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.

[edit] BIRT Version Compatibility

This example was built using version 2.2 M5.

[edit] Example Files

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

[edit] 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.

[edit] Functions

[edit] UCFirst (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 UCFirst(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("UCFirst", UCFirst); USAGE
UCFirst("ALLCAPS"); = gives => Allcaps
UCFirst("alllower"); = gives => Alllower
UCFirst("MiXeDuP"); = gives => Mixedup

[edit] UCWords (upper case the first character of each word in a string)

function UCWords(str) {
   // split string on spaces
   arrStr = str.split(" ");

   var strOut = ;

   for (i=0;i<arrStr.length;i++)
   {
       // split string
       firstChar = arrStr[i].substring(0,1);
       remainChar = arrStr[i].substring(1);

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

       strOut += firstChar + remainChar + ' ';
   }

   // return string, but drop the last space
   return strOut.substring(0, strOut.length - 1);
}

   // Make function globally available
   reportContext.setPersistentGlobalVariable("UCWords", UCWords);

   USAGE
UCWords("HELLO WORLD"); = gives => Hello World
UCWords("hello world"); = gives => Hello World
UCWords("hElLo WoRlD"); = gives => Hello World


[edit] Comments

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


Actually, I had issues with the for loop in function UCWords. It caused my eclipse environment to either run out of memory or to only return the first cell of a multiple cell table.

Instead, I re-coded to use:

function UCWords(str){
  var arrStr = str.split(" ");
  var strOut = "";
  var i = 0;
  while (i < arrStr.length) {
     firstChar  = arrStr[i].substring(0,1);
     remainChar = arrStr[i].substring(1);
     firstChar  = firstChar.toUpperCase(); 
     remainChar = remainChar.toLowerCase();
     strOut += firstChar + remainChar + ' ';
     i++;
  }
  return strOut.substr(0,strOut.length - 1);
}

--Sstrickland.costco.com 19:49, 4 October 2007 (EDT)

Here's a modified version of the function UCWords that takes a set of word delimiters instead of just spaces:

function UCWords(str) {
	var doneStr = '';
	var len = str.length;
	var wordIdx = 0;
	var char;
	for (var i = 0;i < len;i++) {
		char = str.substring(i,i + 1);
		if (' -/#$&'.indexOf(char) > -1) {
			wordIdx = -1;
		}
		if (wordIdx == 0) {
			char = char.toUpperCase();
		} else if (wordIdx > 0) {
			char = char.toLowerCase();
		}
		doneStr += char;
		wordIdx++;
	}
	return doneStr;
}

-- niik