2011-06-29 59 views

回答

2

這個簡單的例子適合我。既然你沒有提供任何代碼或進一步解釋你想要達到什麼目的,我不知道我的例子是否有幫助。

public void onModuleLoad() { 
    CellTable<Person> table = new CellTable<Starter.Person>(); 

    List<HasCell<Person, ?>> cells = new LinkedList<HasCell<Person, ?>>(); 
    cells.add(new HasCellImpl("first name", new Delegate<Person>() { 

     @Override 
     public void execute(Person object) { 
      Window.alert(object.getFirstName()); 
     } 
    })); 
    cells.add(new HasCellImpl("last name", new Delegate<Starter.Person>() { 

     @Override 
     public void execute(Person object) { 
      Window.alert(object.getLastName()); 
     } 
    })); 

    CompositeCell<Person> cell = new CompositeCell<Person>(cells); 
    table.addColumn(new TextColumn<Starter.Person>() { 

     @Override 
     public String getValue(Person object) { 
      return object.getFirstName() + " " + object.getLastName(); 
     } 
    }, "name"); 

    table.addColumn(new Column<Person, Person>(cell) { 

     @Override 
     public Person getValue(Person object) { 
      return object; 
     } 
    }, "composite"); 

    LinkedList<Person> data = new LinkedList<Starter.Person>(); 
    data.add(new Person("Amy", "Reed")); 
    data.add(new Person("Tim", "Gardner")); 
    table.setRowData(data); 

    RootPanel.get().add(table); 
} 

private class HasCellImpl implements HasCell<Person, Person> { 
    private ActionCell<Person> fCell; 

    public HasCellImpl(String text, Delegate<Person> delegate) { 
     fCell = new ActionCell<Person>(text, delegate); 
    } 

    @Override 
    public Cell<Person> getCell() { 
     return fCell; 
    } 

    @Override 
    public FieldUpdater<Person, Person> getFieldUpdater() { 
     return null; 
    } 

    @Override 
    public Person getValue(Person object) { 
     return object; 
    } 
} 

private class Person { 
    private String fFirstName; 
    private String fLastName; 

    public Person(String first, String last) { 
     fFirstName = first; 
     fLastName = last; 
    } 

    public String getFirstName() { 
     return fFirstName; 
    } 

    public String getLastName() { 
     return fLastName; 
    } 
} 
相關問題