TreeControl events

224 views
Skip to first unread message

Qamal Kosim-Satyaputra

unread,
Apr 14, 2010, 6:01:28 AM4/14/10
to Closure Library Discuss
Hi,

Has anyone ever played around with the tree control (goog.ui.tree.TreeControl)? How would you listen to click events on a specific TreeNode? getElement() will return null until the tree is rendered and is in the DOM so I cant really listen to the click event from that or any of the elements... also adding an event listener via 'addEventListener' doesn't work either...

I notice that goog.ui.tree.BaseNode.prototype.onClick_ calls preventDefault() on the event object, has this got anything to do with it failing? The only way I can trigger the listener is by hijacking the onClick_ method and dispatch the event using dispatchEvent(e)... But obviously this is not the correct way to do it since onClick_ is private and compilation would fail...

Any ideas?

Cheers,
Qamal

Nick Santos

unread,
Apr 14, 2010, 10:32:32 AM4/14/10
to closure-lib...@googlegroups.com

When using Closure ui components, you almost never want to listen for
browser events. You want to listen for the higher-level event
dispatched by the component. In this case, i think that the event
you're looking for is goog.events.EventType.CHANGE.

treeControl.addEventListener(
goog.events.EventType.CHANGE,
function() { ... });

does that give you what you need?

Erik Arvidsson

unread,
Apr 14, 2010, 10:41:57 AM4/14/10
to closure-lib...@googlegroups.com

Nick is correct. However if you do want to listen to click events add a listener to the tree element (use goog.events.listen), not the tree items, and when the user clicks anywhere in the tree element the event will propagate up to the tree element. You can then find the tree item element by using the "target" property of the event.

On Apr 14, 2010 7:32 AM, "Nick Santos" <nicks...@google.com> wrote:

On Wed, Apr 14, 2010 at 6:01 AM, Qamal Kosim-Satyaputra
<qama...@gmail.com> wrote:
> Hi,

> Has anyo...

When using Closure ui components, you almost never want to listen for
browser events. You want to listen for the higher-level event
dispatched by the component. In this case, i think that the event
you're looking for is goog.events.EventType.CHANGE.

treeControl.addEventListener(
   goog.events.EventType.CHANGE,
   function() { ... });

does that give you what you need?



--
To unsubscribe, reply using "remove me" as the subject.

Qamal Kosim-Satyaputra

unread,
Apr 14, 2010, 11:30:07 PM4/14/10
to closure-lib...@googlegroups.com
Hi,

Nick: It still doesn't trigger when you attach the event on the TreeNode...

On the other hand listening for CHANGE event on the root of the TreeControl works just like Erik said, but I cant get the TreeNode from e.target, e.target will always return the root TreeControl no matter which TreeNode is being clicked.

Any ideas?

Qamal Kosim-Satyaputra

unread,
Apr 15, 2010, 12:25:23 AM4/15/10
to closure-lib...@googlegroups.com
SOLVED!!!

I missed the getSelectedItem() in the documentation. The above methods were correct! Thanks guys.

Cheers
Reply all
Reply to author
Forward
0 new messages