So that your <p:selectOneMenu>
show the description in the options you must specify which property should be shown through the attribute itemLabel
.
The standard JSF does not have the necessary support for what you need with ENUMS. Therefore, we have to resort to other support bilbiotecas. In that case, the library Primefaces Extensions has improved ENUMS support for JSF.
Using Primefaces Extensions your code would look like this:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:pe="http://primefaces.org/ui/extensions"> <!-- Importação da biblioteca no XHTML -->
<pe:importEnum type="leiEditMB.enumTipoLei" var="enum" allSuffix="ALL_ENUM_VALUES" />
<p:selectOneMenu value="#{leiEditMB.bean.enumTipoLei}">
<f:selectItens value="#{enum.ALL_ENUM_VALUES}" var="e"
itemLabel="#{e.descricao}" itemValue="#{e}" />
</p:selectOneMenu>
The ENUM values can be accessed through the class name (default setting) or through a custom name (attribute var
, in the code above var="enum"
).
You can also get all ENUMS of the class with the suffix "ALL_VALUES" (default) or a custom prefix through the attribute allSuffix
(in the code above I used allSuffix="ALL_ENUM_VALUES"
).
More information about the component <pe:importEnum>
: http://www.primefaces.org/showcase-ext/sections/utils/importEnum.jsf
I did as you said, but I’m having trouble importing ALL.ENUM_VALUES, the following image: https://dl.dropboxusercontent.com/u/17997689/06%20-%20cabe%C3%A7alho.png, and https://dl.dropboxusercontent.com/u/17997689/05%20-%20error%20import%C3%A7%C3%A3o%20pe.png https error://dl.dropboxusercontent.com/u/17997689/Captura%20de%20tela%202015-02-09%2008.33.35.png
– Jackson Emmerich
My dear, I’m sorry. There is a part of the answer that is incorrect (no
<f:selectItems>
), where I wrotevalue="#{leiEditMD.enumTipoLei.ALL_ENUM_VALUES}"
should bevalue="#{enum.ALL_ENUM_VALUES}"
.– Luídne
did not work, the field selectOneMenu is bringing empty, follow image, https://dl.dropboxusercontent.com/u/17997689/07%20-%20selectOneMenu%20in%20white.png
– Jackson Emmerich
How is your code
<p:selectOneMenu>
?– Luídne
<p:selectOneMenu id="enumTipoLei" effect="fade" value="#{leiEditMB.bean.enumTipoLei}">
 <f:selectItems value="#{leiEditMB.enumTipoLei}"/>
 </p:selectOneMenu>
– Jackson Emmerich
You have to use the imported attribute, in this case, the
var="#{enum}"
, so try it like this:<p:selectOneMenu id="enumTipoLei" effect="fade" value="#{leiEditMB.bean.enumTipoLei}"> <f:selectItems value="#{enum.ALL_ENUM_VALUES}" var="e" itemLabel="#{e.descricao}" itemValue="#{e}"/> </p:selectOneMenu>
I tested it here so it works.– Luídne