1 Reply Latest reply on Dec 21, 2011 3:35 AM by Riccardo Esposito

    HtmlDropDownMenu and HtmlMemuItem

    Riccardo Esposito Newbie

      All,
      I'm fighting to associate an action to the label of a dynamic menu (from a db).
      Can someone help me?


      Follow the code:


      @Stateful
      @Name("dynMenu")
      @Scope(ScopeType.SESSION)
      @Restrict("#{identity.loggedIn}")
      public class DynMenuAction implements IDynMenuAction
      {
      
      private HtmlDropDownMenu gtsMenu = new HtmlDropDownMenu();
      
      ...
      
      public void getDynamicMenu()
           {
                log.info("Creating menu");
                FacesContext fc = FacesContext.getCurrentInstance();
                ELContext el = fc.getELContext();
                gtsMenu.setValue(Utils.getBundle().getString(TITLE).toString());
                // List<LayoutComposition> lc = new ArrayList<LayoutComposition>();
                // lc = dao.getDynMenu(contractId_);
                // log.info("the contract has " + lc.size() + " sections.");
      
      
                for (LayoutComposition component : associatedLayout_)
                {
                     log.info("component code: " + component.getCategoryCode().getCode());
                     HtmlMenuItem menuItem = (HtmlMenuItem) fc.getApplication().createComponent(HtmlMenuItem.COMPONENT_TYPE);//new HtmlMenuItem();
                     menuItem.setValue(component.getTitle().toString());
                     menuItem.setSubmitMode("ajax");
                     if (component.getCategoryCode().getCode()
                               .equals(DynMenuLink.LGSVC.name()))
                     {
                          log.info("categoryCode ::"
                                    + component.getCategoryCode().getCode() + "::");
                          MethodExpression me = fc
                                    .getApplication()
                                    .getExpressionFactory()
                                    .createMethodExpression(
                                              el,
                                              "#{dynMenu.jumpTo('"
                                                        + DynMenuLink.LGSVC.getLink()
                                                                  .toString() + "')}",
                                              null, new Class[]{});
                          menuItem.setActionExpression(me);
                          menuItem.setRendered(true);
      
                     }
                     gtsMenu.getChildren().add(menuItem);
                }
      
      public String jumpTo(ActionEvent actEvent)
           {
                HtmlMenuItem item = (HtmlMenuItem) actEvent.getSource();
                if (item != null)
                {
                     lastItem = item.getValue().toString();
                     log.info("Clicked On: " + lastItem);
                }
                return item.getValue().toString();
           }
      
      ...
      }




      <h:form>
           <rich:panelMenu style="border:1px solid black; width:200px">
                <rich:dropDownMenu direction="bottom-right" jointPoint="tr"
                     id="dynMenu" value="Contract Sections" binding="#{dynMenu.GTSMenu}" mode="ajax"/>
           </rich:panelMenu>
      </h:form>



      This is what I see in page.


      <div class="rich-menu-item rich-menu-item-enabled  " id="j_id41:j_id43:j_id78" onclick="RichFaces.Menu.updateItem(event,this);A4J.AJAX.Submit('j_id41:j_id43',event,{'similarityGroupingId':'j_id41:j_id43:j_id78','parameters':{'j_id41:j_id43:j_id78':'j_id41:j_id43:j_id78'} } )" style="">
           <span class="rich-menu-item-icon " id="j_id41:j_id43:j_id78:icon">
                <img alt="" height="16" src="/smsprj/a4j/g/3_3_3.Finalimages/spacer.gif" width="16">
           </span>
           <span class="rich-menu-item-label" id="j_id41:j_id43:j_id78:anchor">SERVICES SECTION</span>
      </div>



      As you see I get correctly the value (set as label, SERVICE SECTION) but the action don't work (at the moment should be only logged).


      Thanks,
      R.

        • 1. Re: HtmlDropDownMenu and HtmlMemuItem
          Riccardo Esposito Newbie

          I solved it, adding rich:jQuery in the form:


          <h:form>
               <rich:panelMenu style="border:1px solid black; width:200px">
                    <rich:dropDownMenu direction="bottom-right" jointPoint="tr"
                         id="dynMenu" value="Contract Sections" binding="#{dynMenu.GTSMenu}" mode="ajax"/>
               </rich:panelMenu>
          
                  <rich:jQuery selector=".menuItem" query="click(function() {location.href = jQuery(this).find('a').attr('href') })" />
          </h:form>
          



          Finally, in the class where I build the dynamic menu I used an HtmlOutputLink and applied a styleClass on the HtmlMenuItem.


          I hope it will be useful for you.