我在一个表单中有两个下拉列表。第一个包含这些选项:
String[] decisions = {"acceptée", "rejettée"};
当选择“rejettée”时,我想禁用第二个下拉列表。
以下是两个下拉列表的相关代码:
<p:selectOneMenu required="true" requiredMessage="veuillez choisir une decision" id="dec" value="#{editCommandController.myCom.decision}">
<f:selectItems value="#{editCommandController.decisions}" />
</p:selectOneMenu>
<p:selectOneMenu id="etat" value="#{editCommandController.myCom.etat}">
<f:selectItems value="#{editCommandController.etats}" />
</p:selectOneMenu>
我怎样才能做到这一点?
答案 0 :(得分:0)
试试这个:
<h:outputLabel for="dec" value="Decision : " />
<p:selectOneMenu required="true"
requiredMessage="veuillez choisir une decision" id="dec"
value="#{editCommandController.myCom.decision}">
<f:selectItems value="#{editCommandController.decisions}" />
<p:ajax update="etat" event="change"/>
</p:selectOneMenu>
<p:message for="dec" display="icon" />
<h:outputLabel for="etat" value="Etat : " />
<p:selectOneMenu id="etat" value="#{editCommandController.myCom.etat}" disabled="#{editCommandController.myCom.decision eq 'rejettée'}">
<f:selectItems value="#{editCommandController.etats}" />
</p:selectOneMenu>
<p:message for="etat" display="icon" />
要在更改时设置为某个默认值,请尝试以下操作:
<h:outputLabel for="dec" value="Decision : " />
<p:selectOneMenu required="true"
requiredMessage="veuillez choisir une decision" id="dec"
value="#{editCommandController.myCom.decision}" valueChangeListener="#{editCommandController.vclistener}">
<f:selectItems value="#{editCommandController.decisions}" />
<p:ajax />
</p:selectOneMenu>
<p:message for="dec" display="icon" />
<h:outputLabel for="etat" value="Etat : " />
<p:selectOneMenu id="etat" value="#{editCommandController.myCom.etat}" disabled="#{editCommandController.myCom.decision ne 'rejettée'}">
<f:selectItems value="#{editCommandController.etats}" />
</p:selectOneMenu>
<p:message for="etat" display="icon" />
然后在支持bean中添加selectItem(如果尚未存在),否则可以跳过它:
public static List<SelectItem> etats = new ArrayList<SelectItem>() { {
add(new SelectItem("Cheese", "Cheese"));
add(new SelectItem("Pickle", "Pickle"));
add(new SelectItem("Mustard", "Mustard"));
add(new SelectItem("Lettuce", "Lettuce"));
}};
public void vclistener(ValueChangeEvent e){
if(e.getNewValue().equals("rejettée")){
EditCommandController.etats.add(0, new SelectItem("Ketchup", "Ketchup"));
myCom.setEtat("Ketchup");
}
RequestContext.getCurrentInstance().update("etat");
}
public List<SelectItem> getEtats() {
return etats;
}
public void setEtats(List<SelectItem> etats) {
EditCommandController.etats = etats;
}