如何基于标志启用/禁用selecManyCheckbox中的项目 [英] How to enable/disable item in selecManyCheckbox based on flag
问题描述
我需要您的帮助来禁用和启用jsf页面中selectManyCheckbox
组件中的项目.首先,selectManyCheckbox组件显示三个复选框(贷款-健康-转移).该列表将从具有以下代码的Bean中填充:
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"));
}
在同一个bean中,我将运行一条SQL语句,该语句将返回Yes或No,并将该值添加到loanFlag
变量中.因此,如果flag ="Y",则需要启用贷款复选框,以便用户可以选择它,否则我需要从selectManyCheckbox
禁用它.问题是我在应用禁用和启用项目selectManyCheckbox
的逻辑时遇到困难,我在上面的代码中列出并始终启用它们.
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.
selectManyChexkbox的代码:
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>
那么如何运用逻辑
推荐答案
您可以编辑hrCertificate类以添加disabled
布尔字段吗?如果是,则可以将itemDisabled="#{hrCerticate.disabled}"
添加到f:selectItems
中,这应该是最简单的解决方案.
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.
另一种选择是使用Map<hrCertificate, Boolean>
代替List<hrCertificate>
.
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>
这篇关于如何基于标志启用/禁用selecManyCheckbox中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!