0

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.

99maas
  • 1,239
  • 12
  • 34
  • 59
  • @BalusC I have edited the topic to be more clear and explained the code more, so can you please guide and assist – 99maas Jul 06 '15 at 12:27
  • @BalusC hrBooleanCertificate I defined it in the hrCertificate class. The hrBooleanCertificate is referring to the boolean false which is in the init() method where at the beginning all the checkboxes should be enabled, but then I do not know what to do next for setting the Loan item disabled – 99maas Jul 06 '15 at 13:21
  • So you mean that I should define in my bean a new boolean variable called hrBooleanCertificate and to initialize it to false by default and then in the IF statement to make it true if yes or false if no based on the condition and then to update the component?Will this only be done to the loan checkbox? – 99maas Jul 06 '15 at 13:30
  • @BalusC is the above correct? – 99maas Jul 06 '15 at 16:03

1 Answers1

0

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);
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555