Loading ...
Sorry, an error occurred while loading the content.

Re: [ydn-javascript] How to get the selected node of a Tree?

Expand Messages
  • Eric Miraglia
    Samba, TreeViews do not really have a concept of a selected index -- only the concept of a node that is being expanded, contracted, or clicked on. You can
    Message 1 of 1 , Dec 4, 2006

      TreeViews do not really have a concept of a "selected index" -- only the concept of a node that is being expanded, contracted, or clicked on.  You can track the last-expanded or last-contracted node in your implementation, but the TreeView Control doesn't automatically do that for you.

      Adding a click event for a node is easy to do, but remember that node's aren't monolithic units — there are clicks on their expand/contract icons, clicks on their labels, and also (when using HTMLNodes) there can be clicks on custom HTML within a node.

      1.  To listen for a click that expands/contracts the node specifically: Use the expand/collapse Custom Events on your Treeview instance: myTree.subscribe("expand", function(node) { //...handle click here...};)

      2. To listen for a click on the lable of a TextNode or MenuNode: Use the labelClick Custom Event on your TreeView instance:  myTree.subscribe("expand", function(node) { //...handle click here...};)

      3. To listen for a click within the HTML of an HTMLNode:  You have many options here.  You can write your onclick handler into the HTML (<div onClick="myFn();"></div>) or you can provide an id attribute for the elements you put in those nodes and use Event Utility to connect event handlers to them.  You might also want to use event delegation to handle this globally; see this message from an earlier thread about how to do that.  

      One last point on this: If you want to listen for a click anywhere within a node, you can attach a click handler to the root element for the node; that element is accessible to you via the node instance's getEl() method:

      var myNode = new YAHOO.widget.TextNode("mylabel", root, false);
      YAHOO.util.Event.addListener(myNode.getEl(), "click", myfn);   


      Eric Miraglia
      Yahoo! Presentation Platform Engineering

      On Dec 4, 2006, at 1:20 AM, chandhrudu wrote:

      Hi everybody,

      Can any one tell me how to get the selected index of a tree ?

      I need to know how to add an event to a click
      event on a node!

      Could you please help me?
      Thanking you in advance,

    Your message has been successfully submitted and would be delivered to recipients shortly.