Building a cascading dropdown menu in Angular 8 using ngFor

Currently, I am working on a code for creating a multi-level dropdown menu. However, the issue is that the output is not displaying as expected, resembling the image above.

Here is my code:

HTML

<div *ngIf="options?.length >= 1" class="dropdown-menu">   
    <a *ngFor="let option of options"
        class="dropdown-item d-flex" 
        (click)="selectOption(option)" 
        [ngStyle]="option.style">
        {{ option.name || option.node_name || option?.product_name || option?.operatorName || option?.batch_number }}
        <div *ngIf="option.sfgProducts.length >= 1" class="dropdown-submenu">
            <a *ngFor="let suboption of option.sfgProducts" 
            class="dropdown-item d-flex">
                {{ suboption.name }}
            </a>
        </div>
    </a>
</div>

SCSS:

.dropdown-menu{
  background: $onAccent;
  color: $accent;
  padding: 0.5rem;
  border-radius: 8px;
  max-height: 50vh;
  overflow-y: auto;
  left:-25px;
}

.dropdown-submenu {
    background: $onAccent;
    color: $accent;
    padding: 0.5rem;
    border-radius: 8px;
    max-height: 50vh;
    overflow-y: auto;
    display: none;
    margin-left: 25px;
}
.dropdown-menu > .dropdown-item:hover > .dropdown-submenu {
    display: block;
  
}
.dropdown-item {
  cursor: pointer;
  min-height: 2rem;
  display: flex;
  align-items: center;
  color: $accent;
  border-radius: 8px;
}

Please refer to the image below:

https://i.sstatic.net/PI4AT.png

I want the dropdown-submenu to be positioned outside of the main dropdown 'a' element instead of being inside it. How can this issue be resolved?

Answer №1

To achieve the desired layout, apply position: relative to the parent element and position: absolute to the child element.


    .dropdown-item {
          position: relative;
          cursor: pointer;
          min-height: 2rem;
          display: flex;
          align-items: center;
          color: $accent;
          border-radius: 8px;
        }
        .dropdown-submenu {
          position: absolute;
          right: 0;
          top: 0;
          background: $onAccent;
          color: $accent;
          padding: 0.5rem;
          border-radius: 8px;
          max-height: 50vh;
          overflow-y: auto;
          display: none;
          margin-left: 25px;
        }

To adjust the alignment of the .dropdown-submenu, modify the values in the right and top properties.

Answer №2

To customize your .dropdown-submenu in the CSS, make sure to include position: absolute;. You can also adjust other properties like width: 100% for stretching and margin-top: 50px to create a slight downward offset from the main menu. Feel free to remove these if they're not needed.

Here's the updated code:

.dropdown-submenu {
   background: $onAccent;
   color: $accent;
   padding: 0.5rem;
   border-radius: 8px;
   max-height: 50vh;
   overflow-y: auto;
   display: none;
   margin-left: 25px;
   margin-top: 50px;
   position: absolute;
   width: 100%;
}

I've provided a Stackblitz with this solution for you to experiment with.

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

Is the new Angular 2 CLI tool incorporating Systemjs?

Seeking Answers Curious about the absence of systemjs.config.js file when using the new Angular2 CLI setup tool. Is there a way to install a basic version of Angular 2 without relying on the CLI tool available on angular.io? Past Experience In the past, ...

Ways to alter the SVG's color upon hovering above the image:

I need help getting the YouTube logo to turn red when I hover over an image with a link. Currently, the color change only occurs when hovering over the logo itself, but I want it to happen when hovering over the entire image. You can see an example of wh ...

PHP tag iteration for unordered list segmentation

My current method of fetching data from MySQL is as shown below: $catmapper = new Application_Model_Mapper_BusinessCategoryMapper(); $result = $catmapper->getBusinessCategory(); To display the results, I use a for loop to iterate through and wrap each ...

Tips for eliminating choices upon button press

How do I remove nested options inside a select element upon button click? I'm not sure where my mistake is, could it be in the for loop? var select = document.getElementById('colorSelect'); var options = document.getElementsByTagName(&ap ...

Is it possible to create a QR Code using Ionic 5?

