简体   繁体   English

是否可以使用Vaadin 8网格在编辑模式下在网格中设置单元格

[英]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: 我已经使用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 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);
       }

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. 当前的Vaadin 8 API无法以编程方式打开编辑器。 There are 2 issues to add this feature currently open: 当前有2个添加该功能的问题:

https://github.com/vaadin/framework/issues/8477 https://github.com/vaadin/framework/issues/8477
https://github.com/vaadin/framework/issues/8820 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). 最好不要在equals方法中使用Value.value,因为这将导致某些Grid功能(例如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: 您需要一点帮助Vaadin:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM