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

 

News:

MetaManager - Administrative Tools for IBM Cognos
Pricing starting at $2,100
Download Now    Learn More

Main Menu

Prompt refresh issue with JavaScript

Started by Dkelley2001, 24 Jan 2014 10:37:06 AM

Previous topic - Next topic

Dkelley2001

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>

CognosPaul

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?

Dkelley2001

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.

CognosPaul

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.

Dkelley2001

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>

CognosPaul

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) );}

Dkelley2001

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>



Dkelley2001

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.