How can I put a widget in a CellTable Cell?
Asked Answered
H

5

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?

Hasdrubal answered 29/1, 2011 at 6:38 Comment(2)
Please don't use signatures or taglines in your posts.Optimum
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.Tremann
A
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()); 
        }
}
Antispasmodic answered 17/3, 2011 at 12:33 Comment(2)
Instead of using widget.getElement.getInnerHTML(), I'm using widget.getElement.getString(), because it includes the outer HTML.Snowblink
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..)Teaching
C
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.

Clymer answered 6/5, 2011 at 21:24 Comment(3)
why did they add UIBinder widget support to celltables then? (not exactly a citation but from the thread: groups.google.com/group/google-web-toolkit/browse_thread/thread/…)Teaching
might be, but sometimes inevitableMaleficence
I disagree, sometimes you don't want to bother with HTML, and just let GWT render widgets for you.Interlace
A
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!!!
    }
});
Amarillas answered 12/3, 2013 at 14:38 Comment(3)
Is there a workaround for this limitation? I really want to implement such a funcitonality.Drub
have a look at #4692301 look at the answer by 'thomas'Dortch
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 - #9108817.Waites
C
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.

Candlemaker answered 29/1, 2011 at 7:58 Comment(0)
S
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");
Serna answered 12/12, 2020 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.