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

Showing/hiding prompt controls at runtime

Let's say a report shows sales quantity by product line and order method type. Users need to filter on either product line or order method type, any one at a time.

They would like a facility to select which prompt they would want to filter on, and depending on the selection, the prompt should appear.

Getting ready

Create a list report that shows product lines, order method types, and sales quantity. Create two options filters—one on product lines and the other on order methods.

How to do it...

In this recipe, we will use JavaScript to control showing or hiding a prompt based on the selection of another prompt. To do this, perform the following steps:

  1. We will start by creating prompts for both the filters. For that, add a prompt page and add two value prompts. Use the prompt wizard to connect them to the parameters (product line and order method).
  2. Set the Hide Adornment property of both the prompts to Yes.
  3. Now drag an HTML item just before the product line prompt. Define it as follows:
    <Input type = radio Name = r1 title= "Click me to select Product Line..." Value = "PL" onclick= "radioSelect(this)">Product Line &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <Input type = radio Name = r1 title= "Click me to select Order Method..." Value = "OM" onclick= "radioSelect(this)">Order Method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <span id = 'ProductSpan'>
  4. Now add another HTML item between the product line prompt and order method prompt. Define it as </span> <span id = 'OrderSpan'>.
  5. Finally, add a third HTML item after the order method prompt. Define it as follows:
    </span>
    <script>
    var fW = (typeof getFormWarpRequest == "function" ?getFormWarpRequest() : document.forms["formWarpRequest"]);
    if ( !fW || fW == undefined) { fW = ( formWarpRequest_THIS_ ?formWarpRequest_THIS_ : formWarpRequest_NS_ );}
    var theSpan = document.getElementById("ProductSpan");
    var a = theSpan.getElementsByTagName('select');
    for( var i = a.length-1; i >= 0; i-- )
    { var ProductBox = a[i];
      ProductBox.style.display = 'none'; }
    theSpan = document.getElementById("OrderSpan");
    a = theSpan.getElementsByTagName('select');
    for( var i = a.length-1; i >= 0; i-- )
    { var OrderBox = a[i];
      OrderBox.style.display = 'none'; }
    
    function radioSelect(rad)
    { if (rad.value == "PL")  /* Hide OrderBox and show ProductBox */
      { ProductBox.style.display = '';
      OrderBox.style.display = 'none';
    }
    else if (rad.value == "OM") /* Hide ProductBox and show OrderBox */
    { ProductBox.style.display = 'none';
      OrderBox.style.display = ''; 
    }
    else /* Hide both controls */
    { ProductBox.style.display = 'none';
      OrderBox.style.display = 'none'; }
    }
    </script>

    Now your prompt page will look like the following screenshot in Report Studio:

  6. Run the report to test it. You will see two radio buttons. Depending on which one you select, one of the prompts will be visible as shown in the following screenshot:

How it works...

This recipe works in three parts. First, we defined the radio buttons in the HTML item. This is our own code, so we can control what happens when users select any of the radio buttons.

Tip

Before explaining how this recipe works, I would like the readers to know that it is possible to achieve the required functionality using conditional blocks instead of JavaScript. You would use the auto-submit functionality of the radio button prompt, which will then cause the conditional block to show the appropriate prompt.

Then, we wrapped both the prompts into spans so that we can capture them in the JavaScript and manipulate the properties.

Finally, we wrote the JavaScript to toggle the display of prompts depending on the radio button selection.

There's more...

When the prompt is hidden through the style.display property, the adornments aren't hidden. That is why we set the adornments to off in step 2.

When the visibility of a control is turned off, the control is still present on the form and the selected value (if any) is also submitted in the query when the user clicks on the Finish button.

Hence, it is preferred that we reset the selection to index(0) when a prompt is hidden. For information on how to select a value through JavaScript, please refer to the Defining dynamic default values for prompts recipe of this chapter.

主站蜘蛛池模板: 武鸣县| 通山县| 铜山县| 永州市| 饶平县| 恩平市| 疏勒县| 内丘县| 潞城市| 阳谷县| 神农架林区| 三原县| 新邵县| 莲花县| 阿合奇县| 乌什县| 景东| 黑水县| 化州市| 威远县| 蒙阴县| 忻州市| 谢通门县| 石棉县| 云龙县| 镇远县| 宁陵县| 龙井市| 和田市| 东乡县| 屏东市| 舒兰市| 沙坪坝区| 玉环县| 南城县| 竹溪县| 老河口市| 鄯善县| 乡宁县| 平乐县| 天全县|