萨拉姆
我正在尝试在我的项目中实现Primefaces PickList,而我受困于必须从源列表转移到目标的max元素: 就我而言,我想从大约12种可能的选择列表中最多转移2种选择。
我的selectChoice.xhtml看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<h1>Liste des cadres</h1>
</h:head>
<h:body>
<ui:composition template="../resources/template.xhtml">
<ui:define name="content">
<h:form>
<p:messages for="update"/>
<p:pickList id="pickList" value="#{etudiantController.pickFiliere}" var="filiere" itemLabel="#{filiere.intitule}" itemValue="#{filiere}" />
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
我的托管bean是这样的:
public List<Filiere> getListFiliere() {
listFiliere = filiereService.findAll();
return listFiliere;
}
public void setListFiliere(List<Filiere> listFiliere) {
this.listFiliere = listFiliere;
}
public DualListModel<Filiere> getPickFiliere() {
if (pickFiliere == null) {
pickFiliere = new DualListModel<Filiere>();
pickFiliere.setSource(getListFiliere());
}
return pickFiliere;
}
public void setPickFiliere(DualListModel<Filiere> pickFiliere) {
this.pickFiliere = pickFiliere;
}
public void onTransfer(TransferEvent event) {
StringBuilder builder = new StringBuilder();
for(Object item : event.getItems()) {
builder.append(((Filiere) item).getIntitule()).append("<br />");
}
FacesMessage msg = new FacesMessage();
msg.setSeverity(FacesMessage.SEVERITY_INFO);
msg.setSummary("Items Transferred");
msg.setDetail(builder.toString());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void onSelect(SelectEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Item Selected", event.getObject().toString()));
}
public void onUnselect(UnselectEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Item Unselected", event.getObject().toString()));
}
public void onReorder() {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "List Reordered", null));
}
如何告诉我的PickList在进行2次传输后禁用元素传输。
感谢
////////////////// ************************** ////// ////////////////////////////////// 建议像melloware那样切换到SelectManyCheckbox,因为用户似乎更喜欢使用复选框,所以我的xhtml:
<p:selectManyCheckbox id="studentChoices" value="#{etudiantController.choixFLTmp}" layout="responsive" columns="3">
<f:selectItems value="#{filiereController.allFilieres}" var="filiere" itemLabel="#{filiere.intitule}" itemValue="#{filiere}" />
<f:validator validatorId="studentChoicesValidator" />
</p:selectManyCheckbox>
<p:message id="verifyMaxChoices" for="studentChoices"/>
i v根据此帖子的balusC个答案创建了一个验证器:How to validate the maximum amount of checked values of a selectManyCheckbox based on the current selection of a selectOneMenu?
我的验证者:
@FacesValidator("studentChoicesValidator")
public class StudentChoicesValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
List<Filiere> choixFLTmp = (List<Filiere>) value;
if (choixFLTmp.size() > 2) {
throw new ValidatorException(new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Vous ne pouvez cochez plus que deux(2) choix !", null));
}
}
}
目前,该消息未显示在我的xhtml中,并且用户可以进行2次以上的检查。
我应该如何触发验证(在我看来,我一直认为验证会自动触发)
谢谢。