Author Topic: Java Script for Text Box Prompt  (Read 408 times)

Offline rajurokz

  • Full Member
  • ***
  • Join Date: Mar 2016
  • Posts: 19
  • Forum Citizenship: +0/-0
Java Script for Text Box Prompt
« on: 18 Apr 2018 10:50:27 pm »
Hi


I have a requirement, where I have 3 prompts(2 text prompts and 1 value prompt). Value prompt is a required field and the user has to input a value in atleast one of the text box prompts, else the finish button should be disabled.


thanks,
Rajesh

Offline squish88

  • Senior Member
  • ****
  • Join Date: Dec 2007
  • Posts: 56
  • Forum Citizenship: +2/-0
Re: Java Script for Text Box Prompt
« Reply #1 on: 20 Apr 2018 12:02:06 pm »
I haven't seen anything for this specifically, but here's a couple of older URL's I had bookmarked around using javascript in Cognos:

https://www.ibm.com/developerworks/data/library/cognos/reporting/scripting_techniques/page516.html

http://cognosandme.blogspot.com/2010_02_01_archive.html

I've also added a script that uses select prompt checks (not text box like you wanted but it might point you in the right direction) that I've cobbled together so that you can maybe get some syntax examples out of it.

Code: [Select]
<script language="javascript">

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

function validat() {

var RSched = fW._oLstChoicesDate_Range_Prompt;
var Granularity = fW._oLstChoicesHeatmap_Granularity_All;
var Sites = fW._oLstChoicesSite_Prompt_External;
var Sites_Chosen = false;
var RScheduleVal = '';
var Granval = '';

/*  Should shortcut out of the granularity check, if both Date Range and Hourly aren't chosen, 'cause there's no need to do the 3 days check  */

if ((RSched[12].selected == true) && (Granularity[1].checked == true)) {
todtval = new Date ( fW.txtDateEnd_Date_Prompt.value );
fmdtval = new Date ( fW.txtDateStart_Date_Prompt.value );
rangeDaysDiff = dateUtils.substractDate(todtval, fmdtval);

if (rangeDaysDiff > 3) {
alert('This report does not support the Hourly level granularity for a date range of greater than 3 days.  Please change either the date range chosen, or the granularity.');
}
else if (rangeDaysDiff < 0) {
alert('Please choose a Start Date that is before the End Date.');
}
else {
if (canSubmitPrompt() == false) {
alert ('Please choose a location before continuing.');
}
else {
promptButtonFinish();
}
}
}

/* should still check to make sure that the start date is before the end date */

else if ((RSched[12].selected == true) && (Granularity[1].checked == false)) {
todtval = new Date ( fW.txtDateEnd_Date_Prompt.value );
fmdtval = new Date ( fW.txtDateStart_Date_Prompt.value );
rangeDaysDiff = dateUtils.substractDate(todtval, fmdtval);
if (rangeDaysDiff < 0) {
alert('Please choose a Start Date that is before the End Date.');
}
else {
if (canSubmitPrompt() == false) {
alert ('Please choose a location before continuing.');
}
else {
promptButtonFinish();
}
}
}

/* if Date Range isn't chosen, then there's nothing to check... so just run the report */

else {
if (canSubmitPrompt() == false) {
alert ('Please choose a location before continuing.');
}
else {
promptButtonFinish();
}
}
}

// These are date utility functions
dateUtils = {
dayInMilliSeconds: 24 * 3600 * 1000,
substractDate:function(endDate, startDate)
{
var startDateObj = startDate;
var endDateObj = endDate;
var diffInMS = endDateObj.getTime() - startDateObj.getTime();
return Math.round(diffInMS != 0? diffInMS/dateUtils.dayInMilliSeconds : 0);
}
}

</script>

<input type="BUTTON" class="clsPromptButton" onClick="validat()" value="Run"/>

Hope it helps!!

P.S. - I am so NOT a javascript expert...  This script was a lot of googling and guesswork, but it worked for me, so thought I'd pass it along!!  You can never have enough syntax examples IMO!!

Offline dougp

  • Statesman
  • ******
  • Join Date: Jul 2014
  • Posts: 293
  • Forum Citizenship: +15/-1
Re: Java Script for Text Box Prompt
« Reply #2 on: 20 Apr 2018 12:12:30 pm »
This is the Cognos 10 forum.  formWarpRequest is a Cognos 8 thing.  It may work in Cognos 10, but I recommend you refer to the Cognos Prompt API documentation.

Offline squish88

  • Senior Member
  • ****
  • Join Date: Dec 2007
  • Posts: 56
  • Forum Citizenship: +2/-0
Re: Java Script for Text Box Prompt
« Reply #3 on: 20 Apr 2018 12:15:29 pm »
Fair enough.  Since no one else had responded yet, I figured I'd give him at least the little bit that I had as a starting point at least.

Offline rajurokz

  • Full Member
  • ***
  • Join Date: Mar 2016
  • Posts: 19
  • Forum Citizenship: +0/-0
Re: Java Script for Text Box Prompt
« Reply #4 on: 20 Apr 2018 01:11:09 pm »
Thanks squish88, That was kind from your part! Will test this with my requirement and see if it works. For that matter, I'm using Cognos 10.2, but I'm hopeful that it works.

thanks,
Rajurokz

Offline CognosPaul

  • Global Moderator
  • Statesman
  • *****
  • Join Date: Jan 2009
  • Posts: 1,619
  • Forum Citizenship: +249/-1
    • Paul's Cognos Blog
Re: Java Script for Text Box Prompt
« Reply #5 on: 20 Apr 2018 01:17:29 pm »
The custom finish button I attached to another post does exactly this. You need to define an array of prompt names in the custom control configuration, and the minimum number of prompts that need to be selected:

"prompts":{"TextBoxPrompt1","TextBoxPrompt2"},"minSelected":1

The problem I'm running into is that I can't find good way of stacking validators onto the prompts, so rechecking each time a prompt is changed isn't working here. This checks if a prompt has a value when the finish button is clicked.

Offline dougp

  • Statesman
  • ******
  • Join Date: Jul 2014
  • Posts: 293
  • Forum Citizenship: +15/-1
Re: Java Script for Text Box Prompt
« Reply #6 on: 20 Apr 2018 07:02:16 pm »
...and it uses RequireJS and AMD so it's compatible with CA 11.0.4+.  Very nice.  I'm very glad you posted this, because I can study it and add it to my tool box.

But, this is the Cognos 10 BI part of the forum.  The code may not work in 10.2.

 


       
Twittear