allegjdm93
allegjdm93

Reputation: 602

How to bind p:selectManyCheckbox value to List<String> of a Map<K, List<String>>

I am trying to find a way to populate a map from multiple p:selectManyCheckbox. However, when i go look at the selected data in the map I get an array of Object array of Attribute which I cant access.

<p:dataList id="serviceCatalogueCriteria" var="category" value="#{serviceCatalogueController.categories}">
<ui:fragment
    rendered="#{categoryService.isMultipleSelect(category)}">
    <p:selectManyCheckbox
            value="#{serviceCatalogueController.categoryToAttributes[category]}"
            layout="pageDirection" columns="1"
            converter="#{attributeConverter}">
            <f:selectItems value="#{category.attributes.toArray()}"
                var="attribute" itemLabel="#{attribute.name}"
                itemValue="#{attribute}" />
    </p:selectManyCheckbox>
</ui:fragment>
</p:dataList>

In the back bean I have

private Map<Category, List<Attribute>> categoryToAttributes = new HashMap<Category,List<Attribute>>();

for (Category cat : categoryToAttributes.keySet()) {
        for (Attribute attr : categoryToAttributes.get(cat)) {
            finalList.add(attributeDAO.fetchAttributeWithCategoryAndName(cat.getInternalName(), attr));
        }
    }

When I run through this I get a nice

[Ljava.lang.Object; cannot be cast to java.util.List

Blows up at categoryToAttributes.get(cat). Cant get the object I also tried using a converter but I get pretty much the same thing.

The converter looks like

@Override
public Object getAsObject(FacesContext context, UIComponent component,
        String value) {
    if (value == null || value.length() == 0)
        return null;
    try {
        Long id = Long.decode(value);
        return attributeDAO.fetchAttributesWithIds(Collections.singletonList(id)).get(0);
    } catch (NumberFormatException e) {
        return null;
    }
}

@Override
public String getAsString(FacesContext context, UIComponent component,
        Object value) {
    if (value == null || !(value instanceof Attribute))
        return null;
    return "" + ((Attribute)value).getId();
}

If I remove the converter I get this

java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.util.List

Upvotes: 0

Views: 1677

Answers (1)

Angel Santiago
Angel Santiago

Reputation: 9

excuse me by my peer English, i hope can help you.

At *.xhtml

<p:dataList id="serviceCatalogueCriteria" var="category" 
value="#{serviceCatalogueController.categories}">

<ui:fragment
rendered="#{categoryService.isMultipleSelect(category)}">
<p:selectManyCheckbox
        value="#{serviceCatalogueController.attributesByCategory}"
        layout="pageDirection" columns="1"
        converter="#{attributeConverter}">
        <f:selectItems value="#{category.attributes.toArray()}"
            var="attribute" itemLabel="#{attribute.name}"
            itemValue="#{attribute}" />
<p:ajax listener="#{alta.almacenarModulos(category)}"/>
</p:selectManyCheckbox>
</ui:fragment>
</p:dataList>

And at your Bean:

private Map<Category, List<Attribute>> attributesMap=new HashMap<>();
private List<Attribute> attributesByCategory;
public void almacenarModulos(Categorycategory)
   {
     attributesMap.put(category,attributesByCategory);
   }

I do some like this by a scholar proyect.

Upvotes: 1

Related Questions