简体   繁体   中英

Add action to a cancel button in an editable grid in vaadin

I'm working with an editable Grid with Vaadin 7. When a row is in edit mode, it shows two buttons: save and cancel.

在此输入图像描述

(Just in case, the image was taken from here Book of Vaadin )

With the:

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{}
});

I can do something in the save action. But, can I do something like that with the cancel action?

Thank you.

This is a serious deficiency of the component. According to the forum, they're working on it, but for the time being it seems that the most straightforward way is to extend the Grid component and override the doCancelEditor method. Here's a snippet:

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;

}

Note that you have to tell the MyGrid object when you create the row. Also, note that you're extending the server side, so you don't have to alter the client (widget code), but you do need to refer to the new component in your UI design.

Actually, saveEditor() should be also overridden, as doCancelEditor() seems to be invoked on save action, too. My code:

    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
    */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM