p:selectOneMenu no list

Asked

Viewed 202 times

2

I’m in the struggle trying to popular a selectOneMenu with data from a Enum, but when I gave up and put the static data with f:selectItem I realized that still the field is empty.

Even with value filled with #{clienteBean.teste} the result is the same, where test is an attribute of type String with your getters and setters.

                <p:selectOneMenu id="console" value="" style="width:125px">
        <f:selectItem itemLabel="Select One" itemValue="" />
        <f:selectItem itemLabel="Xbox One" itemValue="Xbox One" />
        <f:selectItem itemLabel="PS4" itemValue="PS4" />
        <f:selectItem itemLabel="Wii U" itemValue="Wii U" />
    </p:selectOneMenu>  

Obs: the above example is the same as the showcase.

  • 1

    You can post the Enum code and its method, if you have, which returns all Enum values?

1 answer

3


Follow an example:

No Enum:

public enum ClienteTipos{

    SUSPECT("Suspect"),
    PROSPECT("Prospect"),
    CLIENTE("Cliente");

    ClienteTipos(String nome){
        this.nome = nome;
    }

    //Attributes
    private String nome;


    //Properties
    public String getNome(){
       return nome;
    }
}

No Managedbean:

public SelectItem[] getTiposCliente() {
    SelectItem[] items = new SelectItem[ClienteTipos.values().length];
    int i = 0;
    for(ClienteTipos t: ClienteTipos.values()) {
        items[i++] = new SelectItem(t, t.getNome());
    }
    return items;
}

On the xhtml page (JSF):

<h:selectOneListbox id="tipo" size="1" value="#{clienteMB.cliente.tipo}">
    <f:selectItems value="#{clienteMB.tiposCliente}" var="t" 
        itemLabel="#{t.nome}" itemValue="#{t}"/>
</h:selectOneListbox>

Source: Enum JSF Selectonemenu

  • 1

    It worked! thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.