COGNOiSe.com - The IBM Cognos Community

IBM Cognos 10 Platform => Cognos 10 BI => Report Studio => Topic started by: paul5950 on 30 Dec 2013 03:29:37 PM

Title: Multi-select prompt with minimum number of selections
Post by: paul5950 on 30 Dec 2013 03:29:37 PM
Hi,

Is there a way to require users to select at least 3 values from a multi-select prompt?  What are the steps to accomplish this? Note that this prompt cannot be made required because not every user will select this particular option to filter the report by.


Title: Re: Multi-select prompt with minimum number of selections
Post by: bonniehsueh on 02 Jan 2014 08:46:06 PM
It sounds like you are looking for browser side validation to validate that the user selects at least 3 values.

Cognos unfortunately does not have anything 'out of the box' that can achieve exactly what you are looking for. However, JavaScript is good for this type of validation.  I'll need to look into something like this for a project of mine. My requirement is to not allow users to select more than 5 selections in a multi-select value prompt. Please keep us updated with any findings you have! Good luck! :)
Title: Re: Multi-select prompt with minimum number of selections
Post by: yogeswari on 03 Jan 2014 05:45:47 AM
Hi Paul,

please try the below coding:
<script>
var form=getFormWarpRequest();
var listb=form._oLstChoicesxyz;
var count=0;
var lengthy=listb.options.length;
if (lengthy<=2)
{
alert("There are lesser than 3 items in the list");
}
for(i=0;i<lengthy;i++)
{
if(listb.options.selected==true)
{
count=count+1;
}
}
if(count<3)
{
alert("Minimum selection should be 3");
}
else if(count>=3)
{
alert("you have selected 3 or more items");
}
</script>

Place an html item next to the value prompt and place the above code. Hope this helps you.

thanks,
yogeswari.
Title: Re: Multi-select prompt with minimum number of selections
Post by: navissar on 05 Jan 2014 11:54:46 PM
Well, the answer changes according to whether you're using Cognos 10.2 and up or not.
Cognos 10.2 has a prompt API that makes the whole thing pretty simple and neat. For example, you could throw in an HTML Item above your prompt with the following:
<span id="msg" style="color:red"></span>
Then name your prompt (I used "CBP" as name).
And then another HTML Item anywhere after the prompt with this:
<script>
var promptScripts={};
promptScripts.setCheckBoxValidator=function(){
var oCR=cognos.Report.getReport("_THIS_");
//get the prompt
var theCheckBoxPrompt = oCR.prompt.getControlByName("CBP");
theCheckBoxPrompt.setValidator(
function(){
var oCR=cognos.Report.getReport("_THIS_");
//get the prompt
var theCheckBoxPrompt = oCR.prompt.getControlByName("CBP");
//get the values
var values=theCheckBoxPrompt.getValues();
//get the Span
var theSpan=document.getElementById("msg");
if(values.length<3){
theSpan.innerText="At least three values need to be selected for this prompt";
return false;

}
else{
theSpan.innerText="";
return true;
}

}

);
}
promptScripts.setCheckBoxValidator();
</script>

This will take care of everything.
If you're pre-10.2, you'll have to go about it a different way. yogeswari's script is in the right direction, but it doesn't have a trigger (nothing sets it off) and it doesn't handle your finish button.
What I would do is I'd change the finish button's function to include a validator like this:

<script>
var fW = (typeof getFormWarpRequest == "function" ? getFormWarpRequest() : document.forms["formWarpRequest"]);
if ( !fW || fW == undefined)   
{
fW = ( formWarpRequest_THIS_ ? formWarpRequest_THIS_ : formWarpRequest_NS_ );
}
var preFix = "";
if (fW.elements["cv.id"])
{
preFix = fW.elements["cv.id"].value;
}     
var nameSpace = "oCV" + preFix;
function onFinishValidateAndFinish(){
var thePrompt=fW._oLstChoicesCBP;
var index=0;
//loop through prompt and count checked items
for(var i=0;i<thePrompt.length;i++){
if(thePrompt[i].checked){
index++;
}
}
if(index<3){
alert("Hey, you have to select more than three values in the prompt! What's wrong with you?!");
}
else{
window[nameSpace].promptAction("finish");
}
}
//get Finish Button
var allButtons=document.getElementsByTagName("button");
var theButton;
for(var i=0;i<allButtons.length;i++){
if(allButtons[i].name.indexOf("finish")==0){
theButton=allButtons[i];
break;
}
}
theButton.onclick=function(){onFinishValidateAndFinish();}
</script>



This script needs to go in an HTML Item that's located AFTER THE FINISH BUTTON
(Obviously you want to change the alert text).
Another way to go at it in versions prior to 10.2 is to add an onclick event to the prompt itself, that also takes care of disabling the Finish button, but it's slightly more complex. if you want I can set you up with a working sample, in case the two here won't work for you...  ;)
Title: Re: Multi-select prompt with minimum number of selections
Post by: lalitha.nov20 on 14 Mar 2014 01:17:48 AM
Hi,

My requirement is something like this, but i need to disable the finish button until the selection is done. Can you help me with the code.

I have to only select one value in the select and search prompt. Once the value is selected , i need to enable the finish button for the user.

Thank you,
lalitha
Title: Re: Multi-select prompt with minimum number of selections
Post by: navissar on 14 Mar 2014 02:31:34 AM
If I understand your scenario correctly, you don't need a script.
You have a search&select prompt where the user has to select only one value, and only after one value is selected is the finish button enabled. Just make your prompt required and single select.