Is there a way to generate QR Codes in Ionic 5? I attempted it, but keep receiving an error stating that the qrcode element is not recognized. Here is my code: qrcode.html <ion-item> <ion-input type="text" placeholder="My QR d ...

Is it necessary to enable validation for an Angular reactive form toggle?

Can you help with this issue I'm having? I have a radio button that asks the user if they have a massage certificate. If they answer yes, a file input should be displayed. By default, the file input should not be required, but if the user selects yes, ...

Quicker loading times for background images when creating PDFs using wkhtmltopdf

As I work on generating a 5-page PDF from my HTML file using wkhtmltopdf, everything seems to be running smoothly. However, I've encountered an issue when it comes to the time it takes to complete this task, especially when a background image is inclu ...

Delete the initial double line break and then switch the remaining double line breaks to single line breaks

I am facing an issue with some HTML content fetched from an external source. My aim is to specifically target instances of double br tags using jQuery. I want to delete the first occurrence of double br and convert all subsequent occurrences into single br ...

Tips for positioning text on the left and right sides of a div in HTML styling

I am struggling with positioning two pieces of text within a div. Despite having some styling already in place, the text is currently displaying one after the other on the left-hand side. I want to position one piece of text to the left and another to the ...

How can I eliminate the indicator in ngb-carousel using HTML tables rather than images?

Struggling with integrating a rendered HTML table into a carousel within an Angular 5 project. I experimented with the ngb-carousel and achieved the desired result, but now I need to figure out how to remove the indicator at the bottom of the carousel like ...

Trouble with Loading jQuery Files on Beaglebone Server

Working on a wireless robotics project that involves using node.js and jquery to create a webpage "controller". Currently, the code is set up to work with an internet connection because it downloads scripts from internet addresses. The goal is to make it ...

Changing the Text of an Anchor Tag When Clicked in Angular 2

Is there a way to toggle the text between "View" and "Hide" without using JQuery, only Angular? I've tried several methods but none have worked. Can anyone offer guidance on how to achieve this? <a class="history-link view-history-class" id="show- ...

My website, powered by Wordpress, is currently experiencing an issue with excessive whitespace being added to the bottom of the contact form 7 plugin. I am perplexed as to the cause of this issue and

Recently, I've come across an issue with my contact form 7 plugin, which has been integrated into the front-page.php file using the code below: <div id="contact" class="pad-section"> <div class="container"> <h1>Contact Us</ ...

How to Align position:relative Divs Horizontally Using CSS

My goal is to showcase content from my website located at . I want to have the ability to place this content into a designated div where I can easily center or style it as needed. The issue arises when attempting to float the content, as it doesn't fu ...

What is the trick to utilizing CSS transitions to enlarge the font size of a Bootstrap card when it is hovered over

I'm having trouble increasing the font size and changing the color on hover for text inside a Bootstrap card. My current code only changes the color without adjusting the font size or implementing any transitions. I don't have much experience wit ...

Verify that all elements sharing a common class are concealed

I'm facing a simple issue where I have a list with identical class names. When one of them is clicked, it animates out of the container and then redirects you. The list also includes a hide button. My goal is to receive a browser alert when all items ...

Utilize the <wbr> tag within FormattedMessage and assign it as a value while coding with TypeScript

Trying out the optional word break tag <wbr> in a message within <FormattedMessage id="some:message" />. Context Some words or texts are too lengthy for certain parent elements on smaller mobile screens, and we have a column layout t ...

Having issues with -ms-filter not functioning in IE while using Angular 4

Having trouble with filters not functioning in IE browser when using angular 4. Below is the CSS code snippet: .style{-ms-filter: grayscale(50%); -ms-filter: brightness(70%);} ...

Step-by-step guide: Crafting an UP Arrow solely with HTML and CSS

What is the method to utilize solely HTML and CSS for creating a triangle? I am in need of constructing a thick triangle, but want to achieve this using only CSS So far, I have experimented with the following code: .arrow-up { width: 0; height: 0 ...

Making a switch from one image to another - JavaScript

I need a solution to swap out an image that is used in multiple locations on a webpage. Consider this sample HTML page: <html> <head> </head> <body> <img id="1" src="example.com/img/1889.png"> <d ...