官术网_书友最值得收藏!

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

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.

主站蜘蛛池模板: 翁源县| 宣武区| 柘荣县| 深水埗区| 望城县| 古丈县| 新津县| 嘉黎县| 益阳市| 安仁县| 梅河口市| 余姚市| 景宁| 乐安县| 沂南县| 海伦市| 岱山县| 介休市| 永平县| 内黄县| 商洛市| 资溪县| 宜兰市| 烟台市| 南宁市| 久治县| 龙南县| 神木县| 喜德县| 洛浦县| 航空| 海宁市| 汉寿县| 弥渡县| 手游| 文水县| 寿光市| 荆门市| 沙坪坝区| 井陉县| 滨海县|