English 中文(简体)
在显示复杂的超声望的GWT超声望中,我如何为具体的子模添加活动手稿?
原标题:In a GWT HTML widget displaying complex HTML, how do I add an event handler for specific subelements?
  • 时间:2011-11-17 18:37:56
  •  标签:
  • gwt

I m optimizing a GWT application that previously used a variety of nested panels to work with DIVs and Spans. I generate the entire table as a single SafeHtml object and then assigning it into a single SafeHtml widget.

I now want to be able to track mouseover/mouseout events at the level of the specific cell spans rather than the entire table, but I m not sure how to do this.

如果我向超文本植被本身增加一名手,我便会从各种因素中获取活动。

问题回答

Since 2.0 there is quite a simple way to do it. For example if you HTML code is contained in some kind of widget (HTMLPanel or HTML), you can calladdDomHandler(<handler>,<eventtyoe>) on that widget, so you will receive events from inner html. For example if you have a bunch of anchors inside HTMLPanel and you want to know which one was clicked you can do something like this:

panel.addDomHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            Element element=  event.getNativeEvent().getEventTarget().cast();
            if(element.getTagName().equals("A")) {
                AnchorElement anchor = element.cast();
                Window.alert("Anchor with href " + anchor.getHref() + " was clicked");
            }

        }
    }, ClickEvent.getType());

Since you want to track mouseover/out events you will have to use 2 different dom handlers, find out cell you need when event is fired and then change its state.

这样做的方法是:

  • Find the element you need with one of the DOM methods, like DOM.getElementById(..) or any other means. View Widget.getElement() etc.
  • Call DOOM.sinkEvents(element,eventBits) or DOM.sinkBitlessEvent(element,eventName) and pass the required events you want to sink in form of a bitmask, like Event.MOUSEEVENTS or using a named event like click or touchstart if using the second method.
  • 同样:

    DOM.setEventListener( element, new EventListener()
    {
    
        @Override
        public void onBrowserEvent( Event event )
        {
            if ("click".event.getType()) {
               // ..do stuff..
            }
        }
    } );
    

Only events you ve specified in step 2 will be fired to your EventListener, so you need to only handle those.





相关问题
Refresh the UI in gwt

I have created some custom composite widget, which listens to an events (in my case loginEvent). Once the event is caught the state of the widget changes so as the way it should look (in my case I ...

How to create a development/debug and production setup

I recently deployed inadvertently a debug version of our game typrX (typing races at www.typrx.com - try it it s fun). It was quickly corrected but I know it may happen again. After digging on ...

GWT error at runtime: ....style_0 is null

My code works fine in IE 8, but on firefox 3.5, the javascript fails to load. I used firebug to figure out why, and the error I get is (GWT detailed mode) My suspicion is that some style is not ...

GWT s hosted mode not working

I ve been stumped for a while trying to figure out why my GWT demo app isn t working in hosted mode so I went back and downloaded the Google Web Toolkit again, unzipped it and simply went to the ...

How to disable Google Visualizations Tool Tips?

Does anyone know how to disable the tool-tip boxes that popup when a Google Visualizations chart is clicked (Selected)? Following the sample code at Getting Started with Visualizations, the "...

GWT 2 CssResource how to

I have a GWT 1.7 application and I want to upgrade it to GWT 2 Milestone 2. The application uses 2 big external CSS files. In GWT 1.7 I had a public folder and put both the CSS files in the folder and ...

热门标签