tags:

views:

149

answers:

1

Hi,

I have a page pregenerated for me using html, it looks like a scrollable list of divs, something like:

<html>
  <div id="a">
     <div>Item A</div>
  </div>
  <div id="b">
     <div>Item B</div>
  </div>
</html>

I'd like to grab them in my entry point method, and add a click handler to each. I can't figure out how to do that. I have something like:

public void onModuleLoaded() {
    RootPanel rp1 = RootPanel.get("a");
    rp1.addClickHandler(...); // can't do this.
}

how can I listen for a click on one of those items in GWT? Is there a way I can just install a global click handler and just watch for the ID of clicked items and filter on that? I don't necessarily need to add a click handler for each element (and I think the docs recommend against this if you have many elements which require click handlers),

Thanks

Thanks

+1  A: 

I haven't tested this, but the general idea is right, and easy enough to extend for more than one target element. You might like to store the elements returned by DOM.getElementById() beforehand to keep things fast. Bear in mind that onPreviewNativeEvent() will be called for every user event, so keep it light.

Event.addNativePreviewHandler(new NativePreviewHandler() {
    public void onPreviewNativeEvent(NativePreviewEvent event) {
        if (Event.as(event).getTypeInt() == Event.ONCLICK &&
            DOM.isOrHasChild(DOM.getElementById("A"), Element.as(event.getEventTarget()))) {
            // Element 'A' was clicked.
        }
    }
}
hambend
This could work well too, also with just a general <div> with a complex inner structure which is good. I haven't tried it yet, but the inner test may look like (not gonna look nice here): if (event.getTypeInt() == Event.ONCLICK) { Element element = (Element)event.getSource(); if (element.getId().equals("blah")) { [will update post if it works, above] thanks!