PanelMenu – hybrid of accordion and tree

A panel menu is a hybrid of the accordion and tree components used for navigation and action. It renders a vertical menu structure with support for nested menu items.

In this recipe, we will develop a panel menu with three top submenus acting as accordion tabs and nested menu items with a tree-like look and feel.

How to do it…

A panel menu is rendered by the p:panelMenu tag. Top-level submenus define accordion-like tabs. A click on such a tab expands or collapses the subordinated content. The menu structure is similar to every PrimeFaces' menu component—it consists of submenus and menu items. Menu items can call actions, action listeners, or trigger navigations. The following code shows this:

<p:panelMenu style="width:200px">
  <p:submenu label="Ajax Operations">
    <p:menuitem value="Save"
      actionListener="#{panelMenuBean.save}"
      icon="ui-icon-disk"/>
    <p:menuitem value="Update"
      actionListener="#{panelMenuBean.update}"
      icon="ui-icon-arrowrefresh-1-w"/>
  </p:submenu>
  <p:submenu label="Non-Ajax Operations">
    <p:menuitem value="Delete"
      actionListener="#{panelMenuBean.delete}"
      ajax="false" icon="ui-icon-close"/>
  </p:submenu>
  <p:separator/>
  <p:submenu label="Navigations">
    <p:submenu label="Links" icon="ui-icon-extlink">
      <p:submenu label="Prime Products">
        <p:menuitem value="Prime UI" icon="ui-icon-home"
          url="http://primefaces.org/primeui"/>
        <p:menuitem value="Prime Mobile" icon="ui-icon-signal"
          url="http://primefaces.org/showcase/mobile/index.xhtml"/>
      </p:submenu>
      <p:submenu label="Prime Resources">
        <p:menuitem value="Docs" icon="ui-icon-document"
          url="http://primefaces.org/documentation.html"/>
        <p:menuitem value="Download" icon="ui-icon-arrowthick-1-s"
          url="http://primefaces.org/downloads.html"/>
      </p:submenu>
    </p:submenu>
  </p:submenu>
</p:panelMenu>

The result of the preceding code listing is shown in the following screenshot. This screenshot demonstrates the same menu in two states—completely collapsed on the left-hand side and completely expanded on the right-hand side.

How to do it…

How it works…

We see that direct children of p:panelMenu are normally several p:submenu tags with labels describing the accordion-like tabs. Tabs can be styled by the .ui-panelmenu h3 selector. In this recipe, we will use the following definition:

.ui-panelmenu h3 {
  font-size: 1em;
}

As the menu uses menu items, it is easy to invoke actions or action listeners with or without AJAX (ajax="false") as well as navigate. Navigation means that a GET request causes a switch to another page. This is always a full page refresh and only works when the url attribute on p:menuitem is set. In this case, the menu item is rendered as a normal HTML link element. If the url attribute is missing, only POST requests (with AJAX or without) can be sent.

There's more…

The panelMenu component keeps the open or closed state of submenus across web pages. The state is saved in cookies. This means that when the user enters the same page again, panelMenu will be displayed in the same state as when he/she had last interacted with it. This feature is pretty useful, but sometimes, it is not desirable, for example, when multiple users with different accounts work on the same PC. In this case, we can either call the widget method clearState() (a client-side solution) or clear the cookie on logging out in the response (a server-side solution). The cookie name is panelMenu-<id>, where <id> is the client ID of p:panelMenu.

PrimeFaces Cookbook Showcase application

This recipe is available in the demo web application on GitHub (https://github.com/ova2/primefaces-cookbook/tree/second-edition). Clone the project if you have not done it yet, explore the project structure, and build and deploy the WAR file on application servers compatible with Servlet 3.x, such as JBoss WildFly and Apache TomEE.

The showcase for the recipe is available at http://localhost:8080/pf-cookbook/views/chapter6/panelMenu.jsf.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.3.167