How to enable/disable item in selecManyCheckbox based on flag

自作多情 提交于 2019-12-02 06:44:15

Could you edit your hrCertificate class to add a disabled boolean field? If yes, then you can add itemDisabled="#{hrCerticate.disabled}" to your f:selectItems which should be the easiest solution.

Another option would be to use a Map<hrCertificate, Boolean> instead of 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>

First, note that a property does not retire an actual attribute backing it, you only need a getter. So you can have:

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;
   }
 }

If you insist that you want to do the filter "in the .xhtml", you can combine c:forEach from JSTL with <f:selectItem> (note item, not items), but it will make your xhtml more complicated and may cause issues if you want to use Ajax with it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!