javaprimefacesselectmanycheckbox

Primefaces selectManyCheckbox


I have selectManyCheckbox, but I cant get checked values from it after change event (list of selected values stay empty). Change event is triggered but in debug mode there is no values in listSelectedSignalCauses. I tried to set some converter but it didnt help either (maybe it was wrong converter).

<p:selectManyCheckbox value="#{SignalAnalysisBean.listSelectedSignalCauses}" layout="pageDirection" styleClass="lstSignal">  
  <f:selectItems value="#{SignalAnalysisBean.listSignalCauses}" var="causes" itemValue="#{causes}" itemLabel="#{causes.name}"  />
  <p:ajax process="@this" update="test" event="change" listener="#{SignalAnalysisBean.checkCausesList}" />  
                    </p:selectManyCheckbox>
<h:outputText value="#{SignalAnalysisBean.arrSize}" id="test" />
public class SignalAnalysisBean extends AbstractBean {
    private List<GeneralCollDTO> listSignalCauses;
    private List<GeneralCollDTO> listSelectedSignalCauses;
    //...

    public void checkCausesList(){
        arrSize = 4 + listSelectedSignalCauses.size();
    }
    //...
}

public class GeneralCollDTO {
    private String name;
    private String value;

    public GeneralCollDTO() { }

    public GeneralCollDTO(String n, String v) {
        this.name = n;
        this.value = v;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

Solution

  • Eventualy I end up with generic converter that works fine for me.

    @FacesConverter(value = "generalCollDTOConverter", forClass = GeneralCollDTOConverter.class)
    public class GeneralCollDTOConverter  implements Converter {
    
      private static Map<Object, String> entities = new HashMap<Object, String>();
    
      @Override
      public String getAsString(FacesContext context, UIComponent component, Object value) {
        synchronized (entities) {
            if (!entities.containsKey(value)) {
                String uuid = UUID.randomUUID().toString();
                entities.put(value, uuid);
                return uuid;
            } else {
                return entities.get(value);
            }
        }
      }
    
      @Override
      public Object getAsObject(FacesContext context, UIComponent component, String value) {
        for (Entry<Object, String> entry : entities.entrySet()) {
            if (entry.getValue().equals(value)) {
                System.out.println("getKey:" + entry.getKey());
                return entry.getKey();
            }
        }
        return value;
      }
    }