0
votes

I've run into a problem with displaying custom content in my SelectOneMenu. I'm using Primefaces 3.5 right now and I tested the example from the showcase and it works fine, so unfortunately the problem is PEBCAC.
This is my converter code:

@Override
public Object getAsObject(FacesContext ctx, UIComponent component, String id)
{
    groups = getGroups();//gets the groups here
    pType toReturn = new pType();
    if(groups.size()>0){
      toReturn = groups.get(0);
      return toReturn;
     }
   return "";       
}

@Override
public String getAsString(FacesContext fc, UIComponent uic, Object o)
{

    if (o == null || o.equals(""))
    {
        return "---";
    } else
    {           
        try{
            pType val = (pType)o;
            return String.valueOf(val.getRecordID());
        }catch(Exception ex){               
            ex.printStackTrace();
            return "---";
        }
    }
}

This is the xhtml I'm working with right now:

<p:selectOneMenu value="#{controller.selectedVal}"
        rendered="#{controller.showMenu}" effect="fade"
        converter="converter" panelStyle="width:150px" var="p"
        style="width:160px" filter="true" filterMatchMode="contains">
        <f:selectItem itemLabel="Select One" itemValue="" />
        <f:selectItems var="pType"
            itemLabel="#{controller.getNumber(pType)}"
            itemValue="#{pType}" value="#{controller.savedValues}">
                    <p:column>#{p.var1} - #{p.var2}
                    </p:column>
                </f:selectItems>
</p:selectOneMenu>

The controller is a managedbean that is SessionScoped. The variable savedValues is a list of the objects and it does populate with the right data and has data when its called. Right now with the code above, the list just opens up and there's no data inside it. If I switch the var in to "pType" instead of "p", the drop down menu will have a list of the values gotten from controller.getNumber(pType), although there is no custom data being loaded into the menu. But all the examples I've seen show that the variable used is coming from selectOneMenu, which is also something I'm a bit confused on as it seems like it should always be coming from the value field of selectItems.

1

1 Answers

0
votes

First I would try to delete all unnecesarry attributes, like the filter or rendered, and move to more simplicity until you have a working version. This isolates errors more often than you would expect.

Do you have your converter annotated as a FacesConverter with the correct name and class? What exactly does the getGroups() method do, does it contain the right values within the converter? Why does getAsObject always return the first pType?

What should the itemLabel actually be, wouldn't your desired outcome be to display the id or maybe name of the pType?

  • selectOneMenu value: the value of the selected item
  • selectOneMenu var: item that you can access in p:column
  • selectItems itemLabel: text that is displayed in the dropdown menu
  • selectItems itemValue: the actual value that is sent to the bean's setter