Tuesday, April 12, 2016

Cognos Tree Prompt with Javascript on 10.2.1 (select all child on expand, select all child on parent selection and deselect all child on parent deselect)

Cognos Tree Prompt customization with Javascript 

Requirement:
1. When you expand a node, all the child nodes beneath it are checked and current node get deselected.
2. When you deselect the parent node, all the child nodes beneath it are deselected. 
3. When Select the parent node, all the child nodes beneath it are selected.
4. When any node selected or deselected its parent would be deselected.



Solution:
I have  copied some of code from below links to write my code.






Step 1: Create a tree prompt and give it the name “Time”. 
Step 2: Set Multiselect property of tree prompt to yes. 
Step 3: Drag an HTML item to the left of the tree prompt and add below code.

<div id='myTree'>
Step 4:  Drag HTML item to the right and add below code.

</div>

Step 5: Now drag one more html at right side finish button with below code

<script>
var idCounter=0;
 var nodeObj = new Array();
 var nodeId = new Array();
var nodeState = new Array();
document.getElementById('myTree').onmouseup=function(){runTree('Time')}; // replace div id and tree prompt name here
function runTree(id)
{
 t=setTimeout('checkTree("'+id+'")',200);
}
function checkTree(id)
{
  var tree =  window.treeTime; // replace tree prompt name here
  var startNode=tree.getRootNode();
  idCounter=0;
nodeObj[idCounter]=startNode.getChildren()[0];
 getchange();
 getNode(startNode);
}


function getNode(node)
{   // storing node and selection status
               if (node.hasChildren())  
                {
var children = node.getChildren();
for (var iChildCounter = 0; iChildCounter < children.length; iChildCounter++)
{
nodeId[idCounter]=children[iChildCounter].getName();
nodeState[idCounter]=children[iChildCounter].getState();
nodeObj[idCounter]=children[iChildCounter];
children[iChildCounter].setIndicator(''); // Setting indicator null to caputure user selection, a blank indicator with change state means user changes
idCounter++;
if(children[iChildCounter].hasChildren())
getNode(children[iChildCounter]); //recursive call to retrive next level
}
               }

}

function getchange()
{ //Caputure user selection
for(x=0;x<nodeObj.length;x++)
{
if(nodeObj[x].getLoading())  //check if node is loading its child.
{
nodeObj[x].setIndicator('100');   //set different indicator to identify new loading node.
t=setTimeout('getchange()',100);   //keep recalling the fuction till loading completed
break;

}
if(nodeObj[x].getIndicator()=='100' && nodeObj[x].hasChildren()) // if newly loaded node and having children(not the last node)
{
selectChild(nodeObj[x]);
break; //exit for loop if find selected or deselected node
}
else if (nodeObj[x].getState()!=nodeState[x] &&  nodeObj[x].getIndicator()!='101')
if (nodeObj[x].isSelected() && !nodeObj[x].hasSelectedChildren())
selectChild(nodeObj[x]);
else if (!nodeObj[x].isSelected() && !(nodeState[x]==0 && nodeObj[x].getState()==1))
deSelectChild(nodeObj[x]);
break; //exit for loop if find selected or deselected node
}
}
}

function selectChild(currentNode) 
{ // select all new retrived child
for(i=0;i<=currentNode.getChildren().length-1;i++)
{
      var node = currentNode.getChildren()[i];
      node.setSelected(true);
      node.updateNodeSelection();
      node.updateParent();
  node.setIndicator('101'); // set indicator to '101' to idenfie javascript selection and deselection (blank for user selection)
        window.treeTime.setLastSelectedNode(node); // replace tree prompt name here
  
}
// deselect parent node of new retrived child
if(currentNode.getIndicator()=='100')
{
currentNode.setSelected(false);
currentNode.updateNodeSelection();
currentNode.updateParent();
currentNode.setIndicator('101');
}
if(currentNode.getParent().getChildren().length>1)
{
currentNode.getParent().setSelected(false);
currentNode.getParent().updateNodeSelection();
currentNode.getParent().updateParent();
currentNode.getParent().setIndicator('101')
}
idCounter=0;
getNode(window.treeTime.getRootNode()); // replace tree prompt name here
}

function deSelectChild(currentNode) 
{ // deselect child node if parent deselected
for(i=0;i<=currentNode.getChildren().length-1;i++)
{
      var node = currentNode.getChildren()[i];
node.setSelected(false);
      node.updateNodeSelection();
      node.updateParent();
  node.setIndicator('101')
        window.treeTime.setLastSelectedNode(node); // replace tree prompt name here
}
if(currentNode.getParent().getChildren().length>1)
{
currentNode.getParent().setSelected(false);
currentNode.getParent().updateNodeSelection();
currentNode.getParent().updateParent();
currentNode.getParent().setIndicator('101')
}
}
</script>




2 comments: