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

梦想的初衷 提交于 2019-12-12 03:53:59

问题


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?


回答1:


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();
});


来源:https://stackoverflow.com/questions/44324294/is-there-a-way-to-set-a-cell-in-grid-in-edit-mode-with-vaadin-8-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!