Alan Lanzoni
Alan Lanzoni

Reputation: 53

Add selectedValue from selectonemenu to list

So, here's the deal: I have a selectOneMenu in my view:

<p:selectOneMenu value="#{personBean.person.personStates.state}" id="estadoRg"
    converter="entityConverter">
    <f:selectItems value="#{stateBean.states}" var="state" 
        itemLabel=#{state.name}" itemValue="#{state}">
    </f:selectItems>
</p:selectOneMenu>

Where personStates is a list containing person, state n date. Is it possible to add the value from the itemValue directly to the personStates list? (I have a addPersonState method autobuilt in my PersonModel)

If not, how should I do this? I don't want to add one state instance for each selectOneMenu (will be 5) in my personBean... Was it clear?

Upvotes: 3

Views: 275

Answers (1)

&#214;mer Faruk Almalı
&#214;mer Faruk Almalı

Reputation: 3822

You should handle it via selectedState value and then you can bind it for specific person:

<p:selectOneMenu value="#{stateBean.selectedState}" id="estadoRg"
    converter="entityConverter">
    <f:selectItems value="#{stateBean.states}" var="state" 
        itemLabel=#{state.name}" itemValue="#{state}">
    </f:selectItems>
</p:selectOneMenu>

This is the way how p:selectOneMenu is used, to be able to bind selected state and the person call a method:

<p:commandButton actionListener="#{personBean.matchStates}" />

And backing bean method:

public void matchStates {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ElContext elContext = facesContext.getELContext();
    Object stateBean = elContext.getELResolver().getValue(elContext, null, "stateBean"). 
    State selectedState = stateBean.getSelectedState();
    personList.get(index).setState = selectedState;
}

Upvotes: 2

Related Questions