Primeffaces选择Many复选框



我有selectManyCheckbox,但在更改事件后,我无法从中获取检查值(所选值的列表保持为空)。更改事件被触发,但在调试模式下,SelectedSignalCauses列表中没有值。我试着设置一些转换器,但它也没有帮助(也许是错误的转换器)。

<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;
    }
}

为了将自定义对象发送到托管bean,您需要实现一个转换器,否则将无法实现。下面的代码基于您的代码实现了它,它可能是问题的解决方案。

@FacesConverter(value = "generalCollDTOConverter", forClass = GeneralCollDTOConverter.class)
public class GeneralCollDTOConverter implements Converter {
    @Override
    public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
        if (value != null) {
            return component.getAttributes().get(value);
        }
        return null;
    }
    @Override
    public String getAsString(FacesContext ctx, UIComponent component, Object value) {
        if (value != null && value instanceof GeneralCollDTO) {
            GeneralCollDTO generalCollDTO = (GeneralCollDTO) value;
            if (generalCollDTO.getName() != null) {
                component.getAttributes().put(generalCollDTO.getName(), value);
                return generalCollDTO.getName();
            }
        }
        return null;
    }
}

<p:selectManyCheckbox converter="generalCollDTOConverter" value="#{SignalAnalysisBean.listSelectedSignalCauses}" layout="pageDirection" styleClass="lstSignal">

最终我得到了一个对我来说很好的通用转换器。

@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;
  }
}

相关内容

  • 没有找到相关文章

最新更新