COGNOiSe.com - The IBM Cognos Community

IBM Cognos 10 Platform => Cognos 10 BI => Report Studio => Topic started by: xyz on 06 Jul 2017 08:46:15 AM

Title: [Solved] - Java script to select all the values of value prompt in Cognos 10.2.1
Post by: xyz on 06 Jul 2017 08:46:15 AM
Hi All,

We have a requirement to select all the values of value prompt by default using a java script. We are using Congos 10.2.1 version. I tried searching in google but of no use.

Can some one please help me with the java script.

Thanks & Regards,
XYZ
Title: Re: java script to select all the values of value prompt in Cognos 10.2.1
Post by: qvixote on 06 Jul 2017 08:58:37 AM
I think this question may be better placed on a javascript forum. Anyway, you could do it better using jquery. But selecting all the values of a value prompt will have no effect in the report, even if the value prompt is set to send data automatically.
Title: Re: java script to select all the values of value prompt in Cognos 10.2.1
Post by: xyz on 06 Jul 2017 09:29:27 AM
Hi qvixote,

Thanks for the reply, I am using Cognos 10.2.1 and I am not sure jquery would work there.
I want to have a java script code to put in HTML item, to select all the values of the value prompt list box by default.

Can someone, if they have the code, can you guys please paste the code here.

Thanks & Regards,
XYZ
Title: Re: java script to select all the values of value prompt in Cognos 10.2.1
Post by: BigChris on 06 Jul 2017 09:52:04 AM
Can't you just make the prompt / filter optional?
Title: Re: java script to select all the values of value prompt in Cognos 10.2.1
Post by: qvixote on 06 Jul 2017 09:59:37 AM
I use Cognos 10.2.1 too and I use jQuery in my reports.  ;)

To include jQuery, download the compressed file from here: https://code.jquery.com/jquery-3.2.1.min.js

Then open the file with your favourite text editor, copy the full code and paste it in a HTML element on your report, making sure you sorround the code with <script> and </script>.

