Wednesday, December 28, 2011

Customize Value Prompt Names using Javascript

Customize Value Prompts using Javascript
Business Case:
Value prompt is widely used in Report Studio Reports. Users want to display a message in the prompt or display a name different from the parameter name or remove the parameter name.

Environment: IBM Cognos 8.4.1, Internet Explorer/Mozilla Firefox, Javascript.

Here's how my value prompt looks like by default:

Example 1:
In the country prompt, the parameter name is "p_country" or "country", but user wants to display "Please select a Country". User also wants to remove the dummy option with "--------".

Code:
<script>
//Get a list of elements on the page with "select" tag
var value_prompt = document.getElementsByTagName("select");

//Loop through the elements
for(var i=0;i<value_prompt.length;i++)
{
  //Check if element is a value prompt
  if(value_prompt[i].className = "clsSelectControl pv")
  {
    value_prompt[i].options[0].text = "Select a Page Type";
  }
}
</script>


Now the prompt looks like this:
Example 2:
User wants to remove both the name and the "-------" options from the value prompt (the first two entries). The prompt should contain only the values.


Code:
<script>
//Get a list of elements on the page with "select" tag
var value_prompt = document.getElementsByTagName("select");

//Loop through the elements
for(var i=0;i<value_prompt.length;i++)
{
  //Check if element is a value prompt
  if(value_prompt[i].className = "clsSelectControl pv")
  {
    // Remove the first two options
    value_prompt[i].remove(0);
    value_prompt[i].remove(0);
  }
}
</script> 

 
This script removes the first two options and makes the prompt look like:

Implementation:
  • On the prompt page, create an HTML item at the end of the page after all the prompts.
  • Place the code above in the HTML item.
  • You can change the value of [i] to customize the prompt of your choice. For example, if there are multiple value prompts on the page, the code for example 2 above will remove the name and '----' for all the prompts.

---Give the world the best you have and you'll get kicked in the teeth. 
Give the world the best you have anyway.---

8 comments:

  1. Thank you for posting and sharing your expertise.
    I have a similar question and wondering if you have help.
    We have two value prompts as Month (Jan, Feb etc) and Year (2011,2012 etc) and I am looking to default the these pick list values to the current maximum MONTH and YEAR values present in a summary table. If users choose to, they can change the default values to pick the last available prompt values and run the report.
    Is there a way for us to default the values in a list prompt, based on a database value. Any ideas/clues will be of great help. Thank you.

    ReplyDelete
  2. Hi,

    Yes this is possible using javascript. You will have to place a hidden list report (visible property of list set to 'No') in the prompt page which displays the latest month and year in the database. You can thn use javascript to read this value and set the default value of the value prompts.

    Sriram.

    ReplyDelete
  3. Thank you for your input Sriram..May be too much to ask for, but will you be able to post a sample script that can read a list value and default the value prompt.

    Do not have much expertise’s with JavaScripting to start off on this.

    ReplyDelete
  4. Siram,

    What javascript shoul I place, if I have two value prompts in one line and each have different parameter.

    Regards

    ReplyDelete
    Replies
    1. Hi,

      If you want to change the name of multiple prompts, then use the following code outside the for loop. Let's say you have 3 prompts, then:

      value_prompt[0].options[0].text = "Select a Country";
      value_prompt[1].options[0].text = "Select a State";
      value_prompt[2].options[0].text = "Select a City";

      Sriram.

      Delete
  5. Hi, I dont know whether this is the right place, but:
    is there a way to automatically loop through different pages of an html report?

    thanks

    ReplyDelete
    Replies
    1. Hi,
      I have done something similar. But instead of looping through html pages, I put all the sections in different blocks in the same page and use javascript to loop through them like a slide show. I had also set the report to refresh after looping 10 times. I will write it up as a new article.

      Thanks,
      Sriram.

      Delete
  6. Work first time. Awesome! Many thanks

    ReplyDelete