0

can anyone please tell me how to set a default value to the primefaces dropdown. the defauklt value will be coming from the datatable..

<h:outputLabel for="leaveType" value="Type of Leave *:" styleClass="tLabel"/>
<p:selectOneMenu id="leaveType" value="#{leaveBean.selectedLeave.leaveType}" disabled="#{leaveBean.selectedLeave.status!='Pending'}" >
<f:selectItem itemLabel="#{leaveBean.selectedLeave.leaveType}" itemValue="" />
<f:selectItems value="#{leaveBean.leaveDTO.infoBeanList}" var="lType" itemValue="#{lType.leaveTypeCode}@#{lType.leaveType}" itemLabel="#{lType.leaveType}" />
</p:selectOneMenu>
MZimmerman6
  • 8,445
  • 10
  • 40
  • 70
  • Where is the code you're working with? – kolossus Oct 30 '13 at 06:48
  • Just initialize `leaveBean.selectedLeave.leaveType` to a value in the managed bean, preferably in a `@PostConstruct` method – kolossus Oct 30 '13 at 07:17
  • There is a datatable ..once user selects a row from the datatable then the value of the dropdown need to be populated based on the value from the datatbale and also the dropdown initially contains some values –  Oct 30 '13 at 08:35

1 Answers1

0

You can use this one.

public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {
    int size = selectOne ? entities.size() + 1 : entities.size();
    SelectItem[] items = new SelectItem[size];
   int i =0;
    for (Object x : entities) {
      items[i++] = new SelectItem(x, x.toString());
    }
    return items;
  }

List<?> entities will be the list result.

and then make sure you have the code below in your entity class.

@Override
  public String toString() {
    return this.name;
  }
blitzen12
  • 1,350
  • 3
  • 24
  • 33