<script>
/*! jQuery v3.2.1 | (c)....
....
</script>


Now, report studio uses '$' in his own javascript functions, so you need to make a little change on the way you use jQuery in report studio. To do so, include the following code in another HTML element:


<script>
// Give back '$' control to Report Studio scripts.
var jq;
jq = jQuery.noConflict(true);

// Now you need to use 'jq()' instead of '$()'.

jq(document).ready(function() {


    //Your jQuery code here

});
</script>



With this, you can use jQuery.

Now, for your actual need, you may add an HTML element before the value prompt with the code:

<span id="listId">

And another one after the value prompt, with the code:

</span>

Then, go to the HTML element where we give back '$' to report studio and modify the code adding some lines to do what you want, like this:


<script>
// Give back '$' control to Report Studio scripts.
var jq;
jq = jQuery.noConflict(true);

// Now you need to use 'jq()' instead of '$()'.

jq(document).ready(function() {

    //Your jQuery code here
    jq('#listId select option').prop('selected', true);

});
</script>



I hope it can help you.
Title: Re: java script to select all the values of value prompt in Cognos 10.2.1
Post by: xyz on 07 Jul 2017 06:39:31 AM
Thank you very much for replies BigChris and qvixote.

We have suggested the same to the client, but some how client wanted to have all the values selected by default in value prompt.

qvixote, thank you for spending the time and giving the jquery code, I took the code and did what is suggested in the email. I don't see any change, all the values of value prompt are not selected yet.


Thanks & Regards,
XYZ
Title: Re: java script to select all the values of value prompt in Cognos 10.2.1
Post by: xyz on 07 Jul 2017 06:41:40 AM
I have tried the below code which is working with only one selected value, I know I have to loop the value prompt but some how I am not able to achieve it.

<script type="text/javascript">
function init()
{
var fW = (typeof getFormWarpRequest == "function" ? getFormWarpRequest() : document.forms["formWarpRequest"]);
if (fW)
fW._oLstChoicesPL.options[0].selected = true;
}
init();
</script>



Thanks & Regards,
XYZ
Title: Re: java script to select all the values of value prompt in Cognos 10.2.1
Post by: CognosPaul on 07 Jul 2017 08:10:11 AM
in any version after 10.2, selecting all values is really easy:


<script>
var paulScripts ={}
  , oCR = cognos.Report.getReport("_THIS_");

paulScripts.getControl = function(promptName) {
  return oCR.prompt.getControlByName(promptName);
};

paulScripts.selectAll = function(promptName){
  paulScripts.getControl(promptName).setValues(paulScripts.getControl(promptName).getValues(true)) 
};


As this is using the Prompt API, IBM will support it.
Title: Re: java script to select all the values of value prompt in Cognos 10.2.1
Post by: xyz on 07 Jul 2017 09:20:58 AM
Hi Paul,

I don't know for what reason attested code is not working, I just replaced the promptName with the name I provided to value prompt name property and put the HTML item after the value prompt. One more thing, I am using this report in the workspace, where my older code was working in the report viewer, but when placed in Congos workspace it is not working. So I used the code like below


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

Which is working fine, even If i use the my old report is workspace.



Below code is latest code provided in the email with just the replaced with the prompt name value.


<script>
var paulScripts ={}
,oCR = cognos.Report.getReport("_THIS_");

paulScripts.getControl = function(PL) {
  return oCR.prompt.getControlByName(PL);
};

paulScripts.selectAll = function(PL){
  paulScripts.getControl(PL).setValues(paulScripts.getControl(PL).getValues(true)) 
};

</script>



Below code is the old code, where just added formWarpRequest_NS_ to make it work in workspace as well.


<script type="text/javascript">
function init()
{
var fW = (typeof getFormWarpRequest == "function" ? getFormWarpRequest() : document.forms["formWarpRequest"]);
if (!fW || fW == undefined) {
  fW = (formWarpRequest_THIS_ ? formWarpRequest_THIS_:formWarpRequest_NS_);
}
var list = fW._oLstChoicesPL;
list.selectedIndex = 0;

}
init();
</script>
<body onload=init()>


Can you please do please let me know where I am doing wrong or do we make changes to the old code itself, whatever you suggest.

Thanks & Regards,
XYZ
Title: Re: java script to select all the values of value prompt in Cognos 10.2.1
Post by: xyz on 11 Jul 2017 09:07:53 AM
Hi All,

Does any one found the java script to select all the values of a value prompt list box UI by default?


Thanks in advance.


Thanks & Regards,
XYZ
Title: [Solved] - Java script to select all the values of value prompt in Cognos 10.2.1
Post by: xyz on 13 Jul 2017 04:49:37 AM
Hi Guys,

I found the solution, whatever Paul has provided is working, I was doing some silly mistake, below is the code which will select all the values of the value prompt by default. Name of the prompt I gave as 'Region' and put the HTML item after the value prompt and pasted the below code in the HTML item. It is working as expected.


<script>
var paulScripts ={}
  , oCR = cognos.Report.getReport("_THIS_");

paulScripts.getControl = function(promptName) {
  return oCR.prompt.getControlByName(promptName);
};

paulScripts.getControl('Region').setValues(paulScripts.getControl('Region').getValues(true));
</script>


One thing I have observed, IBM has removed all the old java script related solutions from their repository, I guess.

Thanks & Regards,
XYZ
Title: Re: [Solved] - Java script to select all the values of value prompt in Cognos 10.2.1
Post by: xyz on 14 Jul 2017 07:03:48 AM
Thank you very much Paul for the script.

I have one small requirement in it, once all the values of the value prompt are selected, I have to auto submit the page once, it should work even in Cognos Worspace.
I have the command but not quite sure, how to check all the status of the values selected.

If I use the below code the prompt is keep on auto submitting.


<script>
var paulScripts ={}
  , oCR = cognos.Report.getReport("_THIS_");

paulScripts.getControl = function(promptName) {
  return oCR.prompt.getControlByName(promptName);
};

paulScripts.getControl('Region').setValues(paulScripts.getControl('Region').getValues(true));

[color=red]setTimeout('oCVRS.promptAction(\'finish\')', 10);[/color]
</script>




I know it must be small condition, but I am missing on something.
Your help is much appreciated.

Thanks & Regards,
XYZ
Title: Re: [Solved] - Java script to select all the values of value prompt in Cognos 10.2.1
Post by: CognosPaul on 19 Jul 2017 04:47:44 PM
I would set sessionStorage to check if it's the first run, and only run if true.


if(!sessionStorage.getItem('secondaryRun')) {
  sessionStorage.setItem('secondaryRun',1);
  oCVRS.promptAction('finish')
}