There are many reasons why you may want to interact with a Tree Prompt with JavaScript. Maybe you want to enable the finish button if a member on the bottom level is selected, or to select the last member, or to ensure only 5 members are selected.
This post isn’t to detail every possible scenario, but to detail the some functions available and how to use them. It’s important to note that I am hardly a JavaScript expert, so there may be better ways to do anything I say here.
First you need to identify your tree prompt. Unlike most prompt controls, where the identifier changes based on the type viewer being used, the tree prompt can be called using window.treePROMPTNAME. Unfortunately we can’t apply an onmousedown event to the tree prompt, so we have to wrap it in a div.
Createa tree prompt and give it the name “Time”. Drag an HTML item to the left of the tree prompt
<div id='myTree'>
and an HTML item to the right
</div>
.
Now we can attach an event to capture the clicks:
<script> document.getElementById('myTree').onmousedown=function(){runTree('Time')}; </script>
Any click inside that div will now trigger the runTree function passing ‘Time’ as an argument.
Because there are a number of JavaScript functions are run upon selecting an element we can’t immediately get the value of the element. So we can use the setTimeout function to wait 200 milliseconds before getting the data.
<script> function runTree(id) { t=setTimeout('checkTree("'+id+'")',200); } </script>
After 200 milliseconds the checkTree function will run, also passing Time as the argument.
<script> function checkTree(id) { selectedTreeNode = window['tree'+id].getLastSelectedNode(); if(!selectedTreeNode) {return} alert(selectedTreeNode.getName()); alert(selectedTreeNode.getValue()); alert(selectedTreeNode.getLevel()); } </script>
The checkTree function will now alert the selected elements Name, MUN, and Tree level. Note the Tree Level is from the tree prompt, not the member’s hierarchy level. But knowing these, we can then call other functions. You could check the level number of the selected element and enable or disable the tree prompt while popping up a message.
You can programmatically set the default value of the tree prompt using JavaScript. Unfortunately it appears it is only possible to do this on the first level.
<script> var node = window.treeTime.getRootNode().getChildren()[window.treeTime.getRootNode().getChildren().length-1]; node.setSelected(true); node.updateNodeSelection(); node.updateParent(); window.treeTime.setLastSelectedNode(node); </script>
This will only effect the prompt after the page has been loaded. Prompt pages should be fine, but prompts on the report page will need to have a default value set in the prompt macro.
I learned about these functions by going through the js file associated with tree prompts. Check out ..webcontentpromptingCTreeIE5NS6.js for more Tree Prompt functions.
It worth noting that these functions are written by IBM, and are liable to change on upgrade. I’d be interested in hearing if these work in any of the previous versions of Cognos.