31

How to make default selection for <f:selectItem> within <h:selectOneMenu>?

It's needed,that particular "20" item of dropdown to be already selected when page is loaded.

  <h:selectOneMenu value="#{fileSearchCriteriaOut.recordsPerPage}"  >            
               <f:selectItem itemLabel="5" itemValue="5" />
               <f:selectItem itemLabel="10" itemValue="10" />
               <f:selectItem itemLabel="20" itemValue="20" selected="true"/>
  </h:selectOneMenu>

these four don't work:

<f:selectItem itemLabel="20" selected="true"/>
<f:selectItem itemLabel="20" selected="selected"/>
<f:selectItem itemLabel="20" checked="checked"/>
<f:selectItem itemLabel="20" checked="true"/>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sergionni
  • 13,290
  • 42
  • 132
  • 189

3 Answers3

48
<h:selectOneMenu id="items" value="#{bean.selectedItem}">
  <f:selectItem itemLabel="10" itemValue="10"/>
  <f:selectItem itemLabel="20" itemValue="20"/>
  <f:selectItem itemLabel="30" itemValue="30"/>
</h:selectOneMenu>

The default selection would be the one which has value same as selectedItem which you set in bean.

selectedItem = 20;
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
jmj
  • 237,923
  • 42
  • 401
  • 438
  • IMHO, I think you need an itemValue attribute in the selectItems? – Matt Handy Mar 17 '11 at 09:33
  • look like I don't see your point) , I mentioned default selected combo item when .xhtml loaded – sergionni Mar 17 '11 at 09:42
  • 3
    [There](http://myfaces.apache.org/core11/myfaces-impl/tlddoc/f/selectItem.html) is not such attribute, you need the reread what I have answered – jmj Mar 17 '11 at 09:49
  • In your Managed Bean you can set it as: `setSelectedItem(String selectedItemId) { this.selectedItem = selectedItemId; }` – Obaid May 09 '18 at 10:06
5

Initialize the recordsPerPage in your backing bean.

From your source code I assume that you have a bean FileSearchCriteriaOut and your recordsPerPage is a String, then you can do the following in the bean's constructor:

public FileSearchCriteriaOut() {
   recordsPerPage = "20";
}

For the facelet refer to Jigar Joshi's answer.

Matt Handy
  • 29,855
  • 2
  • 89
  • 112
-4
<div class="row">
    <div class="form-group col-md-6">
        <label for="sexo"><span class="obligatorio">#{messageSource['etiqueta_requerido']}</span> #{messageSource['etiqueta_estatus']}
        </label>
        <p:selectOneRadio 
            id                  = "status"  
            required            = "true"
            requiredMessage     = "#{messageSource['mensaje_validacion_datoRequerido']}"
            value="#{mbUnidadDeMedida.dtoUnidadDeMedida.estatus}"
        >
            <f:selectItem itemLabel="#{messageSource['etiqueta_activo']}"   itemValue="1" />
            <f:selectItem itemLabel="#{messageSource['etiqueta_inactivo']}" itemValue="0" />
        </p:selectOneRadio>
    </div>
</div>
Jason Roman
  • 8,146
  • 10
  • 35
  • 40