【问题标题】:How to enable/disable item in selecManyCheckbox based on flag如何根据标志启用/禁用 selectManyCheckbox 中的项目
【发布时间】:2015-07-03 08:42:19
【问题描述】:

在禁用和启用 jsf 页面中 selectManyCheckbox 组件中的项目时,我需要您的帮助。首先,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 中,我将运行一个返回 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>

那么如何应用逻辑

【问题讨论】:

    标签: list jsf primefaces selectmanycheckbox


    【解决方案1】:

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

    另一种选择是使用Map&lt;hrCertificate, Boolean&gt; 而不是List&lt;hrCertificate&gt;

    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>
    

    【讨论】:

    • 我采用了您的第一个解决方案,它解决了它。谢谢
    【解决方案2】:

    首先,请注意,属性不会取消支持它的实际属性,您只需要一个 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 中”进行过滤,您可以将来自 JSTL 的 c:forEach&lt;f:selectItem&gt; 结合起来(注意 item,而不是 items em>),但它会使你的 xhtml 更加复杂,如果你想使用 Ajax 可能会导致问题。

    【讨论】:

      猜你喜欢
      • 2015-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      • 2023-02-07
      • 1970-01-01
      相关资源
      最近更新 更多