One of my readers sent me an interesting problem. They need a checkbox prompt in which the top option is “All” and checking on any other option would automatically uncheck the “All” choice. Similarly, checking “All” should uncheck the other choices. Taking it to the conclusion, when checking or unchecking all of the options, the “All” should be checked.
Since they are still on 10.1, I have not used the Prompt API, meaning this should work on all versions since 8.4. To begin, the prompt itself.
In this case, the prompt is based on the Retailers hierarchy in the Sales and Marketing cube. Other times you might want to add a static value.
The JavaScript itself is not that difficult. The JS will loop through the prompt each time an option is clicked. If the first option is clicked, it will check it and uncheck the other options. If any other option is click it will loop through the prompt, counting the number of checked options and act accordingly. If 0 or all of the options are checked, it will check the first option, otherwise it will simply uncheck it.
When working with Checkbox prompts in JavaScript, the thing to remember is that, for whatever reason, the checkboxes that we see are actually images. To show the prompt as checked, the input needs to have the class “dijitCheckBoxChecked”.
Now the JS:
<script> /* * Function: addEvent * Author: Dan Fruendel * Attachs an event or adds an event listener depending on the browser. */ var addEvent = function(element, event, func){ if(element.addEventListener){ addEvent = function(element, event, func) { element.addEventListener(event, func, false); return true; }; } else if(element.attachEvent) { addEvent = function(element, event, func) { return element.attachEvent("on" + event, func); }; } else { addEvent = function(element, event, func) { var oldEventHandler = element['on' + event]; element['on' + event] = function() { //using .apply to pass on anything this function gets. if(typeof(oldEventHandler) === "function") { oldEventHandler.apply(element, arguments); } func.apply(element, arguments); } return true; }; } addEvent(element, event, func); } // Cognos form and namespace identifier. Don't touch. 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 attacher(elm,prompt,clicked){ var func = function() { //if all is selected, zero out everything else. if(clicked==0) {prompt[0].selected=true;prompt[0].checked=true;prompt[0].parentNode.className="dijitInline dijitCheckBox dijitCheckBoxChecked";for(var i=1;i<prompt.length;++i){prompt[i].selected=false;prompt[i].checked=false;prompt[i].parentNode.className="dijitInline dijitCheckBox"}} //if individual, count number of selected if(clicked>0) {var c=0; for(var i=1;i<prompt.length;++i){ if(prompt[i].checked){++c} } //if the count of checked is 0, then set the all to checked if(c==0) {setter=false;prompt[0].selected=true;prompt[0].checked=true;prompt[0].parentNode.className="dijitInline dijitCheckBox dijitCheckBoxChecked"} //if the count of checked is equal to the length of the prompt, then set the all to checked and uncheck everything else else if(c==prompt.length-1) {setter=false;prompt[0].selected=true;prompt[0].checked=true;prompt[0].parentNode.className="dijitInline dijitCheckBox dijitCheckBoxChecked"; for(var i=1;i<prompt.length;++i){prompt[i].selected=false;prompt[i].checked=false;prompt[i].parentNode.className="dijitInline dijitCheckBox"}} //if the count is one and less than the length of the prompt then just set all to unchecked; else if(c>0&&c<prompt.length-1){prompt[0].checked=false;prompt[0].selected=false;prompt[0].parentNode.className="dijitInline dijitCheckBox"} } canSubmitPrompt(); } addEvent(elm,'click',func) } var prompt=fW._oLstChoicesCountries; for (var i=0;i<prompt.length;++i){ attacher(prompt[i],prompt,i); } </script>
EDIT: An eagle-eyed reader noticed that the appearance of the checks are actually slightly different than they are when first rendered. The solution was to put a parentNode after prompt[i] when calling the className. Thanks Sue!
Checkbox Prompt - All option (1850 downloads)