如何根据标志在 selectManyCheckbox 中 enable/disable 项目
How to enable/disable item in selecManyCheckbox based on flag
我需要你的帮助来禁用和启用 jsf 页面中 selectManyCheckbox
组件的项目。首先,selectManyCheckbox 组件显示三个复选框,它们是(贷款 - 健康 - 转账)。该列表将从具有以下代码的 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 中,我将 运行 一个 SQL 语句 return 是或否,我将把它添加到 loanFlag
variable.So 如果标志 ="Y",我需要启用贷款复选框以便用户可以 select 否则我需要从 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>
那么如何应用逻辑
首先,请注意 属性 不会淘汰支持它的实际属性,您只需要 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;
}
}
如果您坚持要进行过滤 "in the .xhtml",您可以将来自 JSTL 的 c:forEach
与 <f:selectItem>
结合使用(注意 item ,而不是 items),但它会使您的 xhtml 更加复杂,如果您想使用 Ajax 可能会导致问题。
您可以编辑您的 hrCertificate class 以添加 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>
我需要你的帮助来禁用和启用 jsf 页面中 selectManyCheckbox
组件的项目。首先,selectManyCheckbox 组件显示三个复选框,它们是(贷款 - 健康 - 转账)。该列表将从具有以下代码的 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 中,我将 运行 一个 SQL 语句 return 是或否,我将把它添加到 loanFlag
variable.So 如果标志 ="Y",我需要启用贷款复选框以便用户可以 select 否则我需要从 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>
那么如何应用逻辑
首先,请注意 属性 不会淘汰支持它的实际属性,您只需要 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;
}
}
如果您坚持要进行过滤 "in the .xhtml",您可以将来自 JSTL 的 c:forEach
与 <f:selectItem>
结合使用(注意 item ,而不是 items),但它会使您的 xhtml 更加复杂,如果您想使用 Ajax 可能会导致问题。
您可以编辑您的 hrCertificate class 以添加 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>