What is the best way to implement a cascading menu with Angular material elements?

Looking to create a navigation menu that opens another menu alongside it.

I've successfully set up a material nav list with a menu, but I'm struggling to position the second menu to the right of the initial navigation list as per my design. I tried using margins on the menu elements without success

Here's the code snippet:

<mat-nav-list>
  <button
    mat-button
    [matMenuTriggerFor]="menu"
    aria-label="Example icon-button with a menu"
  >
    <span mat-line>Item 1</span>
  </button>
  <div>
    <button
      mat-button
      [matMenuTriggerFor]="menu"
      aria-label="Example icon-button with a menu"
    >
      <span mat-line>Item 2</span>
    </button>
  </div>
  <div>
    <button
      mat-button
      [matMenuTriggerFor]="menu"
      aria-label="Example icon-button with a menu"
    >
      <span mat-line>Item 3</span>
    </button>
  </div>
  <div>
    <button
      mat-button
      [matMenuTriggerFor]="menu"
      aria-label="Example icon-button with a menu"
    >
      <span mat-line>Item 4</span>
    </button>
  </div>

  <mat-menu #menu="matMenu" class="sidemenu">
    <button mat-menu-item>
      <span>Subitem 1</span>
    </button>
    <button mat-menu-item>
      <span>Subitem 2</span>
    </button>
    <button mat-menu-item>
      <span>Subitem 3</span>
    </button>
  </mat-menu>
</mat-nav-list>

Answer №1

To create a menu that triggers another menu, you can nest the menus without using a nav list.

Check out this code example:

    <mat-menu #rootMenu="matMenu">
      <button mat-menu-item [matMenuTriggerFor]="subMenu">
        <span>Item 1</span>
      </button>
      <button mat-menu-item [matMenuTriggerFor]="subMenu">
        <span>Item 2</span>
      </button>
      <button mat-menu-item [matMenuTriggerFor]="subMenu">
        <span>Item 3</span>
      </button>
      <button mat-menu-item [matMenuTriggerFor]="subMenu">
        <span>Item 4</span>
      </button>
    </mat-menu>

    <mat-menu #subMenu="matMenu" class="sidemenu">
      <button mat-menu-item>
        <span>Subitem 1</span>
      </button>
      <button mat-menu-item>
        <span>Subitem 2</span>
      </button>
      <button mat-menu-item>
        <span>Subitem 3</span>
      </button>
    </mat-menu>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Assessing the value of a variable or expression embedded within a string in Sass

Creating a mixin to set a div to transparent requires special attention to evaluate a variable in a string. The -ms-filter line must be quoted and should include the result of a calculation ($amount * 100). How can I successfully incorporate this into my ...

Is it possible to adjust text wrapping based on the size of the screen?

I am currently facing an issue with my modal form where the text does not wrap properly depending on the size of the modal form. I need help with correcting this using CSS. My current approach involves using angular with new lines and hardcoding line brea ...

Cancel the starting path in Angular 9 and initiate a redirection

I am attempting to change the initial path such as localhost:4200/ so that it displays an error view instead of the home page. I want the home page to only be accessible if you navigate to something like localhost:4200/tag1/tag2. I need to be able to capt ...

Obtain all the data in the table cells using Jquery

I've been tasked with a unique challenge. There's a table with a form inside it, and I'm using Jquery to handle the form. Is there a way to retrieve the data from the <td></td> element without any specific id or class? The situa ...

Presenting CSV data in a tabular format using Angular and TypeScript

I am facing an issue with uploading a CSV file. Even though the table adjusts to the size of the dataset, it appears as if the CSV file is empty. Could this be due to an error in my code or something specific about the CSV file itself? I'm still learn ...

Tips for Centering a div When Dynamically Adding Contents

Take a look at this image https://i.sstatic.net/NUwDQ.png I utilized Bootstrap to align the divs and created only one div. The contents within this div are added dynamically from the admin side, then looped through. It's functioning properly. Howeve ...

The sticky table header is being covered by a disabled HTML button, while the active buttons are not visible

I'm currently working on a website using Bootstrap 5 as my framework. The issue I'm facing is that when scrolling down the page and keeping the table header stuck to the top, the disabled button ends up showing over the header. I attempted to us ...

What's the deal with this loading GIF layout problem?

Styling with CSS: .smallLoaderBackground { background-image: url('loaderBackground.gif') !important; background-position: center center; background-repeat: no-repeat; position: fixed; left: 50%; top: 50%; height: 50px ...

Cross-Platform: Varied functionalities in disabled input fields (avoiding any direct replication)

My input HTML field is disabled, but I've noticed that in some browsers (such as Chrome, Edge, Internet Explorer, and Opera), it's still possible to select and copy the text. However, in Firefox, this functionality does not work. To test this yo ...

Problems Arising with Bootstrap Media Queries on Smaller Tablets in Landscape View

I am currently using Bootstrap's standard media queries for my project and they are working perfectly, except for small tablets in landscape mode. I have included an image to demonstrate this issue. The problem seems to arise from the custom CSS that ...

Failing Cypress component test: When leveraging an index.ts file for importing and exporting services

Tech stack: Angular v15 and Cypress v12. My example component that I'm testing: import { Component } from '@angular/core'; import { UserHttp } from '../../services'; @Component({ selector: 'example-view', templateUr ...

Using a nested table will override the "table-layout: fixed" property

I'm currently working on creating a tabular layout and I'm in need of the following: 2 columns with variable widths Columns that are equal in width Columns that are only as wide as necessary After some research, I discovered that by setting "t ...

When utilizing the --watch flag, Node-sass will fail to function properly

Encountering an issue with the "--watch" or "-w" flag when running node-sass. After installing node-sass as a devDependency and setting up a script to compile SCSS code, everything runs smoothly without any flags. However, adding the "--watch" flag causes ...

Is there a way to convert a File into a byte array and then save it in a database using Angular and ASP.Net Core?

Hey everyone, I'm fairly new to working with Angular and I've hit a roadblock when trying to implement file-upload functionality in my Angular application. The technologies I am using include Angular, ASP.Net Core, and Sqlserver. I am tasked wi ...

The issue with Ionic 2 arises when attempting to use this.nav.push() because it

Recently transitioning from Ionic 1 to Ionic 2, I'm encountering an issue with using this.nav.push(nameOftheOtherPage) immediately after a promise. Interestingly, the function this.nav.push works fine when it's called outside of the promise funct ...

What are some techniques for styling a label element with an empty for attribute using CSS?

Here is what I currently have: <label class="filter-label" for="pv[]181"> ::before white <span class="count">5</span> ::after </label> My goal is to modify the ::after element within that label, but so far my attempts ...

Struggling to designate the active button on an HTML/CSS webpage

As a beginner in the world of HTML and CSS, I'm facing some challenges with making buttons appear active. My goal is to have button1 highlighted by default when it's selected, and upon clicking on button2, the content above should change successf ...

Create a scrollable div within a template

After discovering a template that I want to use for my personal project, I noticed a missing element... There's a div where content can be added. That's fine, but what if I add more content than fits in the space provided? The whole website beco ...

Extract the content from the span element on Whatsapp Web

Currently, I am in the process of learning Python along with Selenium and have encountered a challenge. I am attempting to extract the username from the most recent message in a WhatsApp group conversation. Despite several attempts, I have been unsuccessfu ...

Alignment of Bootstrap 5 card text and buttons

I've been diving into the BootStrap 5 Crash Course from this YouTube link: https://www.youtube.com/watch?v=4sosXZsdy-s. The final website from the tutorial can be found here: One specific part focuses on using Cards, but I'm facing an issue wher ...