Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set default value for selectonemenu

Tags:

jsf

I have a select menu like this :

<h:selectOneMenu value="#{serverMB.selectedServerType}"  >
    <f:converter binding="#{serverTypeConverter}"/>
    <f:selectItems value="#{serverMB.serverTypesList}" var="servertypes"
            itemLabel="#{servertypes.server_type_name}" />
    <f:ajax listener="#{serverMB.changeSelectedOneMenuServerType}" render="selectservertype"/>
</h:selectOneMenu>

I want the default value of the select menu to be the value of one object that has been filled before, something like this:

<h:selectOneMenu value="#{serverMB.selectedServerType}" default="serverMB.server.servertype">   // <- default

or like this:

<h:selectOneMenu value="#{serverMB.selectedServerType}">
    <f:converter binding="#{serverTypeConverter}"/>
    <f:selectItem value"serverMB.server.servertype"/> <-   but this item was the first one and has been selected  
    <f:selectItems value="#{serverMB.serverTypesList}" var="servertypes"
            itemLabel="#{servertypes.server_type_name}" />
</h:selectOneMenu>
like image 958
omid haghighatgoo Avatar asked Nov 18 '13 13:11

omid haghighatgoo


2 Answers

You can do something like this if your object is already populated when you call the page, if it is not, it will show the <f:selectItem/> message:

<h:selectOneMenu value="#{serverMB.server.servertype}">
    <f:converter binding="#{serverTypeConverter}"/>
    <f:selectItem itemLabel="Select a Server..." />
    <f:selectItems value="#{serverMB.serverTypesList}" var="servertypes"
            itemLabel="#{servertypes.server_type_name}"/>
    <f:ajax listener="#{serverMB.changeSelectedOneMenuServerType}" 
            render="selectservertype"/>
</h:selectOneMenu>
like image 141
Anika Avatar answered Sep 19 '22 04:09

Anika


You can also use the OmniFaces converter.

Here is my example. It works fine for me. (Note: Year is a object. Make sure, you have equals implemented. Year is name and id only.)

<h:selectOneMenu value="#{bean.user.year}"
        converter="omnifaces.SelectItemsConverter">
    <f:selectItem itemLabel="Please select a year." itemValue="#{null}"
            noSelectionOption="true" />
    <f:selectItems value="#{bean.allyears}" var="year"
            itemValue="#{year}" itemLabel="#{year.name}" />
</h:selectOneMenu>
like image 44
alexander Avatar answered Sep 22 '22 04:09

alexander