我是 vaadin 新手,当 setEditorEnabled = true 时,我对
。 save
和 cancel
button
感到有点困惑
您是否需要提供额外的代码才能保存数据,或者当您单击“保存”时它会自动将所有数据保存到数据库中?
如果有额外的代码,如何为保存和取消按钮添加监听器?
谢谢!
最佳答案
如果您使用缓冲模式,则当您按“保存”按钮时,编辑的值将写入源对象。如果您使用无缓冲模式,则版本会立即写入,因此“保存”和“取消”按钮变得毫无意义。 如果要将编辑后的对象写回数据库,则需要手动添加该功能。在这种情况下,实用的做法是使用缓冲模式,并将数据库调用方法添加到按下保存按钮时调用的方法中。
除了将其添加到 CommitHandler
的postCommit
方法,就像 Daniel Dubec 所写的那样,您还可以覆盖 saveEditor()
和doCancelEditor()
Grid
的方法.
class MyGrid extends Grid {
public MyGrid() {
setEditorEnabled(true);
setEditorBuffered(true);
}
@Override
public void saveEditor() throws CommitException {
super.saveEditor();
// You can persist your data here.
Notification.show("Item " + getEditedItemId() + " was edited.");
}
// Be aware that doCancelEditor() is called whenever super.saveEditor() is called!
@Override
protected void doCancelEditor() {
super.doCancelEditor();
// editedItemId was already set to 'null'.
Notification.show("Cancel button was pressed");
}
}
什么super.saveEditor()
实际上是在调用 commit()
editorFieldGroup
上的方法。但这仅在可编辑网格处于缓冲模式时才有意义。 Read more on Field Buffering here 。
那么会发生什么,当您按下保存按钮时,super.saveEditor()
被调用,然后首先触发预提交事件,然后将编辑器字段值中的更改更新到数据源(即提交本身),然后触发提交后事件。 doCancelEditor()
每当编辑器本身关闭时都会调用该方法,这就是为什么在保存后也会调用它。
关于java - Vaadin 网格 - 当 setEnableEditor= true 时保存/取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30706276/