使用新的用户名编辑列单元格后,当我尝试单击save
时,我无法获得新的单元格值,而是获得了旧的值。
grid.getEditorFieldGroup().addCommitHandler(new FieldGroup.CommitHandler()
{
@Override
public void preCommit(FieldGroup.CommitEvent commitEvent) throws
FieldGroup.CommitException {
BeanItem item = (BeanItem)
commitEvent.getFieldBinder().getItemDataSource();
User user= (User) item.getBean();
user.getName();//getting the old value instead the new column value
}
}
我使用了如下所示的BeanItemContainer:
BeanItemContainer<User> container = new BeanItemContainer<User>(User.class);
grid.setContainerDataSource(container);
答案 0 :(得分:4)
preCommit(FieldGroup.CommitEvent commitEvent)
在提交之前称为 。因此,BeanItem的Bean(用户)包含旧值(新值尚未提交到Bean中)。因此getName()
返回旧值。
如果要访问新值,则必须使用postCommit(FieldGroup.CommitEvent commitEvent)
,在提交后 。
grid.getEditorFieldGroup().addCommitHandler(new FieldGroup.CommitHandler() {
@Override
public void preCommit(FieldGroup.CommitEvent commitEvent) {
BeanItem item = (BeanItem)
commitEvent.getFieldBinder().getItemDataSource();
User user = (User) item.getBean();
String name = user.getName(); // old value
System.out.println(name);
}
@Override
public void postCommit(FieldGroup.CommitEvent commitEvent) {
BeanItem item = (BeanItem)
commitEvent.getFieldBinder().getItemDataSource();
User user = (User) item.getBean();
String name = user.getName(); // new value
System.out.println(name);
}
});
答案 1 :(得分:0)
我们可以通过以下代码在preCommit()方法本身中获取新值,
TextField nameField = (TextField) grid.getColumn("name").getEditorField();
nameField .getValue(); // new name