1

I'm using GWT 2.4. When using a CellTable, I've seen its possible to add a column in which all of the cells are editable ...

final TextInputCell nameCell = new TextInputCell();
Column<Contact, String> nameColumn = new Column<Contact, String>(nameCell) {
  @Override
  public String getValue(Contact object) {
    return object.name;
  }
};
table.addColumn(nameColumn, "Name");

but what if I don't want every cell in the column to be editable, only specific ones, based on properties in my "Contact" object? How would I set this up? Thanks, - Dave

Dave
  • 15,639
  • 133
  • 442
  • 830

1 Answers1

2

The way I would do it is extend the TextInputCell and override the render method to render something else, if you don't want the value in that particular row editable.

Something like this:

public class MyTextInputCell extends TextInputCell {
  @Override
  public void render(Context context, String value, SafeHtmlBuilder sb) {
     YourObject object = getYourObject();
     if ( object.isThisCellEditable() ) {
        super.render(context,value,sb);
     } else {
        sb.appendEscaped(value); // our some other HTML. Whatever you want.
     }
  }
}

In the render method you have access to the cell's context. Context.getIndex() returns the absolute index of the object. I can't remember of the top of my wad right now, but if you do not provide a ProvidesKey implementation when creating your CellTable you will get one that will use the object itself as the key. So you can get the object using Context.getKey().

Strelok
  • 50,229
  • 9
  • 102
  • 115
  • Thanks for this, but I'm still missing something. In my column, I'm dealing with "Contact" objects. How do I get a reference to a Contact object in the MyTextInputCell? – Dave Nov 04 '11 at 14:33
  • I have defined a ProviderKey, but that only identifies how a particular Contact object is unique (i.e. returns its id). How do I use that to figure out how to get the object on which the id is based? Do I have to create a giant map of all my objects and then just reference the map? Seems like there would be a more elegant solution. Thanks, - – Dave Nov 04 '11 at 19:33
  • Have a look at http://google-web-toolkit.googlecode.com/svn/javadoc/latest/com/google/gwt/view/client/SimpleKeyProvider.html this ProvidesKey implementation returns the actual object as the key. So when you call Context.getKey() you will actually get you Contact object. – Strelok Nov 05 '11 at 13:18
  • Wow man,thanks for this!Five years later and you've made me say 'woah' outloud after it worked, THANK YOU! :) – Johnny Jun 20 '16 at 22:30