Thursday, 9 October 2014

Java Script for IBM Cognos Multi Select Tree Prompt

Restrict Multi Select Tree Prompt Different Levels


Introduction


This document describes a technique for Cognos report developers  wish to utilize a multi select Tree Prompt but require that the value submitted be restricted to a designated level within a hierarchy and user will get warning message in case of wrong selection.

Requirement


Requirement is that user should select value of a single level of hierarchy say either Month or Quarter but not Month and Quarter. This functionality is not available in Cognos.


This requirement can be fulfilled using Java Script.


Create simple list report.
Use GO Data Warehouse (analysis) -->Sales and Marketing (analysis)-->Advertising-->Time



Create a multi select optional Tree Prompt above the list report using same Time dimension. Drag two HTML items before and after tree prompt




Give the name of Tree Prompt as ‘T’.



Open first HTML item and put the below

HTML1:

<script>
function test()
{
                var tree =  window.treeT;
                var selectedNodes = getSelected(tree.getRootNode());
                var fist_val=selectedNodes[0];
                var len=selectedNodes.length;

                var bl=1;
                for (i=1;i<len;i++)
                                {
                                if(selectedNodes[i]==fist_val)
                                {
                                bl=1;
                                }
                                else
                                {
                                bl=0;

                                alert("You Can Not Select Two Different Levels");
                                break;
                               
                           

}
if(bl==0)
{
break;
}

                                }
if(bl==1)
{
promptButtonFinish();
}
               
}
function getSelected(node)
{
                if (!node) {
                                node = this.getRootNode();
                }

                var selectedNodes = new Array();

                if (node.hasSelectedChildren()) 
                {
                                //get any children
                                var children = node.getChildren();

                                //move recursively and find all children
                                for (var iChildCounter = 0; iChildCounter < children.length; iChildCounter++)
                                {
                                                var theSelectedNodes = this.getSelected(children[iChildCounter]);
                                                for (i=0;i<theSelectedNodes.length;i++)
                                                {
                                                                selectedNodes[selectedNodes.length] = theSelectedNodes[i];
                                                }
                                }
                }
                if (node.isSelected())
                {
                                selectedNodes[selectedNodes.length] = node.getLevel();
                }

                return selectedNodes;
};
</script>



Now open second HTML item and put the below mentioned code:

HTML2:

<input type="button" value="Submit" onClick="test()"/>


Now run the report, it will appear as below. Select Quarter value of any year and different Year value and click on Submit.
Pop up message will come



Now select any value of same level and click submit


The report runs and filters the report based on the tree prompt selection.

12 comments: