如何在Vaadin网格中只编辑一些列? [英] How to make only some columns editable in a Vaadin Grid?

查看:161
本文介绍了如何在Vaadin网格中只编辑一些列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Vaadin Grid允许定义为可编辑的

Vaadin Grid allows to be defined as editable with

grid.setEditorEnabled(true);

这使得所有可见列都可编辑。但是我不希望用户编辑特定列,但似乎可编辑是全部或全部。

This makes all visible columns editable. However I don't want the user to edit an specific column, but seems like the editable is an all or nothing.

我找到的下一个最佳解决方案是定义一个带有禁用编辑器的编辑器字段,几乎可以完成这一操作,但用户仍然可以选择文本并移动光标(但该字段不再可编辑)。

The next best solution I have found is to define an editor field with a disabled editor, which almost does the trick but the user is still able to select the text and move the cursor (but the field is not editable anymore).

Grid.Column nameColumn = grid.getColumn("fullName");
nameColumn.setHeaderCaption("Full Name");
nameColumn.setEditorField(getNoEditableTextField());

...

private Field<?> getNoEditableTextField() {
    TextField noEditableTextFiled = new TextField();
    noEditableTextFiled.setEnabled(false);
    return noEditableTextFiled;
}

我认为Label不能用,因为它不是Field。

I believe Label cannot be used because it's not a Field.

是否有更好的选择来实现这一目标?

Is there a better option to achieve this?

编辑:正如aakath所说,有一种方法可以实现这一目标要编辑的列,但是在编辑行时,单元格值会消失,这是不可取的。

edit: as aakath said, there is a way of achieving this not enabling the column to be edited, but in doing so the cell value disappears when you edit the row, which is not desirable.

推荐答案

我的解决方案如下。我刚刚完成。它没有经过太多测试。但它可能会给你一些想法。

my solution is below. i have just finished. it was not tested too much. but it may give you some ideas.

ati

  getColumn(columnName).setEditable(true).setEditorField(getNoEditableField(columnName));

...

private Field<?> getNoEditableField(final String columnName) {
    CustomField<Label> result = new CustomField() {
        @Override
        protected Component getContent() {
            Label result = (Label) super.getContent();
            Object editedItemId = getEditedItemId();
            String value = DEFAULT_VALUE;
            if (editedItemId != null) {
                value = CustomizableGrid.this.toString(getContainerDataSource().getItem(editedItemId).getItemProperty(columnName).getValue());
            }
            result.setValue(value);
            return result;
        }

        @Override
        protected Component initContent() {
            Label result = new Label(DEFAULT_VALUE, ContentMode.HTML);
            result.setDescription(getColumnDescription(columnName));
            result.setStyleName("immutablegridcellstyle");
            return result;
        }

        @Override
        public Class getType() {
            return Label.class;
        }
    };
    result.setConverter(new Converter<Label, Object>() {
    //converter for your data
    });

    return result;
}

这篇关于如何在Vaadin网格中只编辑一些列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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