0

I am trying to insert a textbox inside a datagrid column. I know I could have gone with TextCell or EditTextCell but for some reason I went with TextBox.

I went with the following approach

Cell<String> cell = new AbstractCell<String>()
    {

        @Override
        public void render( com.google.gwt.cell.client.Cell.Context context, String value, SafeHtmlBuilder sb )
        {
            FlowPanel fp = new FlowPanel();
            TextBox tb = new TextBox();
            tb.setText( value );
            fp.add( tb );
            sb.appendEscaped( fp.getElement().getString() );
        }
    };
    // Address.
    Column<ContactInfo, String> addressColumn = new Column<ContactInfo, String>( cell )
    {
        @Override
        public String getValue( ContactInfo object )
        {
            return object.getAddress();
        }
    };

The problem I encountered here is that I am getting the following in the UI instead of textbox

<div><input class="gwt-TextBox" type="text"></div>

But when I replaced

sb.appendEscaped( fp.getElement().getString() );

with

sb.appendHtmlConstant( fp.getElement().getInnerHTML() );

I am getting the textbox, But the value is not populating.

Can anyone explain this behaviour. How can I populate the value?

Abhijith Nagaraja
  • 3,370
  • 6
  • 27
  • 55

2 Answers2

1

Don't create a FlowPanel and a TextBox widget. Instead:

sb.appendHtmlConstant("<input class=\"gwt-TextBox\" type=\"text\" value=\"" + myValue + "\"></input>");

where myValue is what you want to see in a textbox.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • Thanks for the reply. This will become a specific solution for textbox. How to generalize it? – Abhijith Nagaraja Sep 14 '12 at 03:40
  • 1
    Why would you need to generalize it? There are already many useful cells available for checkboxes, textboxes, lists, images, buttons, etc. They all have additional functionality that you lose if you simply add HTML to an AbstractCell. If you don't add any special features, use one of those cells. And if you add custom features, you cannot generalize it. – Andrei Volgin Sep 14 '12 at 18:17
-1

Why don't you use TextInputCell http://www.gwtproject.org/javadoc/latest/com/google/gwt/cell/client/TextInputCell.html ?