我想将一个小部件附加到在GWT中使用TableCellBuilder构建的单元格。我该怎么做?

时间:2014-02-03 12:12:00

标签: gwt mouseclick-event gwtp

我按照以下方式声明并初始化了一个表

TableRowBuilder detailCell;
detailCell = startRow();
TableCellBuilder td = detailCell.startTD();

现在我按照以下方式声明并初始化了

Anchor removeAnchor = new Anchor(rowValue);
            removeAnchor.addClickHandler(new ClickHandler() {

                @Override
                public void onClick(ClickEvent arg0) {
                    Window.alert("clicked");
                }

            });

            td.html(new SafeHtmlBuilder().appendHtmlConstant(removeAnchor.toString()).toSafeHtml());
            detailCell.endTD();

因为我没有直接将该锚附加到单元格,所以没有处理click事件。我想要的是将锚点附加到单元格。我该怎么做?

2 个答案:

答案 0 :(得分:1)

如何使用JSNI

http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html#calling

    public void onModuleLoad() {
        exportStaticMethod();
        HTML html=new HTML("<a onclick='doAlert()'>hello</a>");
        RootPanel.get().add(html);
    }

    public final static void doAlert(){
        Window.alert("hello");
    }

    public static native void exportStaticMethod() /*-{
    $wnd.doAlert =
       $entry(@com.akjava.gwt.test2.client.GWTTest2::doAlert());
 }-*/;

答案 1 :(得分:1)

这不是CellTable的用例。

您可以为您的用例创建自定义Cell: http://www.gwtproject.org/doc/latest/DevGuideUiCustomCells.html

static class AnchorCell extends AbstractCell<String> {

    /**
     * The HTML templates used to render the cell.
     */
    interface Templates extends SafeHtmlTemplates {
      /**
       * The template for this Cell, which includes a balise.
       * 
       * @param value the safe value. Since the value type is {@link SafeHtml},
       *          it will not be escaped before including it in the template.
       *          Alternatively, you could make the value type String, in which
       *          case the value would be escaped.
       * @return a {@link SafeHtml} instance
       */
      @SafeHtmlTemplates.Template("<a href=\"javascript:;\">{0}</div>")
      SafeHtml cell(SafeHtml value);
    }

    /**
     * Create a singleton instance of the templates used to render the cell.
     */
    private static Templates templates = GWT.create(Templates.class);

    public AnchorCell() {
      /*
       * Sink the click and keydown events. We handle click events in this
       * class. AbstractCell will handle the keydown event and call
       * onEnterKeyDown() if the user presses the enter key while the cell is
       * selected.
       */
      super("click", "keydown");
    }

    /**
     * Called when an event occurs in a rendered instance of this Cell. The
     * parent element refers to the element that contains the rendered cell, NOT
     * to the outermost element that the Cell rendered.
     */
    @Override
    public void onBrowserEvent(Context context, Element parent, String value, NativeEvent event,
        ValueUpdater<String> valueUpdater) {
      // Let AbstractCell handle the keydown event.
      super.onBrowserEvent(context, parent, value, event, valueUpdater);

      // Handle the click event.
      if ("click".equals(event.getType())) {
        // Ignore clicks that occur outside of the outermost element.
        EventTarget eventTarget = event.getEventTarget();
        if (parent.getFirstChildElement().isOrHasChild(Element.as(eventTarget))) {
          doAction(value, valueUpdater);
        }
      }
    }

    @Override
    public void render(Context context, String value, SafeHtmlBuilder sb) {
      /*
       * Always do a null check on the value. Cell widgets can pass null to
       * cells if the underlying data contains a null, or if the data arrives
       * out of order.
       */
      if (value == null) {
        return;
      }

      // If the value comes from the user, we escape it to avoid XSS attacks.
      SafeHtml safeValue = SafeHtmlUtils.fromString(value);

      SafeHtml rendered = templates.cell(safeValue);
      sb.append(rendered);
    }

    /**
     * onEnterKeyDown is called when the user presses the ENTER key will the
     * Cell is selected. You are not required to override this method, but its a
     * common convention that allows your cell to respond to key events.
     */
    @Override
    protected void onEnterKeyDown(Context context, Element parent, String value, NativeEvent event,
        ValueUpdater<String> valueUpdater) {
      doAction(value, valueUpdater);
    }

    private void doAction(String value, ValueUpdater<String> valueUpdater) {
      // Alert the user that they selected a value.
      Window.alert("You clicked on " + value);

      // Trigger a value updater. In this case, the value doesn't actually
      // change, but we use a ValueUpdater to let the app know that a value
      // was clicked.
      valueUpdater.update(value);
    }
  }