有没有一种方法可以在编辑模式下用Vaadin 8网格在网格中设置单元格 [英] Is there a way to set a cell in grid in edit mode with Vaadin 8 grid

查看:176
本文介绍了有没有一种方法可以在编辑模式下用Vaadin 8网格在网格中设置单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Vaadin 8定义了一个这样的网格:

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

Value bean只是一个普通的bean

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?

推荐答案

目前的Vaadin 8 API无法以编程方式打开编辑器。有2个问题要添加此功能目前打开:

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

最好不要使用Value.value在equals方法中,因为这会导致一些Grid功能的问题,比如grid.select(T item)。

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

隐藏编辑器后,网格不会自动获得焦点。您需要帮助Vaadin:

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

这篇关于有没有一种方法可以在编辑模式下用Vaadin 8网格在网格中设置单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