I need your help in disabling and enabling an item from the selectManyCheckbox
component in a jsf page. First of all, the selectManyCheckbox component is showing three chechboxes which are (Loan - Health - Transfer). The list will be populated from a bean which it has the code:
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"));
}
In the same bean, I will be running a SQL statement that will return either Yes or No and that value I am adding it to the loanFlag
variable.So if the flag="Y", I need to enable the loan checkbox so the user can select it else I need to disable it from the selectManyCheckbox
. The issue is that I am facing difficulties in applying the logic to disable and to enable the item selectManyCheckbox
where in the above code I am listing and enabling them all the time.
The code for the 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>
So how to apply the logic
First, note that a property does not retire an actual attribute backing it, you only need a getter. So you can have:
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.Could you edit your hrCertificate class to add a
disabled
boolean field? If yes, then you can additemDisabled="#{hrCerticate.disabled}"
to yourf:selectItems
which should be the easiest solution.Another option would be to use a
Map<hrCertificate, Boolean>
instead of aList<hrCertificate>
..xhtml