0

i have a Hashtable i want to display it in a selecteOneMenu any idea how to do it ?

itemCat = new Hashtable<Integer,String>();
    itemCat.put(0,"Functional");
    itemCat.put(1, "Interoperability");
    itemCat.put(2, "Load");
    itemCat.put(3, "Performance");
    itemCat.put(4, "Disponibility");
    itemCat.put(5, "Security");
    itemCat.put(6, "Usability");
    itemCat.put(7, "Other");

 <p:selectOneMenu value="#{projectRequirementManagementMB.selectedCat}" filter="true" filterMatchMode="startsWith" >  
                                       <f:selectItem itemLabel="Select One" itemValue="" />  
                                <f:selectItems value=" {projectRequirementManagementMB.hereShouldbeCategoryValues}" />  
                                     </p:selectOneMenu>  

Any idea will be apprecited

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Amira
  • 3,184
  • 13
  • 60
  • 95

1 Answers1

1

The <f:selectItems> already supports the Map interface:

<f:selectItems value="#{projectRequirementManagementMB.itemCat}" />

with just

public Map<Integer, String> getItemCat() {
   return itemCat;
}

However, the map keys are interpreted as item labels while the map values are interpreted as item values. If you can't swap the map keys/values in the model, then you need to swap them in the view in <f:selectItems> as follows, provided that your environment supports EL 2.2 (your question history confirms this).

<f:selectItems value="#{projectRequirementManagementMB.itemCat.entrySet()}" 
     var="entry" itemValue="#{entry.key}" itemLabel="#{entry.value}" />

See also:


Unrelated to the concrete problem, a Hashtable is a rather legacy data structure (from Java 1.0, 1996!!), you should be using its successor HashMap instead. However, this has in turn like as the Hashtable another problem: the items by nature not ordered at all. You'd like to use LinkedHashMap if you'd like to maintain insertion order, or TreeMap if you'd like to have automatic sorting on keys.

itemCat = new LinkedHashMap<Integer, String>();
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555