Header Ad

Monday, April 2, 2012

GWT CellTable - CellTable creation in GWT


The below is sample example for creating a CellTable in GWT.
/**
 * Entry point classes define onModuleLoad().
 */
public class CellTableExample implements EntryPoint {
  // A simple data type that represents a contact.
  private static class Employee {
    private final String firstName;
    private final String lastName;

    public Employee(String firstName, String lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
    }
  }

  // The list of data to display.
  private static List<Employee> EMPLOYEES = Arrays.asList(new Employee("John",
      "Smith"), new Contact("Mary", "Lawyer"), new Contact(
      "Zander", "Pod"));

  public void onModuleLoad() {

    // Create a CellTable.
    CellTable<Employee> table = new CellTable<Employee>();

    // Create firstName column.
    TextColumn<Employee> firstNameColumn = new TextColumn<Employee>() {
      @Override
      public String getValue(Employee employee) {
        return employee.firstName;
      }
    };

    // Make the name column sortable.
    firstName.setSortable(true);
    table.addColumn(firstName, "First Name");

    // Create lastName column.
    TextColumn<Employee> lastNameColumn = new TextColumn<Employee>() {
      @Override
      public String getValue(Employee employee) {
        return employee.lastName;
      }
    };
    table.addColumn(lastName, "Last Name");
    
    table.setRowCount(EMPLOYEES.size(), true);
    table.setRowData(0, EMPLOYEES);

   }

}


 The above example, will create a CellTable with column headings "First Name" and "Last Name". The EMPLOYEES list data will be pulled into the above table.

More information about CellTable with sortable columns and other features can be find here.
https://developers.google.com/web-toolkit/doc/latest/DevGuideUiCellTable

No comments: