当用户将鼠标悬停在GWT-CellTable中的列标题时,有没有办法触发方法?

时间:2014-06-12 14:49:59

标签: gwt

好的,我有一个CellTable CellTable<List<String>> table = new CellTable<List<String>>();

&安培; nameColumn IndexedColumn nameColumn=new IndexedColumn(0)

然后table.addColumn(nameColumn, "Name");

我希望当用户mouseOver标题为nameColumn时,它会触发methodX

table.addCellPreviewHandler仅适用于表格内的单元格而不适用于列的标题或标题

table.addCellPreviewHandler(new Handler<List<String>>(){

        @Override
        public void onCellPreview(
                        CellPreviewEvent<List<String>> event) {

        if ("mouseover".equals(event.getNativeEvent().getType())) {
                        int col=event.getColumn();
                        int row=event.getIndex();
        //call sthing here
        }
});

是否有像table.addCellPreviewHandler这样的简单方法帮助我解决问题? 我不喜欢自定义标题或自定义列?

我试过了:

Header<String> nameColumnHeader = new Header<String>(new ClickableTextCell()) {
        @Override
        public String getValue() {
            return "Name";
        }

        @Override
        public final void onBrowserEvent(Context context, Element elem, NativeEvent event) {
         if ("mouseover".equals(event.getType())) {
           //
           meaningMessagesPopup.show();  
         }
         else if("mouseout".equals(event.getType())){
             meaningMessagesPopup.hide();
         }
        }
    };
table.addColumn(nameColumn, nameColumnHeader);

但似乎Gwt不承认"mouseover".equals(event.getType())

1 个答案:

答案 0 :(得分:0)

我们所有人有时会遇到“小部件”和事件的问题,所以不要担心。

问题是ClickableTextCell只有“click”和“keydown”的沉没事件,所以它不再听了。

通常在小部件中,您可以使用方法sinkEvents指定更多事件,但在这种情况下,该方法不存在。

从我的观点来看,我认为最简单的解决方案是创建自己的“ClickableTextCell”。我已经做到了,并且像魅力一样:

import static com.google.gwt.dom.client.BrowserEvents.CLICK;
import static com.google.gwt.dom.client.BrowserEvents.KEYDOWN;
import static com.google.gwt.dom.client.BrowserEvents.MOUSEOUT;
import static com.google.gwt.dom.client.BrowserEvents.MOUSEOVER;

import com.google.gwt.cell.client.AbstractSafeHtmlCell;
import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.text.shared.SafeHtmlRenderer;
import com.google.gwt.text.shared.SimpleSafeHtmlRenderer;


public abstract class MyClickableTextCell extends AbstractSafeHtmlCell<String> {

  /**
   * Construct a new ClickableTextCell that will use a
   * {@link SimpleSafeHtmlRenderer}.
   */
  public MyClickableTextCell() {
    this(SimpleSafeHtmlRenderer.getInstance());
  }

  /**
   * Construct a new ClickableTextCell that will use a given
   * {@link SafeHtmlRenderer}.
   * 
   * @param renderer a {@link SafeHtmlRenderer SafeHtmlRenderer<String>} instance
   */
  public MyClickableTextCell(SafeHtmlRenderer<String> renderer) {
    super(renderer, CLICK, KEYDOWN, MOUSEOVER, MOUSEOUT);
  }

  @Override
  public void onBrowserEvent(Context context, Element parent, String value,
      NativeEvent event, ValueUpdater<String> valueUpdater) {
    super.onBrowserEvent(context, parent, value, event, valueUpdater);
    if (CLICK.equals(event.getType())) {
      onEnterKeyDown(context, parent, value, event, valueUpdater);
    }
    if (CLICK.equals(event.getType())) {
      onEnterKeyDown(context, parent, value, event, valueUpdater);
    }
    if (MOUSEOVER.equals(event.getType())) {
      onMouseOver(context, parent, value, event, valueUpdater);
    }
    if (MOUSEOUT.equals(event.getType())) {
      onMouseOut(context, parent, value, event, valueUpdater);
    }
  }

  public abstract void onMouseOver(com.google.gwt.cell.client.Cell.Context context, Element parent,
      String value, NativeEvent event, ValueUpdater<String> valueUpdater);

  public abstract void onMouseOut(com.google.gwt.cell.client.Cell.Context context, Element parent,
      String value, NativeEvent event, ValueUpdater<String> valueUpdater);

  @Override
  protected void onEnterKeyDown(Context context, Element parent, String value,
      NativeEvent event, ValueUpdater<String> valueUpdater) {
    if (valueUpdater != null) {
      valueUpdater.update(value);
    }
  }

  @Override
  protected void render(Context context, SafeHtml value, SafeHtmlBuilder sb) {
    if (value != null) {
      sb.append(value);
    }
  }
}

正如您所看到的,创建对象时需要填充两个抽象方法。这些方法是在触发onMouseOver和onMouseOut时。

基本上,当您创建Header<String> nameColumnHeader时,您应该执行以下操作:

    MyClickableTextCell cl = new MyClickableTextCell() {

      @Override
      public void onMouseOver(Context context, Element parent, String value, NativeEvent event,
          ValueUpdater<String> valueUpdater) {
        GQuery.console.log("onMouseOver");
      }

      @Override
      public void onMouseOut(Context context, Element parent, String value, NativeEvent event,
          ValueUpdater<String> valueUpdater) {
        GQuery.console.log("onMouseOut");        
      }
    };

  //Specify the MyClickableTextCell to you Column's Header  
  Header<String> nameColumnHeader = new Header<String>(cl){
      ....
  }

  .... //Add the Column with the Header to the CellTable as normal.

快乐的编码!