COGNOiSe.com - The IBM Cognos Community

IBM Cognos 10 Platform => Cognos 10 BI => Report Studio => Topic started by: Dkelley2001 on 24 Jan 2014 10:37:06 AM

Title: Prompt refresh issue with JavaScript
Post by: Dkelley2001 on 24 Jan 2014 10:37:06 AM
I have a report with the following requirements:

On prompt page there are several prompts. Fiscal Year, From date, To Date and others. These first three prompts are the ones in question. User needs to be able to select a Fiscal Year which auto-submits upon selection. Depending on the value selected the From date and To Date prompt should default to the fiscal year date range for selected year. For example 2013 = Oct 01 2012 to Sep 30 2013.

Then the user can modify that date range or apply other prompts from the page.

The first part works. User selects year, date range populates correctly. Issue is once date range is modified or other prompts are added the page refreshes and JavaScript runs causing date range to go back to original range. How can I get the JavaScript to execute only when Fiscal Year is changed?

Here is the script for the ToDate. Thank for the help.

<script type="text/javascript">

var fW = getFormWarpRequest();
//Fiscal Year Prompt
var prmpt = fW._oLstChoicesMyPrompt.value;

function ToDate()
{
var dtToDt = new Date (14,09,30);
var strToDt = [prmpt ,09,dtToDt.getDate()].join("-");
return strToDt;
}
pickerControlPDateTo.setValue(ToDate() );
</script>
Title: Re: Prompt refresh issue with JavaScript
Post by: CognosPaul on 25 Jan 2014 11:17:44 PM
The easiest way would to apply an on-change event to the year prompt. You can also also add a new HTML item for pickerControlPDateTo.setValue(ToDate() ); with a render variable so it only renders when the date parameter has no value.

Which version are you using?
Title: Re: Prompt refresh issue with JavaScript
Post by: Dkelley2001 on 27 Jan 2014 06:45:52 AM
CognosPaul

Thank you for the reply. I am using Cognos 10.1

I had looked into the on-change, with little luck. I am not real clear on how to apply it. Would this function work just like it sounds? The to_date() function would only be called after change to the year prompt?

Thanks for the help.
Title: Re: Prompt refresh issue with JavaScript
Post by: CognosPaul on 27 Jan 2014 07:03:01 AM
It works exactly as it sounds, your function would be called every time the prompt is changed.

Try the below:
<script>

var fW = (typeof getFormWarpRequest == "function" ? getFormWarpRequest() : document.forms["formWarpRequest"]);
if ( !fW || fW == undefined)
   { fW = ( formWarpRequest_THIS_ ? formWarpRequest_THIS_ : formWarpRequest_NS_ );}


function yo(){
alert(this.value)
}

var list = fW._oLstChoicesMyPrompt;

list.onchange= yo;

</script>


Since you're not passing any values to the function, you can call it with list.onchange=functionName; If you did need to pass parameters, you could use an anonymous function:
list.onchange = function() {yo('a',1)}

Note the different way that I'm getting the fW class. This method will work in pages and portlets.
Title: Re: Prompt refresh issue with JavaScript
Post by: Dkelley2001 on 27 Jan 2014 08:54:32 AM
Thanks, I think this is getting me very close. The onchange is working as needed, now my issue is actually populating the date prompt with the proper date. In my original script I was using pickerControlPDateTo.setValue(ToDate() ); to set the prompt with strToDt.  Now I need to do this only when onchange is triggered. This is my latest script. For Alert this now returns 'undefined'. I have tried different ways to use the anonymous function, with no luck.

<script type="text/javascript">

var fW = (typeof getFormWarpRequest == "function" ? getFormWarpRequest() : document.forms["formWarpRequest"]);
if ( !fW || fW == undefined)
   { fW = ( formWarpRequest_THIS_ ? formWarpRequest_THIS_ : formWarpRequest_NS_ );}

function ToDate(){
alert(this.value)
var dtToDt = new Date (14,09,30);
var strToDt = [prmpt ,09,dtToDt.getDate()].join("-");
return strToDt;
}

var prmpt = fW._oLstChoicesMyPrompt;
prmpt.onchange =  function(){pickerControlPDateTo.setValue(ToDate() );}

</script>
Title: Re: Prompt refresh issue with JavaScript
Post by: CognosPaul on 27 Jan 2014 12:28:14 PM
The alert is failing because it's being called from the anonymous function. So the "this" is the function, which lacks a value attribute, thus giving the "undefined" error.

Instead, you can pass values to the ToDate function:

function ToDate(prompt, promptValue){
alert(prompt.value);
alert(promptValue);
var dtToDt = new Date (14,09,30);
var strToDt = [prmpt ,09,dtToDt.getDate()].join("-");
return strToDt;
}

var prmpt = fW._oLstChoicesMyPrompt;
prmpt.onchange =  function(){pickerControlPDateTo.setValue(ToDate(this, this.value) );}
Title: Re: Prompt refresh issue with JavaScript
Post by: Dkelley2001 on 27 Jan 2014 01:57:29 PM
Thank you. I am going to try and implement it that way now. In the mean time I was able to get it to work with the script below. While this does work there are a few things that don't look logical to me. I have two prompts PDateFrom and PDateTo. Originally I had two scripts. one for each prompt. Now I have 1 script that is used in two separate HTML items. I also need the year for PDateFrom to be MyPrompt -1. As you can see I had to do 'this.value--',  to the PDateTo. This script does work, but I really don't understand why. Maybe you can help me with that?

<script type="text/javascript">

var fW = (typeof getFormWarpRequest == "function" ? getFormWarpRequest() : document.forms["formWarpRequest"]);
if ( !fW || fW == undefined)
   { fW = ( formWarpRequest_THIS_ ? formWarpRequest_THIS_ : formWarpRequest_NS_ );}

var prmpt = fW._oLstChoicesMyPrompt;

fW._oLstChoicesMyPrompt.onchange = function() {

pickerControlPDateTo.setValue([this.value--, '09', '30'].join('-'));
pickerControlPDateFrom.setValue([this.value, '10', '01'].join('-'));
}
   
</script>


Title: Re: Prompt refresh issue with JavaScript
Post by: Dkelley2001 on 28 Jan 2014 07:03:21 AM
CognosPaul

Thanks for your help on this. I was able to get things to work last night. I was able to work with your suggestions and everything is working as expected.

I really appreciate your help on this.