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

 

[Solved] - Java script to select all the values of value prompt in Cognos 10.2.1

Started by xyz, 06 Jul 2017 08:46:15 AM

Previous topic - Next topic

xyz

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

qvixote

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.

xyz

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

BigChris


qvixote

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.

xyz

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

xyz

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

CognosPaul

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.

xyz

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

xyz

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

xyz

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

xyz

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

CognosPaul

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