Header Ad

Tuesday, April 10, 2012

GWT DataGrid - DataGrid creation in GWT


The below will give the brief view of creating or loading a DataGrid in GWT page with data. The class "DataGridExample" has a "Employee" inner class to define the data for loading in "DataGrid".

The EMPLOYEES is the ArrayList having "Employee" class objects. An instance with name "dataGrid" has been created.Two text columns "First Name" and "Last Name" are created and added to the DataGrid.

Finally, you can see the below piece code in the "DataGridExample" class.
    dataGrid.setRowCount(EMPLOYEES.size(), true);
    dataGrid.setRowData(0, EMPLOYEES);

This will define to set the data to the dataGrid with row count.


/**
 * Entry point classes define onModuleLoad().
 */

public class DataGridExample 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  Employee("Mary", "Lawyer"), new  Employee(
      "Zander", "Pod"));

  public void onModuleLoad() {

    // Create a CellTable.
    DataGrid<Employee> dataGrid = new DataGrid<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);
    dataGrid.addColumn(firstName, "First Name");

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

   }

}

1 comment:

Unknown said...

You have just an error ...

dataGrid.addColumn(firstNameColumn, "First Name");

dataGrid.addColumn(lastNameColumn, "Last Name");


I think you should test your code before posting
But thanks for the example.