Rafael Simonelli
Rafael Simonelli

Reputation: 304

JSF Updating p:selectOneMenu from another p:selectOneListbox with Ajax

I've been trying to do something that would be really simple... yet it won't work.

I have 2 "p:selectOneMenu", so that the second one items depends on the chosen item in the first one. They both have their own methods in the bean to list their items.

What I wanted to do was when the first changes its value, update the another so that the component should reload the list considering the choosen object. But the ajax never happens, at least in debug the method in the Bean is never called again.

What is wrong? The code is something like...

Thanks!!! :)

<p:selectOneMenu value="#{itemBean.lotacao}">
    <f:selectItem itemLabel="Choose one" />
    <f:selectItems value="#{itemBean.listLotacoes()}" 
        var="lotacao"
        itemLabel="#{lotacao.format()}" />
    <p:ajax update="localizacaoPorLotacao" />
</p:selectOneMenu>

<p:selectOneMenu id="localizacaoPorLotacao" 
    value="#{itemBean.localizacao}">
    <f:selectItem itemLabel="Choose one" />
    <f:selectItems value="#{itemBean.listByLotacao(itemBean.lotacao)}" />
</p:selectOneMenu>

@ManagedBean
@SessionScoped
public class ItemBean {

    // attributes, getters and setters here...

    public List<Lotacao> listLotacoes() {
        LotacaoService lotacaoService = new LotacaoService();  
        List<Lotacao> lotacoes = lotacaoService.getAll();       
        return lotacoes;
    }

    public List<Lotacao> listByLotacao(Lotacao lotacao) {
        if (lotacao == null) {
            return new ArrayList<Lotacao>();
        }

        String prefixo = lotacao.getCodigo().substring(0, 1);
        LotacaoService lotacaoService = new LotacaoService();  
        List<Lotacao> lotacoes = lotacaoService.getByPrefix(prefixo);

        return lotacoes;
    }

}

Upvotes: 0

Views: 1199

Answers (2)

Rafael Simonelli
Rafael Simonelli

Reputation: 304

Found it!

The lack of a converter to the class made the JSF don't trigger the setter. After creating a converter and adding it to the OneMenu, the code worked just fine.

The final version is:

<p:selectOneMenu value="#{itemQuadroVagasBean.lotacao}" 
    converter="orgaoLotacaoCodigoConverter">
    <p:ajax event="change" 
        update="localizacaoPorLotacao" />       
    <f:selectItem itemLabel="Selecione a lotação" />
    <f:selectItems value="#{itemQuadroVagasBean.listarLotacoes()}" 
        var="lotacao"
        itemLabel="#{lotacao.formatarLocalizacao()}" />                                                             
</p:selectOneMenu>

Upvotes: 2

Multisync
Multisync

Reputation: 8787

Maybe you need event:

<p:ajax update="localizacaoPorLotacao" event="change"/>

Upvotes: 0

Related Questions