1 2 3 4 Previous Next 49 Replies Latest reply on Jul 12, 2011 1:41 AM by pankaj_jboss

    How to build rich:dropDownMenu dynamically

    tomarenz

      Hi, subject says all. I would build a rich:dropDownMenu through items coming from a config file. Neither rich:dropDownMenu nor rich:menuItem seem to accept contents coming from a bean. All examples report a static tree layout of items.
      I guess it should be possible, any suggestion ?
      Thanks -- Renzo

        • 1. Re: How to build rich:dropDownMenu dynamically

          You can create any components hierarchy using JSF API. rich:dropDownMenu and rich:menuItem are both standard JSF components. So, the common approach is acceptable.

          • 2. Re: How to build rich:dropDownMenu dynamically
            tomarenz

            Sergey, I need to create rich:dropDownMenu the normal way, e.g. included into an .xhtml component (I use Facelets).
            Only item contents should be create dynamically. I guess I can use the binding attribute from rich:dropDownMenu, then attach java-created items to it from inside the bean.
            Is that correct or is there another approach ?

            • 3. Re: How to build rich:dropDownMenu dynamically
              tomarenz

              As a further hint, the component HtmlDropDownMenu has no method/attribute to deal with item contents, even if I can create it from bean binding.
              Value get/set manage the text label, according to docs.
              I don't see any way to deal with menu contents from java.

              • 4. Re: How to build rich:dropDownMenu dynamically

                I think you should be able to create new HtmlMenuItems and add them to the children collection of the HtmlDropDownMenu using a standard JSF component approach. Something like:

                // assuming you have a dropDownMenu variable already
                
                Application app = FacesContext.getCurrentInstance().getApplication();
                HtmlMenuItem item= (HtmlMenuItem) app.createComponentHtmlMenuItem.COMPONENT_TYPE);
                item.setId("...");
                item.setValue("..");
                
                dropDownMenu.getChildren().add(item);
                


                Is that what you're looking for?

                • 5. Re: How to build rich:dropDownMenu dynamically
                  tomarenz

                  Yes, exactly, thanks. But why not just:

                  HtmlMenuItem item = new HtmlMenuItem();
                  item.set ....
                  dropDownMenu.getChildren().add(item);
                  

                  Moreover, when should I perform this filling ? I guess during the setter call associated to the binding attribute, not sure though.

                  -- Renzo


                  • 6. Re: How to build rich:dropDownMenu dynamically

                    I'm not sure what the createComponent method does, if anything, that just newing up your own object won't do, that's just how I've always seen it done.

                    In the setter of the binding is a good place to dynamically add them, although depending on the scope of your bean you might only need to do it if you haven't already.

                    • 7. Re: How to build rich:dropDownMenu dynamically
                      tomarenz

                      Thanks, I will investigate further.
                      Funny enough, I already have a similar solution based on Tomahawk jscookmenu, where I add menu items on the fly, using simply:

                       item = new NavigationMenuItem(label, null);
                       item.setValue(topic);
                       subMenu.add(item);
                      ...
                      

                      and so on, building up the menu item tree. Unfortunately, that component doesn't support disabled (grayed) items, that's why I'm looking at rich:dropDownMenu.


                      • 8. Re: How to build rich:dropDownMenu dynamically

                        Hello:

                        I have successfully followed your indications for building a dropDownMenu dynamically, but I have problems assigning an action to a HtmlMenuItem.

                        Here is the page code:

                        <rich:dropDownMenu binding="#{menuBean.myMenu}" >
                        </rich:dropDownMenu>


                        Here is the Java code in the menuBean:

                        private HtmlDropDownMenu getMyMenu() {
                         HtmlDropDownMenu menu = new HtmlDropDownMenu();
                         menu.setStyle("border:1px solid #BCD1FF");
                         menu.setValue("Security Actions");
                        
                         HtmlMenuItem menuItem = new HtmlMenuItem();
                         String subOption = "Sec. Change Passsword";
                         menuItem.setValue(subOption);
                        
                         Application app = FacesContext.getCurrentInstance().getApplication();
                         MethodBinding mb = app.createMethodBinding ("#{otherBean.action}", null);
                         menuItem.setAction (mb);
                        
                         menu.getChildren().add(menuItem);
                         return menu;
                         }



                        The problem is that the class MethodBinding and the method setAction of HtmlMenuItem class are deprecated.

                        Do you know another way for assigning an action to a HtmlMenuItem ?

                        Thanks in advance

                        Maria Consuelo Franky

                        • 9. Re: How to build rich:dropDownMenu dynamically
                          alexsmirnov

                          JSF 1.2 deprecate MethodBinding/ValueBinding classes.
                          For a JSF UICommand - subclasses ( DropDownMenu is extend UICommand, too ) should be used MethodExpression / setActionExpression :

                          • 10. Re: How to build rich:dropDownMenu dynamically

                            In this context, "deprecated" means JSF 1.2 uses another way for the same thing. However, RichFaces does support both JSF 1.1 and JSF 1.2. To reach this compatibility we have to use the methods that will work in both cases (i.e the ones that marked as "deprecated").

                            When RichFaces starts to declare support JSF 1.2 only, it will stops to use the old methods. Currently, it is just does not make sense.

                            • 11. Re: How to build rich:dropDownMenu dynamically

                              you may use setActionExpression() instead.

                              • 12. Re: How to build rich:dropDownMenu dynamically

                                Wow, I love the situation when all 3 of us answered!

                                • 13. Re: How to build rich:dropDownMenu dynamically
                                  emsa

                                  I have also had some trouble doing this in a gui-designer friendly way.

                                  Why not add a repeating-menuItem?

                                  <rich:repeatingMenuItem var="v" value="#{backing.items}">
                                   <rich:menuItem value="#{v.label}" action="#{home.select(v.id)}" />
                                  </rich:repeatingMenuItem>


                                  or similar?

                                  • 14. Re: How to build rich:dropDownMenu dynamically
                                    garypinkham

                                    Is there a working sample on how to do this? I was reading the Dev Guide and they show this example that just doesn't seem to be enough. :-)

                                    ...
                                    org.richfaces.component.html.HtmlMenuItem myItem = new org.richfaces.component.html.HtmlMenuItem ();
                                    ...
                                    


                                    1 2 3 4 Previous Next