1
votes

I obtained the below javascript from a website a while back which shows and hides a date prompt based on the selections from a radio button value prompt.

<script type="text/javascript">
var inputs = document.getElementsByTagName("input");
var list_box = new Array();

/****Identify Radio Buttons and load them into an array****/
var radio_buttons = new Array();
j=0;

for(i=0;i<inputs.length;i++)
{
if(inputs[i].type=='radio')
  {
    radio_buttons[j] = inputs[i];
    j++;
  }   
}

/****Set the onclick event of each radio button option to trigger our custom function****/
radio_buttons[0].setAttribute("onclick",function(){ToggleDate();});
radio_buttons[1].setAttribute("onclick",function(){ToggleDate();});
radio_buttons[2].setAttribute("onclick",function(){ToggleDate();});


/****Create a handle for date prompt****/
var prompt = document.getElementById("date_prompts").childNodes[0];

function ToggleDate()
{
if(radio_buttons[0].checked)
  {
  /**** First options (in our case "Custom") is selected.**** 
   **** Date Prompt will be Shown or Enabled.            ****/

  document.getElementById("date_prompts").style.display = '';  //show
  // prompt.disabled = '';   //enable

  canSubmitPrompt();
  }
else
  {
  /**** Second options (in our case "Yesterday") is selected.****
   **** Date Prompt will be Hidden or Disabled.              ****/

  document.getElementById("date_prompts").style.display = 'none';   //hide
  //prompt.disabled = 'true';   //disable

  canSubmitPrompt();
  }
}
</script> 

Whilst we have been using Cognos 10.2.2 this code worked fine (in conjunction with a couple of other html items spanning the date prompt). We're now in the process of upgrading to Cognos 11.0.11, whilst I've been testing the reports in the new environment I've found that this code no longer works and the date prompt is shown irrespective of what choices are selected with the radio buttons.

Please can someone give me some pointers as to what might be going on here or better yet tell me how to resolve the issue.

When creating a new report in Cognos 11 and adding the elements individually the following error message occurs...

HTML report output is not well formed. If your report uses "HTML Item" elements ensure that they result in well-formed HTML.

Reason: A name contained an invalid character.

