How to get the current bean being edited in a grid

I have an editor enabled grid, and I was trying to figure out how to get the bean from the container when it’s being edited by a user.

What I really want is to send the item’s changes that the user made after hitting save back to the database, but I can’t figure out how to access the bean? This is all I could figure out.

[code]
resultsGrid.getEditorFieldGroup().addCommitHandler(new FieldGroup.CommitHandler(){
@Override
public void preCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
personList.getIdByIndex(resultsGrid.getEditedItemId());
}
@Override
public void postCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException{

                }
            })

[/code]I think I can get it to return an object, but not the bean? If I could at least the the row’s data (Name and Num) or the row number it’s editing, maybe I could come up with something. But I’m really not sure what I’m doing. Does anybody have any hints?

Hi Alisa,

One way to retrieve the row’s data would be from the commit handler such as:

Grid grid = new Grid();

grid.addColumn("Name", String.class);
grid.addColumn("Num", Integer.class);

grid.addRow("Nicolaus Copernicus", 1543);
grid.addRow("Galileo Galilei", 1564);
grid.addRow("Johannes Kepler", 1571);

grid.setEditorEnabled(true);

grid.getEditorFieldGroup().addCommitHandler(new CommitHandler() {

  @Override
  public void preCommit(CommitEvent commitEvent) throws CommitException {
    Item item = commitEvent.getFieldBinder().getItemDataSource();
    String name = (String) item.getItemProperty("Name").getValue();
    Integer num = (Integer) item.getItemProperty("Num").getValue();

    System.out.println("Pre commit values: " + name + " " + num);
  }

  @Override
  public void postCommit(CommitEvent commitEvent) throws CommitException {
    Item item = commitEvent.getFieldBinder().getItemDataSource();
    String name = (String) item.getItemProperty("Name").getValue();
    Integer num = (Integer) item.getItemProperty("Num").getValue();

    System.out.println("Post commit values: " + name + " " + num);
  }
});

Additonally, if you are using a BeanItemContainer, then you can also get the bean through the commit event. For example, if we have the following bean object:[color=#5b7f8b]

[/color][code]
public class GridExampleBean {

    private String name;
    private int count;

    public GridExampleBean(String name, int count) {
        this.name = name;
        this.count = count;
    }

    public String getName() {
        return name;
    }

    public int getCount() {
        return count;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return String.format("Name: %s, count: %s", name, count);
    }
}

[/code]
Then in the commit event handler, you can access the bean itself, such as:

//...grid is created and populated

grid.getEditorFieldGroup().addCommitHandler(new CommitHandler() {

  @Override
  public void preCommit(CommitEvent commitEvent) throws CommitException {
    BeanItem item = (BeanItem) commitEvent.getFieldBinder().getItemDataSource();
    GridExampleBean bean = (GridExampleBean) item.getBean();

    System.out.println("Pre commit: " + bean.toString());
  }

  @Override
  public void postCommit(CommitEvent commitEvent) throws CommitException {
    BeanItem item = (BeanItem) commitEvent.getFieldBinder().getItemDataSource();
    GridExampleBean bean = (GridExampleBean) item.getBean();

    System.out.println("Post commit: " + bean.toString());
  }
});

Hope this helps,
Goran

Thank you so much for the clear explaination! That helped a lot.

Hi,

I’m trying to do the same thing with the TreeGrid in Vaadin 8, but the API seems to be different.
What would be the equivalent for V8?

I tried this, in the hope that I would get back the currently edited bean object:

        treeGrid.getEditor().addSaveListener(editorSaveEvent -> {
            LOG.debug(treeGrid.getEditor().getBinder().getBean().getName());
        });

But that throws a null error.

edit: ok I got it now, for some reason when I tried the lambda the IDE didn’t show me all available methods and I was kinda irritated why the editorSaveEvent wouldn’t give me access to the commited data. But of course it does.

        treeGrid.getEditor().addSaveListener(editorSaveEvent -> {
			LOG.debug(editorSaveEvent.getBean().getName());
        });

BR