If you are unable to create a new account, please email support@bspsoftware.com

 

Integrate OnChange/Onclick event to control default prompt select via JavaScript

Started by CWillSAG, 06 Apr 2017 03:14:23 PM

Previous topic - Next topic

CWillSAG

Hello,

Cognos 10.2.1 is current environment.

I'm working with a basic JavaScript (from IBM) that sets the default value for a dynamic prompt every time an prompt event occurs (all prompts are on report page, and are set to autosubmit).

<script>
var zxcv = {};

// Get the required prompt.
var oCR = cognos.Report.getReport("_THIS_");
var oP = oCR.prompt.getControlByName("Prompt_Name");

// Get the values and isolate the first value.
var allValues = oP.getValues(true);
var firstValue = allValues[0];

// Set the prompt selection to the first value.
oValues = [firstValue];
oP.setValues(oValues);

</script>

Easy enough, but not sure this is what I need. What I would like to do is the following:

1. I have 7 potential prompts/events on this page (along with other prompts/non-events that can be ignored), and I want to execute the above action (reset default of Prompt_Name)  only when a user selects a new value from one of these 7 prompts/events. How can I integrate such logic using an attribute like onChange or onClick (or something else)?
2. When performing this action, I need to clear the value stored in my report parameter (?p_prompt?) as well what is displayed on the prompt, and run the report for the default value that is selected in "Prompt_Name", all in one fell swoop. In my attempts, the default is set in the prompt when the report is rendered, but ParamValue('p_prompt') shows the previous value.
3. Is there a way to hide a static value in "Prompt_Name" if certain conditions are met (ie Event Prompt in (value list)). I can probably try to separate prompts, but that can get messy.

Also, the prompt I'm setting the default for is a mix of static values and query values, so the list of choices is dynamic.

This a cube based report.

Sounds like a fun challenge, doesn't it?

AnalyticsWithJay

Quote from: CWillSAG on 06 Apr 2017 03:14:23 PM
What I would like to do is the following:

1. I have 7 potential prompts/events on this page (along with other prompts/non-events that can be ignored), and I want to execute the above action (reset default of Prompt_Name)  only when a user selects a new value from one of these 7 prompts/events. How can I integrate such logic using an attribute like onChange or onClick (or something else)?

2. When performing this action, I need to clear the value stored in my report parameter (?p_prompt?) as well what is displayed on the prompt, and run the report for the default value that is selected in "Prompt_Name", all in one fell swoop. In my attempts, the default is set in the prompt when the report is rendered, but ParamValue('p_prompt') shows the previous value.

3. Is there a way to hide a static value in "Prompt_Name" if certain conditions are met (ie Event Prompt in (value list)). I can probably try to separate prompts, but that can get messy.

Also, the prompt I'm setting the default for is a mix of static values and query values, so the list of choices is dynamic.

How's your Javascript knowledge?

To do this, you need to add an event listener for the change as such:


<script>

var report = cognos.Report.getReport("_THIS_");
var controls = report.prompt.getControls();

var jayControl1 = document.getElementById(controls[0]._eS.id);
var jayControl2 = document.getElementById(controls[1]._eS.id);
var jayControl3 = document.getElementById(controls[2]._eS.id);

// Non-IE browsers
if (jayControl1.addEventListener){
           jayControl1.addEventListener('change', function(){
//code goes here
alert('Something changed');
});
}
// IE Browsers
else if (jayControl1.attachEvent){
    jayControl1.attachEvent('onchange', function(){
//code goes here
alert('Something changed');
});
}


</script>


Essentially, you need to:
1) Access the control
2) Add an event listener, such as change or click

Note: I did this by accessing the _eS.id property (ID of the control) to make my example easier, but this is really a hack so I can GetElementById. This property isn't exposed by the API, so it might not work in the future. It's better to access the controls by getElementsbyTagName. It'll require slightly more code, but it's safer.

For tasks such as clearing values, setting values, etc, use the Prompt API.

To utilize dynamic hidden values, check out these posts:
http://www.cognoswithjay.com/assigning-a-dynamic-default-value-to-a-text-box-prompt-using-javascript/

http://www.cognoswithjay.com/selecting-a-dynamic-default-value-for-value-prompts-using-javascript/

http://www.cognoswithjay.com/selecting-multiple-prompt-values-dynamically-using-javascript/


Please post your updated code if you run into any issues.

CWillSAG

Thanks for taking the time to respond. I'll give it a try and let you know the results.

My JavaScript knowledge is very limited/outdated, but I have someone in my organization working with me. Your suggestion looks pretty straightforward, so we'll be thrilled to give this a shot.