java - 有没有办法使用 Vaadin 8 网格在编辑模式下设置网格中的单元格

标签 java grid vaadin vaadin8

我使用 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);
       }

我想要做的是添加按钮,因此通过单击该按钮,应将新行添加到网格中,并且新行中的唯一列应设置为编辑模式,以便用户可以直接写入新值。 可以这样做吗?

最佳答案

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

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

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

这段代码将允许用户使用箭头和回车键来编辑最新的项目。

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

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

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

关于java - 有没有办法使用 Vaadin 8 网格在编辑模式下设置网格中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44324294/

相关文章:

spring-security - 如何在 Vaadin 中注销 Spring Security?

java - Vaadin:按回车键后自动单击按钮

java - 为什么 Web 应用程序的 Solr 搜索引擎在搜索 ‘Java Spring’ 时会返回 ‘Spring Fertility’ 结果?如何解决这个问题?

java - 如何摆脱 Maven 快照版本

css - 如何创建网格/平铺 View ?

html - CSS Grid - 自动列高

java - Vaadin - 将数据传递到不同的 UI

java - 使用 Swing 播放音频 - 我的代码线程安全吗?

java - NFC 安卓光束

java - 如何将网络添加到我在 GridWorld 中制作的游戏中?