1

我需要您的帮助来禁用和启用selectManyCheckboxjsf 页面中组件中的项目。首先,selectManyCheckbox 组件显示了三个复选框,它们是(Loan - Health - Transfer)。该列表将从具有代码的 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 语句,该语句将返回 Yes 或 No,并将该值添加到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>

那么如何应用逻辑

4

2 回答 2

0

你能编辑你的 hrCertificate 类来添加一个disabled布尔字段吗?如果是,那么您可以添加itemDisabled="#{hrCerticate.disabled}"到您的f:selectItems哪个应该是最简单的解决方案。

另一种选择是使用 aMap<hrCertificate, Boolean>而不是 a 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>
于 2015-07-03T09:05:45.830 回答
-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 中”进行过滤,您可以将c:forEachJSTL 与<f:selectItem>(注意item,而不是items)结合起来,但这会使您的 xhtml 更加复杂,并且如果您想将 Ajax 与它。

于 2015-07-03T09:04:42.927 回答