How can I put a widget in a CellTable Cell?

后端 未结 5 2085
抹茶落季
抹茶落季 2021-02-04 03:35

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 prede

相关标签:
5条回答
  • 2021-02-04 03:42

    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()); 
            }
    }
    
    0 讨论(0)
  • 2021-02-04 03:46

    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");
    
    0 讨论(0)
  • 2021-02-04 03:51

    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.

    0 讨论(0)
  • 2021-02-04 03:54

    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!!!
        }
    });
    
    0 讨论(0)
  • 2021-02-04 04:01

    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.

    0 讨论(0)
提交回复
热议问题