2 Replies Latest reply on Sep 24, 2009 1:59 AM by Huang Jun

    rich:combobox key value pair issue?

    Huang Jun Newbie

      Does combobox support key value pair?

      I use below code

      item.setLabel(itemValue);
      item.setValue(itemKey);
      or
      item.setValue(itemKey);
      item.setDescription(itemValue)
      


      It seems only the Value will show in the combobox.
      Did I miss something? Or do I need to write a converter, if I do, How?

      Any suggestions??? Thanks!!!

        • 1. Re: rich:combobox key value pair issue?
          Huang Jun Newbie

          Sorry my code is not complete.

          
          SelectItem item = new SelectItem();
          item.setValue(itemKey);
          item.setDescription(itemValue)
          
          <rich:comboBox width="100px" value="# {retrieveQuotesBusinessContract.quoteType}">
          <f:selectItems value="#{dropdownListDelegator.quoteType}"/>
          </rich:comboBox>
          
          


          • 2. Re: rich:combobox key value pair issue?
            Huang Jun Newbie

            I soloved this issue by using converter.

            Build the SelectItem

            SelectItem item = new SelectItem();
            item.setLabel(itemValue);
            item.setValue(itemKey);
            


            Converter
            package com.avnet.quest.ui.converter;
            
            import java.util.List;
            
            import javax.faces.context.FacesContext;
            import javax.faces.component.UIComponent;
            import javax.faces.component.UISelectItems;
            import javax.faces.model.SelectItem;
            
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.annotations.intercept.BypassInterceptors;
            import org.jboss.seam.annotations.faces.Converter;
            import org.jboss.seam.contexts.Contexts;
            
            import com.avnet.quest.ui.dropdown.DropdownListDelegator;
            
            @Name("comboxLabelConverter")
            @Converter
            @BypassInterceptors
            public class ComboxLabelConverter implements javax.faces.convert.Converter{
            
            
             public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) {
             List<SelectItem> items = retrieveSelectItems(uiComponent);
             if(items!=null){
             for(SelectItem item:items){
             if(item.getLabel().equals(s))
             return item.getValue();
             }
             }
            
             return s;
             }
            
             public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
            
             List<SelectItem> items = retrieveSelectItems(uiComponent);
             if(items!=null){
             for(SelectItem item:items){
             if(item.getValue().equals(value))
             return item.getLabel();
             }
             }
             return value == null? null:"canot convert label for :"+value;
             }
            
            
            
            
             private List<SelectItem> retrieveSelectItems(UIComponent uiComponent) {
             List<SelectItem> items = null;
             if(uiComponent.getChildren()!=null&&!uiComponent.getChildren().isEmpty()
             &&uiComponent.getChildren().get(0) instanceof UISelectItems){
             items = (List<SelectItem>)((UISelectItems)uiComponent.getChildren().get(0)).getValue();
             }
             return items;
             }
            }