URL: Line:274 Character:24 Source: for(i=0;i

2

2 Answers

1
votes

Cognos released a JavaScript API with Cognos 10.2 that vastly simplifies what you are doing here. Additionally, it's guaranteed to be supported between versions.

Here's the same functionality written against the Cognos JavaScript API:

Assumptions:

  • There is a single Cognos Select prompt configured as a radio button group that has the Name property 'radio_buttons' giving a choice of three separate values (1, 2, and 3)
  • There is a date prompt wrapped in a span with id attribute 'date_prompts' that should be shown when the first radio button is selected but not shown if the other two are selected

Code

var report = cognos.Report.getReport('_THIS_'); //Get report reference
var radioprompt = report.prompt.getControlByName('radio_buttons'); //Get prompt reference
var datespan = document.getElementById('date_prompts'); //Get span to hide/unhide

radioprompt.setValidator(validateRadio); //Assign a validation function

function validateRadio(values) {
     var result = true;
     if (values && values.length > 0) { //Make sure prompt has value
          if (values[0].use == '1') {  //Check if first radio button selected
               datespan.style.display = ''; //Show
          } else {
               datespan.style.display = 'none'; //Hide
          }
     }    
     return result; //Return result to Cognos. Always true in this case.
}

You can find more information on the Cognos JavaScript API here: Cognos 11 JavaScript API Documentation

0
votes

You can do that with a render variable.

  1. Set the Run with full interactivity property of the report to No.
  2. Create a prompt page and add a value prompt and a date prompt.
  3. Set the Select UI property of the value prompt to Radio button group.
  4. Set the Auto-submit property of the value prompt to Yes.
  5. Add the values Show Date Prompt and Hide Date Prompt to the Static choices property of the value prompt.
  6. Create a string variable (Queries | Condition explorer) that uses the parameter associated with the value prompt.
  7. Set the Render variable property of the date prompt to the variable created in the previous step. Deselect Hide Date Prompt.

To keep the auto-submit feature from running the report, you'll probably need to create some custom features to replace the Finish button. See the sample code in the report spec below. (The Run with full interactivity property doesn't appear to be stored in the report spec. You'll need to set that manually.)

This specific example works in 10.2.1 or in 11.0.7 in compatibility mode (Run with full interactivity = No). I haven't yet worked up to do this properly in 11.0.4+ using RequireJS.

<report xmlns="http://developer.cognos.com/schemas/report/14.1/" useStyleVersion="11.4" expressionLocale="en-us">
    <drillBehavior/>
    <layouts>
        <layout>
            <reportPages>
                <page name="Page1">
                    <style>
                        <defaultStyles>
                            <defaultStyle refStyle="pg"/>
                        </defaultStyles>
                    </style>
                    <pageBody>
                        <style>
                            <defaultStyles>
                                <defaultStyle refStyle="pb"/>
                            </defaultStyles>
                        </style>
                        <contents/>
                    </pageBody>
                </page>
            </reportPages>
            <promptPages>
                <page name="Prompt page1">
                    <pageHeader>
                        <contents>
                            <block>
                                <contents>
                                    <textItem>
                                        <dataSource>
                                            <staticValue>Prompt Page</staticValue>
                                        </dataSource>
                                        <style>
                                            <defaultStyles>
                                                <defaultStyle refStyle="tt"/>
                                            </defaultStyles>
                                        </style>
                                    </textItem>
                                </contents>
                                <style>
                                    <defaultStyles>
                                        <defaultStyle refStyle="ta"/>
                                    </defaultStyles>
                                </style>
                            </block>
                        </contents>
                        <style>
                            <defaultStyles>
                                <defaultStyle refStyle="hp"/>
                            </defaultStyles>
                        </style>
                    </pageHeader>
                    <pageBody>
                        <contents>
                            <table>
                                <style>
                                    <defaultStyles>
                                        <defaultStyle refStyle="tb"/>
                                    </defaultStyles>
                                    <CSS value="border-collapse:collapse;width:100%"/>
                                </style>
                                <tableRows>
                                    <tableRow>
                                        <tableCells>
                                            <tableCell>
                                                <contents>
                                                    <selectValue selectValueUI="radioGroup" autoSubmit="true" parameter="ShowHide">
                                                        <selectOptions>
                                                            <selectOption useValue="Show Date Prompt">
                                                                <displayValue>Show Date Prompt</displayValue>
                                                            </selectOption>
                                                            <selectOption useValue="Hide Date Prompt">
                                                                <displayValue>Hide Date Prompt</displayValue>
                                                            </selectOption>
                                                        </selectOptions>
                                                    </selectValue>
                                                    <selectValue parameter="Parameter2" cascadeOn="ShowHide" required="false">
                                                        <style>
                                                            <CSS value="visibility:hidden"/>
                                                        </style>
                                                    </selectValue>
                                                </contents>
                                                <style>
                                                    <CSS value="text-align:left;vertical-align:top"/>
                                                </style>
                                            </tableCell>
                                            <tableCell>
                                                <contents>
                                                    <selectDate parameter="Date">
                                                        <conditionalRender refVariable="String1">
                                                            <renderFor refVariableValue="Show Date Prompt"/>
                                                        </conditionalRender>
                                                    </selectDate>
                                                </contents>
                                                <style>
                                                    <CSS value="text-align:left;vertical-align:top"/>
                                                </style>
                                            </tableCell>
                                        </tableCells>
                                    </tableRow>
                                </tableRows>
                            </table>
                        </contents>
                        <style>
                            <defaultStyles>
                                <defaultStyle refStyle="py"/>
                            </defaultStyles>
                        </style>
                    </pageBody>
                    <pageFooter>
                        <contents>
                            <promptButton type="cancel">
                                <contents/>
                                <style>
                                    <defaultStyles>
                                        <defaultStyle refStyle="bp"/>
                                    </defaultStyles>
                                </style>
                            </promptButton>
                            <promptButton type="finish">
                                <contents/>
                                <style>
                                    <defaultStyles>
                                        <defaultStyle refStyle="bp"/>
                                    </defaultStyles>
                                </style>
                            </promptButton>
                        </contents>
                        <style>
                            <defaultStyles>
                                <defaultStyle refStyle="fp"/>
                            </defaultStyles>
                        </style>
                    </pageFooter>
                    <style>
                        <defaultStyles>
                            <defaultStyle refStyle="pp"/>
                        </defaultStyles>
                    </style>
                </page>
            </promptPages>
        </layout>
    </layouts>
    <XMLAttributes>
        <XMLAttribute output="no" name="RS_CreateExtendedDataItems" value="true"/>
        <XMLAttribute output="no" name="RS_modelModificationTime" value="2013-01-08T15:30:33.117Z"/>
        <XMLAttribute output="no" name="listSeparator" value=","/>
    </XMLAttributes>
    <modelPath>/content/folder[@name=&apos;Samples&apos;]/folder[@name=&apos;Models&apos;]/package[@name=&apos;GO Sales (query)&apos;]/model[@name=&apos;model&apos;]</modelPath>
    <reportVariables>
        <reportVariable type="string" name="String1">
            <reportExpression>ParamDisplayValue(&apos;ShowHide&apos;)</reportExpression>
            <variableValues>
                <variableValue value="Show Date Prompt"/>
                <variableValue value="Hide Date Prompt"/>
            </variableValues>
        </reportVariable>
    </reportVariables>
    <reportName>ShowHide</reportName>
</report>