简体   繁体   中英

Is there a way to set a cell in grid in edit mode with Vaadin 8 grid

I have defined a grid like this using 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();

The Value bean is just a regular 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);
       }

What I want to do is to add button so by clicking on that button a new row should be added to grid and the only column in the new row should be set to edit mode so user can write a new value directly. Is it possible to do this?

Opening editor programmatically is not possible with the current Vaadin 8 APIs. There are 2 issues to add this feature currently open:

https://github.com/vaadin/framework/issues/8477
https://github.com/vaadin/framework/issues/8820

It might be good idea not to use Value.value in equals method because this will cause problems with some Grid features, like grid.select(T item).

This piece of code will allow user to use arrow and enter keys to edit the latest item.

private void onButtonClick(Button.ClickEvent clickEvent) {
    Value newValue = new Value("New value");
    list.add(newValue);
    grid.getDataProvider().refreshAll();
    grid.focus();
}

Grid will not receive focus automatically after editor is hidden. You need to help Vaadin a bit:

gridEditor.addSaveListener((EditorSaveListener<Value>) event -> {
    gridDataProvider.refreshAll();
    grid.focus();
});

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