Rodrigo Martinez
Rodrigo Martinez

Reputation: 943

Execute Backing Bean Action on p:selectOneRadio

I'm using a radio component on which, when selecting an item I want to execute an action on backing bean (not navigate to an outcome page, but to perform some action and then update current page via Ajax). Problem is I can't get the backing bean value change listener action to execute

<h:form id="one-radio">
    <p:selectOneRadio layout="grid" 
                    valueChangeListener="#{myBean.selectRadioItem}" >
        <p:ajax process="@this" update="@form" />
        <f:selectItems value="#{myBean.radioOptions}" var="radio"
            itemLabel="#{radio.name}" itemValue="#{radio.id}" >
        </f:selectItems>
    </p:selectOneRadio>
</h:form>

and the backing bean method...

public void selectRadioItem(ValueChangeEvent event) {
    String[] selectedItems = (String[]) event.getNewValue();
    //more...
}

is there something wrong in the code which I'm missing? I've used this same structure for select many check box which is working...

Upvotes: 3

Views: 14117

Answers (1)

Pellizon
Pellizon

Reputation: 1375

Rodrigo, there's a difference between valueChangeListener and a simple method call via ajax.

Check this answer by BalusC about the difference between valueChangeListener and <f:ajax>

To solve your problem, you could use the listener property of <p:ajax>.

OneRadio component is used to receive only one value, if you want to select a list of values, use SelectOneMenu

Try to do the following:

<p:selectOneRadio layout="grid" value="#{myBean.radioValue}">
    <p:ajax process="@this" update="@form" listener="#{myBean.selectRadioItem}"/>
    <f:selectItems value="#{myBean.radioOptions}" var="radio"
        itemLabel="#{radio.name}" itemValue="#{radio.id}" >
    </f:selectItems>
</p:selectOneRadio>

In the backbean, you can remove the event parameter, because the value of the OneRadio component now is a property, which I named as radioValue

String radioValue;
...
public void selectRadioItem() {
    String selectedItem = this.radioValue; 
//more...
}

Upvotes: 4

Related Questions