问题
I have a radiobutton list and would like to disable some items according to the outcome of a backing bean method.
<h:selectOneRadio value="#{managedBean.selectedItem}">
<f:selectItems value="#{managedBean.selectItems}"
var="x"
itemDisabled="#{managedBean.checkIncompatible(x)}" />
</h:selectOneRadio>
Is this the right way to do it? Meaning, will this code call checkIncompatible(x)
for each x
from the selectItems
list and set that item as enabled/disabled or just once and that's that?
I only managed to get all buttons to be either enabled or disabled and my suspicion is that the method only gets called once. Or that the rest of my code is not as perfect as I like to believe. And that would take a much longer question to fix.
回答1:
I can't reproduce your problem on Mojarra 2.1.4 with the following view:
<h:selectOneRadio value="#{bean.item}">
<f:selectItems value="#{bean.items}" var="item"
itemDisabled="#{bean.isDisabled(item)}" />
</h:selectOneRadio>
and the following bean:
private String[] items = { "one", "two", "three" }; // +getter
private String item; // +getter+setter
public boolean isDisabled(String item) {
return "two".equals(item);
}
The above example correctly disables item two
.
So, your problem is caused elsewhere, perhaps by a bug in checkUncompatible()
method. A breakpoint on the method teaches me that it's definitely called for each item.
来源:https://stackoverflow.com/questions/8249625/set-itemdisabled-from-backing-bean-method-for-each-item-in-list