There is a report which allows users to select a shipment month. The Shipment Month dimension has values up to current month. However, users frequently select prior month. They want the prompt to have prior month selected by default.
Take any report that filters on Shipment Month Key. Add a value prompt to the Shipment Month Key on the prompt page.
<span id = 'A1'>
</span>
<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>
The logic used here is that we first sort the months in descending order and then select the second option from top. As the values populated from the database are up to the latest month, the second value from 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 Cognos.
When we put a JavaScript within an HTML item, it is automatically executed when the page loads.
With Cognos 8.3, the report viewer architecture has been majorly changed. With CRN 8.1 and 8.2, it was a common practice to define a NAME
or ID
for the prompt controls and use that to manipulate controls at runtime through JavaScript.
However, 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 SPAN tags, we will reduce the scope of our search in JavaScript.
As we want to capture the Value Prompt within span, we search elements with select
tag within the span A1.
If we want to perform same operation on multiple value prompts, we can put them all within same span. The GetElementsByTagName
function returns an array of elements with the specified tag.
A more suitable example of 'dynamic selection' will be iterating through the value prompt options and selecting one based on a condition.
You can use the Java functions to capture 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 the selectedIndex
to 3.
Please refer to one such example on the IBM website at this URL: http://www-01.ibm.com/support/docview.wss?uid=swg21343424