She_rish She_rish - 7 months ago 194
Javascript Question

Find node clicked on jsTree click event to expand or collapse

I have 4 levels of tree:


- Reg1
-level 2
-level 3
-level 4
+level 2
+Reg2
level 2
level 3
level 4
level 2
+Reg3
level 2
level 3
level 4
level 2
+Reg4
level 2
level 3
level 4
level 2


I have implemented this using using
struts2 jQuery JSTree
plugin using JSON data.

If I select any child node (e.g.: Reg1 --> Level 2 --> level 3 --> level 4), when I try to collapse or expand the tree node which contains the selected child (level 4), it triggers the click event.

When I expand/collapse on the other nodes which are in a different hierarchy (Reg 2 or Reg 3 or Reg 4), then it won't trigger the click event.

Is there anyway we can capture which element is clicked on the click event, so that if it's an image with INS tag then I can skip my logic?

Answer

http://struts.jgeppert.com/struts2-jquery-showcase/index.action

Check out the More widgets -> Tree part

use the onClickTopics to catch the click event

Example

<sjt:tree 
            id="jsonTree" 
            href="%{treeDataUrl}"
            onClickTopics="treeClicked" 
        />

And you can get which element is clicked in the listen topic

$.subscribe('treeClicked', function(event, data) {
          var item = event.originalEvent.data.rslt.obj;
          alert('Clicked ID : ' + item.attr("id") + ' - Text ' + item.text());
    });