Pradeep Vishnu
Pradeep Vishnu

Reputation: 11

selectOneChoice populate with pre-selected default value

I need af:selectOneChoice populated with values from the backing bean and one the value from the list (say index=5) should be selected by default. We are using Oracle Adf 10.*

Can somebody help me on this ?

Thanks

Upvotes: 0

Views: 11484

Answers (2)

Povilas Petrauskas
Povilas Petrauskas

Reputation: 21

For setting default value, you can use this:

<af:selectOneChoice label="My Field"
                    id="MyField" value="#{bindings.MyAttribute.inputValue}"
                    autoSubmit="true"
                    binding="#{MyBean.myField}">
  <f:selectItems value="#{bindings.MyAttribute.items}"
                 id="MyFieldItems"/>
</af:selectOneChoice>

Notice that SelectOneChoice field has a binding to a java bean. We will use setter method to set default value.

public void setMyField(RichSelectOneChoice myField) {
    // since getter/setter methods are called multiple times on
    // page load, we need to prevent setting attribute value more than once
    if(myField != null && myField.getValue() == null){
        ADFUtils.setBoundAttributeValue("MyAttribute", "SomeValue");
    }
    this.myField = myField;
}

For setting default index (for example, first from the list) we can use a similar approach:

public void setMyField(RichSelectOneChoice myField) {
    if(myField != null && myField.getValue() == null){
        // default value will be 1st from the list
        myField.setValue(0);
    }
    this.myField = myField;
}

Upvotes: 0

Florin Marcus
Florin Marcus

Reputation: 1657

For populating the list values, you can use:

<af:selectOneChoice value="val3" label="XXXX" id="soc1" >
      <f:selectItems value="#{YourBean.values}" id="si1"/>
</af:selectOneChoice>

In YourBean.java , you will have a method like:

public List<SelectItem> getValues() {
   if (list == null) {
            list = new ArrayList<SelectItem>();
            list(new SelectItem("val1","Label 1"));
            list(new SelectItem("val2","Label 2"));
            list(new SelectItem("val3","Label 3"));

   }
   return list;

}

This way you will see "Label 3" as default value in your choice list.

Upvotes: 1

Related Questions