set itemDisabled from backing bean method for each

2019-07-07 01:41发布

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条回答
啃猪蹄的小仙女
2楼-- · 2019-07-07 02:31

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.

查看更多
登录 后发表回答