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

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.

主站蜘蛛池模板: 尚义县| 宁南县| 得荣县| 东方市| 射阳县| 汕尾市| 英吉沙县| 东乡族自治县| 光泽县| 县级市| 包头市| 金溪县| 青岛市| 民权县| 鹤山市| 缙云县| 班戈县| 扶余县| 古浪县| 苗栗县| 北辰区| 哈密市| 兴宁市| 西林县| 沈丘县| 黄浦区| 伊宁县| 合山市| 晋江市| 津南区| 大厂| 米林县| 西吉县| 和硕县| 汉川市| 忻州市| 九龙县| 古丈县| 新绛县| 婺源县| 连州市|