萨拉姆,
我正在尝试在我的项目中实现一个 Primefaces PickList,并且我被困在我必须从源列表转移到目标的最大元素中:在我的情况下,我想从大约 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"/>
iv 基于balusC对这篇文章的回答创建了一个验证器:How to validate the maximum amount of checked values of a selectManyCheckbox based on 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 次以上的检查。
我应该如何触发验证(在我看来,我一直认为它会自动触发)
谢谢。