onclick - GWT - How to retrive real clicked widget? -
i have onclick event on somepanel. , click on , works. but.. how retrive real click target? when click on panel inside od somepanel show me click on somepanel..
i know have this:
element e = element.as( event.getnativeevent().geteventtarget());
but returns element - want widget..
how this?
i use feature in gwtquery widget associated given element: https://code.google.com/p/gwtquery/wiki/gettingstarted#manipulating_your_widgets
widget = $(e).widget();
the problem element clicked couldn't element associated widget child. in case use gquery selectors traverse dom until parent widget based on css property.
// gwt widgets contains class .gwt- fail // use more accurate selector 1 in example widget = $(e).closest("[class*='.gwt-']")
if wanted yourself, taking method getassociatedwidget
in gquery
gives solution:
eventlistener listener = dom.geteventlistener(e); // no listener attached element, no widget exist element if (listener == null) { return null; } if (listener instanceof widget) { // gwt uses widget event listener return (widget) listener; }
edited: here have working example:
import static com.google.gwt.query.client.gquery.*; // panel widgets panel panel = new verticalpanel(); final html widget1 = new html("<span>foo</span> <span>bar</span"); final html widget2 = new html("<span>foo</span> <span>bar</span"); final html widget3 = new html("<span>foo</span> <span>bar</span"); panel.add(widget1); panel.add(widget2); panel.add(widget3); // need wrap our panel widget supporting click events focuspanel wrapper = new focuspanel(); wrapper.add(panel); rootpanel.get().add(wrapper); wrapper.addclickhandler(new clickhandler() { public void onclick(clickevent event) { // element not html widget clicked span element element e = event.getnativeevent().geteventtarget().cast(); // using gquery closest widget clicked element // take advanrage of html widgets having gwt-html class widget w = $(e).closest(".gwt-html").widget(); if (w == widget1) { window.alert("clicked on widget 1"); } else if (w == widget2) { window.alert("clicked on widget 2"); } else if (w == widget3) { window.alert("clicked on widget 3"); } else { window.alert("clicked on non gwt html widget"); } } });
Comments
Post a Comment