Search code examples
ajaxlistjsfprimefacesselectmanycheckbox

How to enable/disable specific item in selectManyCheckbox on ajax keyUp


I need your help in enabling/disabling a specific item in the selectManyCheckbox component based on the ajax call keyup.

When the page loads, I am firing the below method to populate the selectManyCheckbox items in the form:

 @PostConstruct

    public void init() throws SQLException {

    this.hrCertificatesList.add(new hrCertificate(("Employment"), "CE", false));

    this.hrCertificatesList.add(new hrCertificate(("Loan"), "LC", false));

}

And here is the jsf code:

  <p:inputText id="selectedEmployee" value="#{HRRequest.selectedEmployeeCode}">

     <p:ajax event="keyup" update="employeeName" listener="#{HRRequest.getEmployeeName}" />

   </p:inputText>

<h:outputText id="employeeName" value="#{HRRequest.selectedEmployeeName}" />

<p:selectManyCheckbox id="hrCertificates" value="#{HRRequest.selectedHRCertificates}">
 <f:selectItems value="#{HRRequest.hrCertificatesList}" var="hrCertificate" 
itemLabel="#{hrCertificate.hrCertificateName}"
    itemValue="#{hrCertificate.hrCertificateCode}" itemDisabled="#{hrCertificate.hrBooleanCertificate}"/>

    </p:selectManyCheckbox>

Once the page loads, all the checkboxes are enabled and when the user enters employeeCode in the inputText, an ajax will be fired to call a method to get the employeeName and to check whether has loan or not, if has loan, then the checkbox should be enabled, otherwise disabled.

To summarize my issue, what I want is that when the value of the variable temp equals to yes, then I need to disable the loan checkbox only and the other item Employment should remain enable, so how can I do this?

The bean code is:

 public String getEmployeeName() throws SQLException {
     if (temp.equals("Yes"))
{ 
//How to enable and disable the Loan checkbox only and to update the form view
RequestContext.getCurrentInstance().update(":HRform:hrCertificates");    
}

So can you please help.


Solution

  • Just manipulate the model in such way that itemDisabled="#{hrCertificate.hrBooleanCertificate}" evaluates true instead of false so that the view knows what it must do.

    One way might be:

    this.hrCertificatesList.get(1).setHrBooleanCertificate(true);