2

I have a GWT project which will have several DataGrids. SO far I have implemented one (with an ASyncDataProvider) and it's working just fine. But I really don't like all the messy, boiler-plate code to specify the columns. Here's a sample:

    //some column
    Column<DataRecord, String> dataColumn = new Column<DataRecord, String>(new TextCell()) {
        public String getCellStyleNames(Context ctx, DataRecord record) {
            //some columns have a style based on the value, calculate style here
        }
        public String getValue(DataRecord record) {
            return record.getProperty();
        }
    };
    dataGrid.addColumn(dataColumn);
    dataGrid.setColumnWidth(dataColumn, 25, Unit.PX);

Is there a way to reduce the amount of code? Imagine a table with a dozen or so columns, the code gets very tedious. The grid itself is rendered by UiBinder but it seems UiBinder cannot go as far as specifying columns on a DataGrid.

I've had a look at this: Defining GWT CellTables with UiBinder but that involves using an HTML Table, but I don't want to lose all the functionality provided by DataGrid, so I'm reluctant to take that route.

Any other suggestions would be welcomed.

Community
  • 1
  • 1
NickJ
  • 9,380
  • 9
  • 51
  • 74

1 Answers1

0

Use class MyColumn like

private class MyColumn extends Column<DataRecord, String>
{
  private String field;
  public MyColumn(String field)
  {
    super(new TextCell());
    this.field = field;
  }

  public String getValue(DateRecord rec)
  {
     if (this.field.equals("prop1")
         return rec.getProp1();
     else if (this.field.equals("prop2")
         return rec.getProp2();
     else
        return "ERROR";
  }
}  

and then

dataGrid.addColumn(new MyColumn("prop1"));
dataGrid.addColumn(new MyColumn("prop2"));

Ugly, but works. To make it nicer you can use some reflection toolkit which works with GWT.