我使用 Vaadin 8 定义了这样的网格:
ListDataProvider<Value> gridDataProvider =
new ListDataProvider<>(new ArrayList<>());
GridSelectionModel<Value> selectionModel;
Grid<Value> grid = new Grid<>(Value.class);
TextField editorField = new TextField();
editorField.setSizeFull();
Binder<Value> binder = new Binder<>();
binder.bind(editorField, Value::getValue, Value::setValue);
Editor<Value> gridEditor = grid.getEditor();
gridEditor.setBinder(binder);
gridEditor.addSaveListener((EditorSaveListener<Value>) event -> {
gridDataProvider.refreshAll();
});
gridEditor.setEnabled(true);
grid.addColumn(Value::getValue).setEditorComponent(editorField, Value::setValue);
grid.removeHeaderRow(0);
selectionModel = grid.setSelectionMode(Grid.SelectionMode.MULTI);
grid.setDataProvider(gridDataProvider);
grid.setSizeFull();
Value bean 只是一个普通的 bean
private class Value {
private String value;
Value(String value) {
this.value = value;
}
String getValue() {
return value;
}
void setValue(String value) {
this.value = value;
}
@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Value value1 = (Value) o;
return Objects.equals(value, value1.value);
}
我想要做的是添加按钮,因此通过单击该按钮,应将新行添加到网格中,并且应将新行中的唯一列设置为编辑模式,以便用户可以直接写入新值。是否有可能做到这一点?