Menu fetched from menumodel does not start expanded

Post Reply
nbs
Posts: 5
Joined: 19 Feb 2018, 14:44

12 Mar 2018, 07:32

I have loaded the sidebar menu using a menumodel
<h:form id="menuform">
<pc:menu widgetVar="me" model="${mainMenuModel.model}" />
</h:form>
In the menuodel I have set it as expanded but when the application starts the menu is minimized.

Code: Select all

DefaultSubMenu fileMenu = new DefaultSubMenu("MENU");
fileMenu.setId("fileItem0");
fileMenu.setExpanded(true);
for (Dynamicmenu dMenu : dMenuList) {
            if (dMenu.isParent()) {
                DefaultSubMenu pMenu = new DefaultSubMenu(dMenu.getLabel());
                String menuIdTag = dMenu.getLabel().replace(" ", "_") + "_" + id;
                pMenu.setId(menuIdTag);
                pMenu.setIcon("fa fa-fw fa-bars");
                if (getSubmenus(dMenu, pMenu, formIDs) > 0) {                    
                    fileMenu.addElement(pMenu);
                }
           }
      ....... add more menuItem or submenus depending on certain conditions
}
 
This doesn't seem to work, did I miss something or is there another way to make the menu start in expanded mode?

User avatar
aragorn
Posts: 2985
Joined: 29 Jun 2013, 12:38

12 Mar 2018, 13:36

Could you please try it after making the following changes in CaliforniaMenuRenderer.java;

Code: Select all

public class CaliforniaMenuRenderer extends BaseMenuRenderer {
   ...

   protected void encodeElement(FacesContext context, AbstractMenu menu, MenuElement element) throws IOException {
        ResponseWriter writer = context.getResponseWriter();

        if (element.isRendered()) {
            if (element instanceof MenuItem) {
                MenuItem menuItem = (MenuItem) element;
                String menuItemClientId = (menuItem instanceof UIComponent) ? menuItem.getClientId() : menu.getClientId(context) + "_" + menuItem.getClientId();
                String containerStyle = menuItem.getContainerStyle();
                String containerStyleClass = menuItem.getContainerStyleClass();

                writer.startElement("li", null);
                writer.writeAttribute("id", menuItemClientId, null);
                writer.writeAttribute("role", "menuitem", null);

                if (containerStyle != null) {
                    writer.writeAttribute("style", containerStyle, null);
                }
                if (containerStyleClass != null) {
                    writer.writeAttribute("class", containerStyleClass, null);
                }

                encodeMenuItem(context, menu, menuItem);

                writer.endElement("li");
            } else if (element instanceof Submenu) {
                Submenu submenu = (Submenu) element;
                String submenuClientId = (submenu instanceof UIComponent) ? ((UIComponent) submenu).getClientId() : menu.getClientId(context) + "_" + submenu.getId();
                String style = submenu.getStyle();
                String styleClass = submenu.getStyleClass();
                boolean isExpanded = submenu.isExpanded();                                                                           // ADDED THIS LINE
                String defaultStyleClass = "ui-menuitem-submenu";
                styleClass = styleClass != null ? styleClass + " " + defaultStyleClass : defaultStyleClass;
                styleClass = isExpanded ? styleClass + " active-menuitem" : styleClass;                               // ADDED THIS LINE

                writer.startElement("li", null);
                writer.writeAttribute("id", submenuClientId, null);
                writer.writeAttribute("role", "menuitem", null);
                writer.writeAttribute("class", styleClass, null);

                if (style != null) {
                    writer.writeAttribute("style", style, null);
                }

                encodeSubmenu(context, menu, submenu);

                writer.endElement("li");
            } else if (element instanceof Separator) {
                encodeSeparator(context, (Separator) element);
            }
        }
    }

    protected void encodeSubmenu(FacesContext context, AbstractMenu menu, Submenu submenu) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        String icon = submenu.getIcon();
        String label = submenu.getLabel();
        int childrenElementsCount = submenu.getElementsCount();
        boolean isExpanded = submenu.isExpanded();

        writer.startElement("a", null);
        writer.writeAttribute("href", "#", null);

        encodeItemIcon(context, icon);

        if (label != null) {
            writer.startElement("span", null);
            writer.writeText(label, null);
            writer.endElement("span");

            if (submenu instanceof UISubmenu) {
                encodeBadge(context, ((UISubmenu) submenu).getAttributes().get("badge"));
            }
            encodeToggleIcon(context, submenu, childrenElementsCount);
        }

        writer.endElement("a");

        //submenus and menuitems
        if (childrenElementsCount > 0) {
            writer.startElement("ul", null);
            writer.writeAttribute("role", "menu", null);

            /********************* ADDED THESE LINES ********************/
            if (isExpanded) {
                writer.writeAttribute("style", "display:block;", null);
            }
            /***************************************************************/

            encodeElements(context, menu, submenu.getElements());
            writer.endElement("ul");
        }
    }
    
    ...
}

nbs
Posts: 5
Joined: 19 Feb 2018, 14:44

13 Mar 2018, 12:00

That worked.
Thanks.

User avatar
aragorn
Posts: 2985
Joined: 29 Jun 2013, 12:38

13 Mar 2018, 14:08

Glad to hear, thanks a lot!

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest