If you are unable to create a new account, please email support@bspsoftware.com

 

News:

MetaManager - Administrative Tools for IBM Cognos
Pricing starting at $2,100
Download Now    Learn More

Main Menu

javacript in tree prompt to scroll its members

Started by mederik, 04 May 2012 10:54:05 AM

Previous topic - Next topic

mederik

Hi

the goal of this javascript is to scroll all level 1 members of my tree prompt in a loop and select a default member according to a test. I want to use javascript because the default value is dynamic and not static

I start from an existing javascript which select a member in a tree prompt according to its position in the tree. This value is fixed into the code and is static. So if the position of my member default value changes, i'm dead  :-\ This is why I want to use a loop.

The starting script is this one :
<SCRIPT LANGUAGE=javascript>
var node = window.treeTime.getRootNode().getChildren()[window.treeTime.getRootNode().getChildren().length-2];
node.setSelected(true);
node.updateNodeSelection();
node.updateParent();
window.treeTime.setLastSelectedNode(node);
</script>


So as i'm not a m***** f***** killer in javascript I did a kind of "step by step" test that works fine. I get all the information I need. Here it is and sorry if not elegant :

<SCRIPT LANGUAGE=javascript>

/*ligne 165*/
/*string to look for*/
var rString= "1P]";

/*depth of the axis (# of level 1 members)*/
var opt= window.treeTime.getRootNode().getChildren().length;
alert('longeur : '+opt);

var node= window.treeTime.getRootNode().getChildren();
alert(node);

/*mon compteur*/
var i=-3

/*the code of the member tree prompt*/
var member = node[window.treeTime.getRootNode().getChildren().length+i].getValue();
alert('membre : '+member);

/*how many caracter in the member */
var longChaine = member.length;
alert('longeur membre : '+longChaine);

/*last 3 caracters for the test*/
var extract = member.substring(longChaine-3)
alert('substring membre : '+extract);

/*the test*/
IF (rString== extract)
alert('OK !!');
else
alert('bad shit !');


</script>


Now here is below, more or less, the same code but into a loop now. And........it doesn't work. I have a line 176 car.4 error (See attachment).

<SCRIPT LANGUAGE=javascript>

/*ligne 165*/
/*String to look for*/
var rString = "1P]";

/*depth of the axis (# of level 1 members)*/
var opt= window.treeTime.getRootNode().getChildren().length;
alert('longeur : '+opt);

FOR (i=1; i<=opt; i++)
{
   /*ligne 176 car 4 is the below*/
   member=window.treeTime.getRootNode().getChildren()[window.treeTime.getRootNode().getChildren().length+i].getValue();
   alert(member);
   longString = member.length;
   extract=member.substring(longString -3);
   IF (extract == rString )
   {
        window.treeTime.getRootNode().getChildren()[window.treeTime.getRootNode().getChildren().length+i]
    }
}
</script>


I have of course tried different ways of writing the code but each time I have a different error code. So i finally gave up with this one. I'm sure i'm not far away from the solution but I can't see how and why it doesn't work in the loop whereas it worked in the "step by step" try.

Your help and advice are very welcomed on that.

Mederik

CognosPaul

Hi Mederik,

I've been incredibly busy for the past year week so I missed this.  Although I posted a reply on my blog, I'll go into more detail here.

You almost have the js right, a few logic problems though.

window.treeTime.getRootNode().getChildren() will return an array, and the bracketed number returns the object at that position.  Arrays are 0 indexed, so the last object in the array is actually (array.length - 1). By doing window.treeTime.getRootNode().getChildren()[window.treeTime.getRootNode().getChildren().length+i] you're actually telling it to go i + 1 positions past the last object in the array. Convert it to window.treeTime.getRootNode().getChildren()[i]  and you're golden.

You may want to use getName() instead of getValue(). getName() will return the caption of the member, while getValue() returns the mun.

Also, once you find the value you're looking for, you should break out of the loop. Unless it's a multi-select tree prompt of course.
FOR (i=1; i<=opt; i++)
{
   /*ligne 176 car 4 is the below*/
   member=window.treeTime.getRootNode().getChildren()[i].getValue();
   alert(member);
   longString = member.length;
   extract=member.substring(longString -3);
   IF (extract == rString )
   {
        doStuff();
        break;
    }
}