我需要您帮助您在jsf页面中禁用和启用selectManyCheckbox
组件中的项目。首先,selectManyCheckbox组件显示三个chechbox(贷款 - 健康 - 转移)。该列表将从一个bean中填充,它具有代码:
private List<hrCertificate> hrCertificatesList = new ArrayList<hrCertificate>();
//Getter and Setter
Private String loanFlag="";
@PostConstruct
public void init() {
this.hrCertificatesList.add(new hrCertificate(("Loan"), "LC"));
this.hrCertificatesList.add(new hrCertificate(("Health"), "HI"));
this.hrCertificatesList.add(new hrCertificate(("Trasnfer"), "TE"));
}
在同一个bean中,我将运行一个返回Yes或No的SQL语句,并将该值添加到loanFlag
变量中。如果flag =“Y”,我需要启用贷款复选框,以便用户可以选择它,否则我需要从selectManyCheckbox
禁用它。问题是我在应用逻辑禁用和启用项目selectManyCheckbox
时遇到了困难,我在上面的代码中列出并一直启用它们。
selectManyChexkbox的代码:
<p:selectManyCheckbox id="hrCertificates" value="#{user.selectedHRCertificates}" layout="pageDirectio>
<f:selectItems value="#{user.hrCertificatesList}"
var="hrCertificate" itemLabel="#{hrCertificate.hrCertificateName}"
itemValue="#{hrCertificate.hrCertificateCode}"/>
</p:selectManyCheckbox>
那么如何应用逻辑
答案 0 :(得分:0)
您是否可以编辑hrCertificate类以添加disabled
布尔字段?如果是,那么您可以将itemDisabled="#{hrCerticate.disabled}"
添加到f:selectItems
,这应该是最简单的解决方案。
另一种选择是使用Map<hrCertificate, Boolean>
代替List<hrCertificate>
。
private Map<hrCertificate, Boolean> hrCertificatesMap = new HashMap<hrCertificate, Boolean>();
@PostConstruct
public void init() {
hrCertificatesMap.put(new hrCertificate(("Loan"), "LC"), null);
hrCertificatesMap.put(new hrCertificate(("Health"), "HI"), null);
hrCertificatesMap.put(new hrCertificate(("Trasnfer"), "TE"), null);
}
// Then when you're done with your SQL query, update your Map to add the corresponding boolean values...
.xhtml
<p:selectManyCheckbox id="hrCertificates" value="#{user.selectedHRCertificates}" layout="pageDirectio>
<f:selectItems value="#{user.hrCertificatesMap.keySet().toArray()}" var="hrCertificate" itemLabel="#{hrCertificate.hrCertificateName}" itemValue="#{hrCertificate.hrCertificateCode}" itemDisabled="#{user.hrCertificatesMap.get(hrCertificate)}" />
</p:selectManyCheckbox>
答案 1 :(得分:-1)
首先,请注意,属性不会退出支持它的实际属性,您只需要一个getter。所以你可以:
public class MyBean implements Serializable {
private FilterEnum certFilter = FilterEnum.NO_FILTER;
private List<Certificate> certificates;
... // including certificates initialization.
public FilterEnum getCertFilter() {
return this.certFilter;
}
public void setCertFilter(FilterEnum certFilter) {
this.certFilter = certFilter;
}
public List<Certificate> getCertificates() {
// I am sure there is a cooler way to do the same with streams in Java 8
ArrayList<Certificate> returnValue = new ArrayList<>();
for (Certificate certificate : this.certificates) {
switch (this.certFilter) {
case FilterEnum.NO_FILTER:
returnValue.add(certificate);
break;
case FilterEnum.ONLY_YES:
if (certificate.isLoan) {
returnValue.add(certificate);
}
break;
case FilterEnum.ONLY_NO:
if (!certificate.isLoan) {
returnValue.add(certificate);
}
break;
}
}
return returnValue;
}
}
如果您坚持要在.xhtml&#34;中进行过滤&#34;,可以将JSTL中的c:forEach
与<f:selectItem>
合并(注意 item ,而不是项目),但它会使你的xhtml更复杂,如果你想使用Ajax,可能会引起问题。