Dec 20

To include a calculated field in a ListGrid you need to create a new ListGridField and specify the cellFormatter property to handle calculating the fields value.

// Following example shows a data source that return two fields
// firstName and lastName
// A CellFormatter is used to concatenate the first and last name
// in a single field in the ListGrid


DataSource peopleDS = new RestDataSource();
peopleDS.addField(new DataSourceTextField("firstName"));
peopleDS.addField(new DataSourceTextField("lastName"));

final ListGrid people = new ListGrid();
people.setDataSource(peopleDS);

// Create a new "Name" field in ListGrid

ListGridField name = new ListGridField("name", "Name");

// Specify a CellFormatter to calculate the fields value

name.setCellFormatter(new CellFormatter()
{
   public String format(Object value, ListGridRecord record,
      int row, int column)
   {
        // Get 'firstName' and 'lastName' values 
        // from record being displayed

        String firstName = record.getAttributeAsString("firstName");
        String lastName = record.getAttributeAsString("lastName");

        // return concatenated name

        return firstName + " " + lastName;
   }
});

// Add calculated field to the ListGrid

people.setFields(name);

written by objects \\ tags: , , ,