vaadin7网格编辑器字段无法设置空表示?

时间:2016-05-11 12:31:22

标签: grid vaadin

仍然没有解决方案! (┬_┬)

我尝试使用以下代码将空字符串设置为NullRepresentation,但是失败了,任何人都可以提供帮助吗?或其他好的解决方案表示赞赏。

final Collection fields = this.grid.getEditorFieldGroup().getFields();
    fields.stream().forEach((field -> {
        if (field instanceof TextField) {
            ((TextField) field).setNullRepresentation("");
        }
    }));

我尝试使用自定义FieldGroup来覆盖它,但又失败了。

public class CmsFieldGroup extends FieldGroup {

@Override
public Field<?> getField(final Object propertyId) {
    Field field = null;
    field = super.getField(propertyId);
    if (field instanceof TextField) {
        ((TextField) field).setNullRepresentation("");
    }

    return field;
}

@Override
public Collection<Field<?>> getFields() {
    final Collection<Field<?>> fields = super.getFields();
    fields.stream().forEach((field -> {
        if (field instanceof TextField) {
            ((TextField) field).setNullRepresentation("");
        }
    }));

    return fields;
}

}

final CmsFieldGroup cmsFieldGroup = new CmsFieldGroup();
    this.grid.setEditorFieldGroup(cmsFieldGroup);

2 个答案:

答案 0 :(得分:0)

似乎可以按照以下方式完成,但代码似乎很难看。

public class CmsFieldGroup extends FieldGroup {

@Override
public Field<?> getField(final Object propertyId) {
    Field field = null;
    field = super.getField(propertyId);
    if (field instanceof TextField) {
        ((TextField) field).setNullRepresentation("");
    }

    return field;
}

@Override
public Collection<Field<?>> getFields() {
    final Collection<Field<?>> fields = super.getFields();
    for (final Field field : fields) {

        try {
            if (field instanceof TextField && !"".equals(((TextField) field).getNullRepresentation())) {
                ((TextField) field).setNullRepresentation("");
            }
        } catch (final Exception e) {
            System.out.print("something wrong");
        }
    }

    return fields;
}

}

答案 1 :(得分:0)

以下似乎也有效:

FieldGroup fieldGroup = grid.getEditorFieldGroup();
fieldGroup.setFieldFactory(new MyFieldGroupFieldFactory());

MyFieldGroupFieldFactory定义如下:

class MyFieldGroupFieldFactory extends DefaultFieldGroupFieldFactory {

    @Override
    public <T extends Field> T createField(Class<?> type, Class<T> fieldType) {
        T field = super.createField(type, fieldType);
        if (field instanceof TextField) {
            TextField textField = (TextField) field;
            textField.setNullRepresentation("");
        }
        return field;
    }
}