5

I wrote an EnumConverter that is described in Use enum in h:selectManyCheckbox? Everything was fine until we recognize that this converter does not work properly in primefaces editable datatable. The problem is that although I added an attribute inside getAsString and getAsObject methods as following:

@Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value instanceof Enum) {
            component.getAttributes().put(ATTRIBUTE_ENUM_TYPE, value.getClass());
            return ((Enum<?>) value).name();
        } else {
            throw new ConverterException(new FacesMessage("Value is not an enum: " + value.getClass()));
        }
    }
public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Class<Enum> enumType = (Class<Enum>) component.getAttributes().get(ATTRIBUTE_ENUM_TYPE);
        try {
            return Enum.valueOf(enumType, value);
        } catch (IllegalArgumentException e) {
            throw new ConverterException(new FacesMessage("Value is not an enum of type: " + enumType));
        }
    }

In the latter method(getAsObject) could not find the attribute that I gave to the components attribute map. But out of the pprimefaces editable datatable everything is fine. Is there any solution to achieve this?

Community
  • 1
  • 1
demdem
  • 182
  • 3
  • 14

1 Answers1

2

This problem is caused because the custom component attribute was not saved in the row state of the PrimeFaces datatable (it works fine in standard h:dataTable).

We're going to need to store this information elsewhere. In the view scope along with the component ID would be one way.

In the getAsString():

context.getViewRoot().getViewMap().put(ATTRIBUTE_ENUM_TYPE + component.getId(), value.getClass());

And in the getAsObject():

Class<Enum> enumType = (Class<Enum>) context.getViewRoot().getViewMap().get(ATTRIBUTE_ENUM_TYPE + component.getId());
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555