GWT - Clickhandler on a TextColumn in cellTable

2.5k views Asked by At

Is there a way to add a clickHandler to a Column in a cellTable in GWT ??

I do not see any option from the documentation for TextColumn.

My requirement goes this way - I have to display 5 columns of data in a cell table and one of the columns must have a onClick event to be fired. But I found no way to add a clickhandler to the textColumn.

if this was supposed to be done in regular html it would not take me 5 seconds to write the code -

1

There are 1 answers

0
Tom Miette On BEST ANSWER

Write your custom cell (here an example I wrote based on GWT ActionCell) :

public abstract class ActionTextCell<C> extends AbstractCell<C> {

  public static interface Delegate<T> {
    void execute(T object);
  }

  private final Delegate<C> delegate;

  public ActionTextCell(Delegate<C> delegate) {
    super("click", "keydown");
    this.delegate = delegate;
  }

  @Override
  protected void onEnterKeyDown(Context context, Element parent, C value, NativeEvent event,
      ValueUpdater<C> valueUpdater) {
    delegate.execute(value);
  }

  @Override
  public void onBrowserEvent(Context context, Element parent, C value, NativeEvent event, ValueUpdater<C> valueUpdater) {
    super.onBrowserEvent(context, parent, value, event, valueUpdater);
    if ("click".equals(event.getType())) {
      onEnterKeyDown(context, parent, value, event, valueUpdater);
    }
  }

  @Override
  public void render(Context context, C value, SafeHtmlBuilder sb) {
    sb.append(new SafeHtmlBuilder().appendHtmlConstant("<span>" + render(value) + "</span>")
      .toSafeHtml());
  }

  public abstract String render(C value);

}

And use it within an IndentityColumn which you add to your CellTable:

final ActionTextCell<MyClass> cell = new ActionTextCell<MyClass>(new ActionTextCell.Delegate<MyClass>() {

      @Override
      public void execute(MyClass c) {
        // do something with c
      }
    }) {

      @Override
      public String render(MyClass c) {
        // return a string representation of c
      }
    };

final IdentityColumn<MyClass> column = new IdentityColumn<MyClass>(cell);