Class SideNavItem

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.sidenav.SideNavItem
All Implemented Interfaces:
AttachNotifier, DetachNotifier, HasElement, HasEnabled, HasStyle, HasPrefix, HasSuffix, Serializable

@Tag("vaadin-side-nav-item") @NpmPackage(value="@vaadin/side-nav", version="24.6.3") @JsModule("@vaadin/side-nav/src/vaadin-side-nav-item.js") public class SideNavItem extends Component implements HasEnabled, HasPrefix, HasSuffix
A menu item for the SideNav component.

Besides the target path it can contain a label, prefix and suffix component, like icons or badges. You can create a hierarchical navigation structure by adding other SideNavItem instances to this SideNavItem instance via addItem(SideNavItem...).

Author:
Vaadin Ltd
See Also:
  • Constructor Details

    • SideNavItem

      public SideNavItem(String label)
      Creates a menu item which does not link to any view but only shows the given label.
      Parameters:
      label - the label for the item
    • SideNavItem

      public SideNavItem(String label, String path)
      Creates a new menu item using the given label that links to the given path.
      Parameters:
      label - the label for the item
      path - the path to link to
    • SideNavItem

      public SideNavItem(String label, Class<? extends Component> view)
      Creates a new menu item using the given label that links to the given view.
      Parameters:
      label - the label for the item
      view - the view to link to
    • SideNavItem

      public SideNavItem(String label, Class<? extends Component> view, RouteParameters routeParameters)
      Creates a new menu item using the given label that links to the given view.
      Parameters:
      label - the label for the item
      routeParameters - the route parameters
      view - the view to link to
    • SideNavItem

      public SideNavItem(String label, String path, Component prefixComponent)
      Creates a new menu item using the given label and prefix component (like an icon) that links to the given path.
      Parameters:
      label - the label for the item
      path - the path to link to
      prefixComponent - the prefix component for the item (usually an icon)
    • SideNavItem

      public SideNavItem(String label, Class<? extends Component> view, Component prefixComponent)
      Creates a new menu item using the given label and prefix component (like an icon) that links to the given view.
      Parameters:
      label - the label for the item
      view - the view to link to
      prefixComponent - the prefixComponent for the item (usually an icon)
    • SideNavItem

      public SideNavItem(String label, Class<? extends Component> view, RouteParameters routeParameters, Component prefixComponent)
      Creates a new menu item using the given label and prefix component (like an icon) that links to the given view.
      Parameters:
      label - the label for the item
      view - the view to link to
      routeParameters - the route parameters
      prefixComponent - the prefixComponent for the item (usually an icon)
  • Method Details

    • setupSideNavItem

      protected void setupSideNavItem(SideNavItem item)
      Implement this method to set up/modify the SideNavItem right before it's added to the list of the navigation items.
      Parameters:
      item - Item to be set up
    • getLabel

      public String getLabel()
      Gets the label of this menu item.
      Returns:
      the label or null if no label has been set
    • setLabel

      public void setLabel(String label)
      Set a textual label for the item.

      The label is also available for screen reader users.

      Parameters:
      label - the label text to set; or null to remove the label
    • setPath

      public void setPath(String path)
      Sets the path in a form or a URL string this navigation item links to. Note that there is also an alternative way of how to set the link path via setPath(Class).
      Parameters:
      path - The path to link to. Set to null to disable navigation for this item.
      See Also:
    • setPath

      public void setPath(Class<? extends Component> view)
      Retrieves Route and RouteAlias annotations from the specified view, and then sets the corresponding path and path aliases for this item.

      Note: Vaadin Router will be used to determine the URL path of the view and this URL will be then set to this navigation item using setPath(String).

      Parameters:
      view - The view to link to. The view should be annotated with the Route annotation. Set to null to disable navigation for this item.
      See Also:
    • setPath

      public void setPath(Class<? extends Component> view, RouteParameters routeParameters)
      Retrieves Route and RouteAlias annotations from the specified view, and then sets the corresponding path and path aliases for this item.

      Note: Vaadin Router will be used to determine the URL path of the view and this URL will be then set to this navigation item using setPath(String).

      Parameters:
      view - The view to link to. The view should be annotated with the Route annotation. Set to null to disable navigation for this item.
      routeParameters - the route parameters
      See Also:
    • getPath

      public String getPath()
      Gets the path this navigation item links to.
      Returns:
      path this navigation item links to
    • setQueryParameters

      public void setQueryParameters(QueryParameters queryParameters)
      Sets the QueryParameters of this item.

      The query string will be generated from QueryParameters.getQueryString() and will be appended to the path attribute of this item.

      Parameters:
      queryParameters - the query parameters object, or null to remove existing query parameters
    • getPathAliases

      public Set<String> getPathAliases()
      Gets the path aliases for this item.
      Returns:
      the path aliases for this item, empty if none
    • setPathAliases

      public void setPathAliases(Set<String> pathAliases)
      Sets the specified path aliases to this item. The aliases act as secondary paths when determining the active state of the item.

      Note that it is allowed to pass null as value to clear the selection.

      Parameters:
      pathAliases - the path aliases to set to this item
    • getTarget

      public String getTarget()
      Gets the target of this item.
      Returns:
      the target of this item
    • setTarget

      public void setTarget(String target)
      Where to display the linked URL, as the name for a browsing context.

      The following keywords have special meanings for where to load the URL:

      • _self: the current browsing context. (Default)
      • _blank: usually a new tab, but users can configure browsers to open a new window instead.
      • _parent: the parent browsing context of the current one. If no parent, behaves as _self.
      • _top: the topmost browsing context (the "highest" context that?s an ancestor of the current one). If no ancestors, behaves as _self.

      Parameters:
      target - the target of this item
    • isMatchNested

      public boolean isMatchNested()
      Gets whether this item also matches nested paths / routes.
      Returns:
      true if this item also matches nested paths / routes, false otherwise
    • setMatchNested

      public void setMatchNested(boolean value)
      Sets whether to also match nested paths / routes. false by default.

      When enabled, an item with the path /path is considered current when the browser URL is /path, /path/child, /path/child/grandchild, etc.

      Note that this only affects matching of the URLs path, not the base origin or query parameters.

      Parameters:
      value - true to also match nested paths / routes, false otherwise
    • isRouterIgnore

      public boolean isRouterIgnore()
      Returns:
      true if this item should be ignored by the Vaadin router and behave like a regular anchor.
    • setRouterIgnore

      public void setRouterIgnore(boolean ignore)
      The routing mechanism in Vaadin by default intercepts all side nav items with a relative URL. This method can be used to make the router ignore this item. This makes it behave like a regular anchor, causing a full page load.
      Parameters:
      ignore - true if this item should not be intercepted by the single-page web application routing mechanism in Vaadin.
    • setOpenInNewBrowserTab

      public void setOpenInNewBrowserTab(boolean openInNewBrowserTab)
      Sets whether the target URL should be opened in a new browser tab.

      This is a convenience method for setting the target to _blank. See setTarget(String) for more information.

      Parameters:
      openInNewBrowserTab - true if the target URL should be opened in a new browser tab, false otherwise
    • isOpenInNewBrowserTab

      public boolean isOpenInNewBrowserTab()
      Gets whether the target URL should be opened in a new browser tab.
      Returns:
      true if the target URL should be opened in a new browser tab, false otherwise
    • setExpanded

      public void setExpanded(boolean expanded)
      Sets the expanded status of the item.
      Parameters:
      expanded - true to expand the item, false to collapse it
    • isExpanded

      @Synchronize(property="expanded", value="expanded-changed") public boolean isExpanded()
      Returns whether the side navigation menu item is expanded or collapsed.
      Returns:
      true if the item is expanded, false if collapsed
    • addItem

      public void addItem(SideNavItem... items)
      Adds navigation menu item(s) to the menu.
      Parameters:
      items - the navigation menu item(s) to add
    • addItemAsFirst

      public void addItemAsFirst(SideNavItem item)
      Adds the given navigation item as the first child of this navigation item.
      Parameters:
      item - the item to add, value must not be null
    • addItemAtIndex

      public void addItemAtIndex(int index, SideNavItem item)
      Adds the given item as child of this navigation item at the specific index.
      Parameters:
      index - the index, where the item will be added. The index must be non-negative and may not exceed the children count
      item - the item to add, value must not be null
    • getItems

      public List<SideNavItem> getItems()
      Gets the items added to this navigation item (the children of this component that are instances of SideNavItem). This doesn't include the items added to the children of this navigation item.
      Returns:
      the child SideNavItem instances in this navigation menu
      See Also:
    • remove

      public void remove(SideNavItem... items)
      Removes the menu item(s) from the menu.

      If the given menu item is not a child of this menu, does nothing.

      Parameters:
      items - the menu item(s) to remove
    • removeAll

      public void removeAll()
      Removes all navigation menu items from this item.