19

I am using CellTable to show my records but now the thing is I want show a select box when user clicks on a cell. One more thing is that select box is my own widget, not a predefined. Can you please suggest to me any method of doing this?

Jesper Rønn-Jensen
  • 106,591
  • 44
  • 118
  • 155
vara kumar
  • 193
  • 1
  • 1
  • 4
  • Please [don't use signatures or taglines](http://stackoverflow.com/faq#signatures) in your posts. – user229044 Jan 29 '11 at 07:06
  • I have the same problem - I need to render some fairly heavyweight widgets (in terms of their logic) into cells in a table. What I wanted from CellTable was to be able to supply an IsWidget factory to be used to generate the widget for rendering each cell in a column. I can't see an easy way to do this but I'd rather not implement tables from scratch. – drdozer Jul 17 '12 at 15:38

5 Answers5

24

There's a post on the GWT google group that discusses the answer. Basically you create your custom widget as normal, and inside the render function you use widget.getElement().getInnterHTML().

@Override
public void render(com.google.gwt.cell.client.Cell.Context context,
            String value, SafeHtmlBuilder sb) {
        if (value != null) {
             MyWidget widget = new MyWidget(value);
             sb.appendEscaped(widget.getElement.getInnerHTML()); 
        }
}
John
  • 847
  • 2
  • 9
  • 16
  • 4
    Instead of using widget.getElement.getInnerHTML(), I'm using widget.getElement.getString(), because it includes the outer HTML. – Italo Borssatto Jul 12 '11 at 11:52
  • 1
    is it possible to reuse the same widget to render the HTML for each cell? (i should probably just write some code to find out..but just intrigued..) – HaveAGuess Nov 01 '11 at 13:29
10

This is an anti-pattern. The whole purpose of cells is so that you do NOT have widgets: you are supposed to "render" html directly in the cell.

Nick Hristov
  • 905
  • 1
  • 8
  • 17
  • why did they add UIBinder widget support to celltables then? (not exactly a citation but from the thread: http://groups.google.com/group/google-web-toolkit/browse_thread/thread/9cfb4a3b5c4af0be/b75305d15a1c32e8?lnk=gst&q=celltable+widget#b75305d15a1c32e8) – HaveAGuess Oct 31 '11 at 12:39
  • might be, but sometimes inevitable – jabal Feb 14 '12 at 07:57
  • I disagree, sometimes you don't want to bother with HTML, and just let GWT render widgets for you. – Rade_303 Mar 13 '15 at 10:09
7

There's a post on the GWT google group that discusses the answer. Basically you create your custom widget as normal, and inside the render function you use widget.getElement().getInnterHTML().

@Override
public void render(com.google.gwt.cell.client.Cell.Context context, String value, SafeHtmlBuilder sb) {
    if (value != null) {
         MyWidget widget = new MyWidget(value);
         sb.appendEscaped(widget.getElement.getInnerHTML()); 
    }
}

It works but there is a limitation:

  • You CAN'T attach any handler directly on your widget (outer or inner).

eg:

widget.addClickHandler(new ClickHandler() {
    @Override
    public void onClick(ClickEvent event) {
        // Won't work!!!
    }
});

or:

widget.getMyTextBox().addClickHandler(new ClickHandler() {
    @Override
    public void onClick(ClickEvent event) {
        // Won't work!!!
    }
});
Freddy Boucher
  • 1,387
  • 1
  • 16
  • 27
  • 1
    Is there a workaround for this limitation? I really want to implement such a funcitonality. – nanospeck Sep 30 '13 at 05:11
  • have a look at http://stackoverflow.com/questions/4691801/how-can-i-render-a-clickabletextcell-as-an-anchor-in-a-gwt-celltable look at the answer by 'thomas' – user949110 Oct 07 '13 at 22:20
  • Be aware the click event will be triggered whenever **anything** in the column is clicked. If you have two buttons in the column, you won't be able to distinguish between which is clicked. In this case refer to this answer - http://stackoverflow.com/questions/9108317/gwt-celltable-need-to-have-two-buttons-in-last-single-cell-of-each-row. – Ben Jul 21 '14 at 17:04
3

Some time ago I faced with the similar problem (tried to insert a custom widget into CellList cell), but unfortunately did not find an easy solution.

Generally, you can implement specific cell class, extending AbstractCell or ActionCell. In this case you will have to override render() method and implement your own rendering. Good example is given in AbstractCell class javadoc.

Kel
  • 7,680
  • 3
  • 29
  • 39
0

I think @Kel has given the closest answer , I use his answer and I found ActionCell can use IdentityColumn and CellTable Can use IdentityColumn

ActionCell<MyEntity> refreshCell = new ActionCell<>("Refresh", new ActionCell.Delegate<MyEntity>() {

  @Override
  public void execute(MyEntity entity) {
    //bla bla bla
  }
});
IdentityColumn<MyEntity> refreshColumn = new IdentityColumn<>(refreshCell);
cellTable.addColumn(refreshColumn, "Refresh");
taha
  • 731
  • 2
  • 7
  • 18