7 Replies Latest reply on Sep 10, 2008 4:34 AM by molif

    how to order aplhabetically a selectOneMenu

      I have a h:selectOneMenu, which takes its values from a bean's properties. How can I make them display in alphabetic order?

        • 1. Re: how to order aplhabetically a selectOneMenu
          whitty69

          if you are retrieving the data from a backing bean then you can use any of the standard sorting methods such as sorted sets and comperators to pre-sort your values, then add the selectItems from that pre-sorted list.

          hope this helps.

          • 2. Re: how to order aplhabetically a selectOneMenu

            Actually I take the data from the bean's properties. Like this:

            <s:selectItems value="#{templateManager.template.templateFields}" var="templateField" label="#{templateField.field.parentField.code}.#{templateField.field.code}"/>


            I want to order the items by templateField.field.parentField.code
            I guess I have to do it in the JSF page itself? But how?

            • 3. Re: how to order aplhabetically a selectOneMenu

              Please, help? Is there any tag or something to add to the code?

              • 4. Re: how to order aplhabetically a selectOneMenu

                Can I user some rich faces tag instead of the seam one?

                • 5. Re: how to order aplhabetically a selectOneMenu

                  On situations like this I usually have a

                  List<SelectItem>
                  on my backing bean and that's where my
                  <h:selectOneMenu>
                  gets all its options from. On the backing I have a method that looks kinda like this (simplified and easy-to-understand version lol):
                  private void loadUserList()
                   {
                   backingBeanList = new ArrayList<SelectItem>();
                   List<User> lista = new UserController().retrieveAll();
                  
                   for (User u : lista) {
                   String name = u.getName();
                   backingBeanList .add(new SelectItem(u, name));
                   }
                   }
                  


                  They're automatically displayed in alphabetical order inside the selectOneMenu if I do it like this.
                  Hope this helps.
                  Cya.

                  • 6. Re: how to order aplhabetically a selectOneMenu

                    Thank you for the idea, but unfortunately, it won't work. Here is my code:

                    public List<SelectItem> getFieldsForTemplate() {
                     List<SelectItem> result = new ArrayList<SelectItem>();
                     Set<TemplateField> fields = //retrieve somehow
                    
                     for (TemplateField tf : fields) {
                     String label = tf.getField().getParentField().getCode() + "." + tf.getField().getCode();
                     result.add(new SelectItem(tf, label));
                     }
                     return result;
                    }


                    In the JSP, I have:
                    <h:selectOneMenu id="templateFieldMenu" value="#{newTemplateField}">
                     <f:selectItems value="#{templateManager.fieldsForTemplate}" />
                     <s:convertEntity />
                     </h:selectOneMenu>


                    And finally, in the application, the drop-down menu is still not ordered alphabetically. :(

                    • 7. Re: how to order aplhabetically a selectOneMenu

                      Well, I created my own Comparator and it worked:

                      Collections.sort(result, new Comparator<SelectItem>() {
                       public int compare(SelectItem sItem1, SelectItem sItem2) {
                       String sItem1Label = sItem1.getLabel();
                       String sItem2Label = sItem2.getLabel();
                      
                       return (sItem1Label.compareToIgnoreCase(sItem2Label));
                       }
                       });


                      Thank you however :)
                      Regards