Cog JS Cookbook 5
Cog JS Cookbook 5
Introduction
Report Studio is a web-based tool and the reports designed in Cognos Report Studio are
accessed through a web browser. This allows us to do certain web page specific tasks,
for example, embedding our own HTML code or JavaScript files.
Often, business users need certain functionality which is not naturally available in Cognos
Report Studio. Hence, a new area has evolved in the Cognos Report Studio developer's
world—"JavaScripting".
With JavaScript, we can do certain manipulations on the objects used for prompt pages.
Please note that this was not officially introduced in the initial Cognos documentation.
However, lately many such techniques were published on the IBM website itself.
Using JavaScript Files – Tips and Tricks
70
In this chapter, we will look at some recipes that will teach you very useful and commonly
required functionalities achieved using JavaScript files. All these recipes are valid for IBM
Cognos 10. For Cognos 8, some code changes might be required. There are a lot of
examples and reading material is available for prior versions on the Internet.
After trying these recipes, you can build upon the ideas to write more sophisticated scripts
and do a lot more with your Cognos Reports. Please note that IBM doesn't directly support
these techniques and does not guarantee any upward or backward compatibility. However,
they are aware that developers are widely using them, and hence IBM will try to maintain
most of the objects, properties, and events in the future.
The level of JavaScript that we will be using in this chapter is basic. However, if you have
never used JavaScript before, I would recommend getting familiar with JavaScript basics
using books or online tutorials. The website http://www.w3schools.com/js is a good
source with a nice collection of samples and provides a quick tool to try your own scripts.
Please note that all the JavaScript-based recipes will need you to enable JavaScript in your
web browser. Usually, it is enabled by default.
Defining dynamic default values for prompts
Suppose that we have a report which allows users to select a shipment month. In our data
warehouse, the Time dimension (for shipment month) contains values up to the current
month. However, the business owners frequently select the prior month, so they want the
prompt to have the prior month selected by default.
Getting ready
Create a report that filters on the Shipment Month Key. Create a prompt page and add a
value prompt for the Shipment Month Key.
How to do it...
To achieve the requirements of the business owner, we will write a JavaScript code
that selects the second value from the top by default. In order to do this, perform the
following steps:
1. Open the prompt page in the report and select the value prompt. Adjust the sorting
property such that the Shipment Month Keys are populated in the descending order.
2. Let's start by adding an HTML item before the Shipment Month value prompt.
The HTML should be <span id = 'A1'>.
Chapter 3
71
3. Now add another HTML item after the Shipment Month value prompt. The HTML
should be </span>, as shown in the following screenshot:
4. Now add another HTML item to the prompt page.
5. Define the item as shown in the following code:
<script>
var theSpan = document.getElementById("A1");
var a = theSpan.getElementsByTagName("select"); /* This stmt
return an array of all value prompts within span */
for( var i = a.length-1; i >= 0; i-- ) /* now loop through the
elements */
{ var prompts = a[i];
if( prompts.id.match(/PRMT_SV_/))
{prompts.selectedIndex = 3; } /* This selects the second
options from top */
canSubmitPrompt();
}
</script>
6. Execute the report to test it.
How it works...
The logic used here is that we first sort the months in descending order and then select the
second option from the top. As the values populated from the database are up to the latest
month, the second value from the top will be the previous month.
As mentioned at the beginning of the chapter, Report Studio prompt pages are similar to any
other HTML pages with most of the controls being standard web controls. The HTML item in
Report Studio is a powerful component which allows us to embed our own code within the
page generated by IBM Cognos.
When we put a JavaScript within an HTML item, it is automatically executed when the
page loads.
Using JavaScript Files – Tips and Tricks
72
Span
With IBM Cognos 8.3, the report viewer architecture has been majorly changed. Before IBM
Cognos 8.3, it was common practice to define a NAME or ID for the prompt controls and use
that to manipulate controls at runtime through JavaScript.
However, from Version 8.3 onwards, the IDs of the controls are generated randomly and are
not fixed. So, it is a little difficult to get hold of a control. For this reason, we have defined a
span around the control that we want to manipulate.
By wrapping the control within the span tags, we will reduce the scope of our search
in JavaScript.
GetElementsByTagName
As we want to capture the value prompt within the span, we search for elements with the
select tag within the span A1.
If we want to perform the same operation on multiple value prompts, we can put them all
within the same span. The GetElementsByTagName function returns an array of elements
with the specified tag.
SelectedIndex
Once a value prompt object is captured in a variable, we can set its SelectedIndex property
to set the selection to the required value.
CanSubmitPrompt
In prior versions of Cognos, we used the CheckData() function to submit the prompt value.
This means Report Studio will accept the value and the adornments will disappear. However,
from Version 8.3 onwards, we can use a global CanSubmitPrompt() function for the
same purpose.
There's more...
A more suitable example of dynamic selection is iterating through the value prompt options
and selecting one based on a condition.
You can use the JavaScript functions to capture the system date and accordingly work out
the prior month. Then, traverse through all the values and select an appropriate one. Similarly,
you can iterate through all the prompt values and select the required entry based on value
instead of hard-coding selectedIndex to 3.
Chapter 3
73