2015-07-10 25 views
1

我正在用Vaadin 7編輯網格。當某行處於編輯模式時,它顯示兩個按鈕:保存和取消。將操作添加到vaadin中的可編輯網格中的取消按鈕

enter image description here

(以防萬一,在拍攝時從這裏Book of Vaadin拍攝)隨着:

grid.getEditorFieldGroup().addCommitHandler(new CommitHandler() { 
    private static final long serialVersionUID = 1L; 
    @SuppressWarnings("unchecked") 
    @Override 
    public void preCommit(CommitEvent commitEvent) throws CommitException{} 
    @Override 
    public void postCommit(CommitEvent commitEvent) throws CommitException{} 
}); 

我可以做在攢動的東西。 但是,我可以使用取消操作來做類似的事嗎?

謝謝。

+1

你想要做什麼? – tk12

+1

不,FieldGroup沒有取消事件。也許實現你自己的EditorFieldGroup的擴展並在那裏處理它? –

+0

我正在添加一個新的空行,如果點擊取消按鈕,我想刪除它。 – Luislode

回答

3

這是一個嚴重的組件缺陷。根據論壇,他們正在研究它,但暫時看來,最直接的方法是擴展Grid組件並覆蓋doCancelEditor方法。這裏有一個片段:

public class MyGrid extends Grid { 

protected Object newRow; 

@Override 
protected void doCancelEditor() { 
    super.doCancelEditor(); 
    getContainerDataSource().removeItem(newRow); 
    setEditorEnabled(false); 

} 

public void setNewRow(Object newRow) { 
    this.newRow = newRow; 

} 

請注意,您必須在創建行時告訴MyGrid對象。此外,請注意,您正在擴展服務器端,因此您不必更改客戶端(窗口小部件代碼),但您需要在UI設計中引用新組件。

0

實際上,saveEditor()也應該被覆蓋,因爲doCancelEditor()似乎也在保存操作中被調用。我的代碼:

public class MyGrid extends Grid { 

     private boolean addingMode = false; 
     private JPAContainer<SomeEntity> container; 
     private Object recentlyAddedItemID; 

     public MyGrid(Indexed indexed) { 
      container = indexed; 
     } 

     @Override 
     protected void doCancelEditor() { 
      Object id = getEditedItemId(); 
      super.doCancelEditor(); 
      if (addingMode) { 
      getContainerDataSource().removeItem(id); 
      recentlyAddedItemID = null; 
      }    
      addingMode = false; 
     } 

     @Override 
     public void saveEditor() throws FieldGroup.CommitException { 
      if (addingMode) recentlyAddedItemID = getEditedItemId(); 
      addingMode = false;    
      super.saveEditor(); 
     } 

     public Object getRecentlyAddedItemID() { 
      return recentlyAddedItemID; 
     } 

     public void addNewElement(SomeEntity entity) { 
      addingMode = true;   
      editItem(container.addEntity(entity)); 
     }  
    } 

    MyGrid grid = new MyGrid(JPAContainerFactory.make(SomeEntity.class, entityManager));  
    grid.addNewElement(new SomeEntity()); 
    /* 
    if we want to know the new items's ID (actually the new primary key 
    in case of JPAContainer), we can check it by: 
    */ 
    Object id = grid.getRecentlyAddedItemID(); 
    /* 
    returns null if editor was cancelled and finally nothing new was persisted 
    */ 
相關問題