
Selecting and submitting values automatically
A business report has numerous prompts on the prompt page. Often, users want to run a report for the latest month in the database, Camping Equipment product and E-mail as an order method.
They want a facility to either manually select values for these prompts or alternatively run the report for the previous selections on a single button click.
Getting ready
Create a list report with Product line, Order method, and Sales Quantity as columns.
Create optional filters on Product line, Order method, and the shipment month, that is, Month Key (shipment date).
Create a prompt page with three value prompts for these filters.
How to do it...
In this recipe, we will add a custom button on the prompt page that will allow users to quickly run the report for frequently used selections. To do this, perform the following steps:
- We will start by wrapping the prompts within a span so that they can be captured easily in JavaScript. Add one HTML tag before and one after each prompt to define the spans. Define the spans as PL, OM, and SM for Product Line, Order Method, and Shipment Month respectively. This is similar to the wrapping we have done in most of the prior recipes.
- Add one more HTML item on the prompt page after all the prompts and define it as follows:
<script> function defaultSelect() { var a = document.getElementById("PL"); var PL = a.getElementsByTagName("select"); for( var i = PL.length-1; i >= 0; i-- ) /* Captures Product Line prompt */ { var PLBox = PL[i]; } a = document.getElementById("OM"); var OM = a.getElementsByTagName("select"); for( var i = OM.length-1; i >= 0; i-- ) /* Captures Order Method prompt */ { var OMBox = OM[i]; } a = document.getElementById("SM"); var SM = a.getElementsByTagName("select"); for( var i = SM.length-1; i >= 0; i-- ) /* Captures Shipment Month prompt */ { var SMBox = SM[i]; } PLBox.selectedIndex = 2; OMBox.selectedIndex = 2; SMBox.selectedIndex = 4; canSubmitPrompt(); promptButtonFinish(); } </script> <button type="button" onclick="defaultSelect()" class="bt" style="font-size:8pt">Run for Defaults</button>
Now your prompt will look similar to the following screenshot in Report Studio:
- Run the report to test it. You should see a button that you did not see in Report Studio. When you click on the button, it will automatically select the prompt values and run the report as shown in the following screenshot:
How it works...
In this recipe, we are mixing two techniques learnt from previous recipes. In the Defining dynamic default values for prompts recipe, we learnt how to capture a value prompt and change its selection.
So, we are using the same technique here but instead of calling on Page Load
, we are calling the routine when users click on the button.
Then, we are also using a function, promptButtonFinish()
, that we used in the Validating textbox prompts recipe to submit the prompt.
The custom button is defined using the <button>
tag, and as it is our own object, we can easily make it call our JavaScript function for the on click
event.
As mentioned in the Defining dynamic default values for prompts recipe, you will not hardcode the selectedIndex
in your script. Instead, you should traverse through all the prompt selection options and choose one based on the value. For example, look for Camping Equipment so that its order in the list won't matter.
Please refer to one such example on the IBM website at this URL: http://www-01.ibm.com/support/docview.wss?uid=swg21343424.
There's more...
This technique is very useful in real-life scenarios. You can define multiple buttons for different frequently used selections. It saves time for users and makes the reports convenient to use, especially when there are more than five prompts.