Tips for maintaining the select dropdown open while opening the mat-menu in Angular

I am working with a mat-select component that has custom options including an additional menu item "Set as default":

<mat-form-field appearance="fill">
  <mat-label>Favorite food</mat-label>
  <mat-select>
    <mat-option *ngFor="let food of foods" [value]="food.value">
      {{food.viewValue}}
      <button mat-icon-button [matMenuTriggerFor]="menu" aria-label="Example icon-button with a menu">
        <mat-icon>more_vert</mat-icon>
      </button>
      <mat-menu #menu="matMenu">
        <button mat-menu-item>
          <span>Set as default</span>
        </button>
      </mat-menu>
    </mat-option>
  </mat-select>
</mat-form-field>

However, I am facing an issue where when I open the menu, the select option list disappears and only the "Set as default" pop-up is visible.

Is there a way in Angular Material to display "Set as default" on top of the select option list or is it not possible?

Here is the link to Stackblitz for reference: https://stackblitz.com/edit/angular-material-playground-hcw1wo?file=src/app/app.component.html

Answer №1

If you want to create nested menu items, consider using mat-menu:

<button mat-raised-button [matMenuTriggerFor]="main_menu">Favorite food</button>
<mat-menu #main_menu="matMenu">
  <ng-container *ngFor="let food of foods">
    <button mat-menu-item [matMenuTriggerFor]="sub_menu">{{ food.value }}</button>
    <mat-menu #sub_menu="matMenu">
       <button mat-menu-item>Set as default</button>
    </mat-menu>
  </ng-container>
</mat-menu>

Another option to consider is utilizing mat-optgroup within mat-select. For more information, visit: https://material.angular.io/components/select/examples

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

Guide on accessing a div by using the class name of another div

If I have five divs where the first four share the same class, while the fifth has a different class. How can I target any of the first four divs using the class name present in the fifth div which is labeled as "otherclass" in this scenario? No IDs are ...

Angular Application encountering issues when attempting to load Web.Config File

I could use some assistance with deploying my Angular Application on a local IIS Server. ISS version : 10 I have successfully deployed my Angular Application on IIS and it's running smoothly. However, when I add a web.config file, it throws an error ...

Including "entryComponents" in a TestBed

One of the challenges I'm facing involves a component that receives a class of another component to dynamically create as a child. let componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentToCreate); this.componentReferenc ...

Enable automatic scrolling for CSS overflow: scroll feature

I am currently developing a chat application where messages are displayed in a background. I have used CSS to add an overflow scrollbar with the line overflow-y: scroll;. Everything is working properly, but I'm wondering if there is a way to automatic ...

Combining an AJAX POST within a JSON GET request

function performTest() { $.getJSON("/Home/GetAp", function (result) { $.each(result, function () { if (this.is_disabled == "False") { var a = $("#MainDiv") .append('<div id="imagew ...

Finding the nth-level children using JQuery

Is there a way to select nth-level children in CSS/jQuery without using any ID or class references, only tag names? I know how to get first level children with the selector div#block > div. But I am looking for a solution to target deeper levels. & ...

Having trouble sharing content from my React next.js website on social media due to issues with open graph meta tags

I'm currently facing an issue with my Next.js application when trying to share content on Facebook and Twitter. I've made sure to include the necessary open graph meta tags in the document's head section and I'm using React Helmet to dy ...

The compilation of create-react-app has encountered an error, however the process will continue running

I'm encountering a peculiar issue with my application built using create-react-app. It seems that the script refuses to exit when an error occurs. For example: ~/my-project$ ./node_modules/.bin/react-scripts build Creating an optimized production buil ...

Arranging two spans to appear in a vertical column beside a thumbnail

My goal is to create a layout with a thumbnail, two pieces of information stacked underneath, and options floating to the right. I have attempted to achieve this using the following HTML and CSS: span.author_name { font-size: 14px; vertical-align: t ...

Is there another way to retrieve a marketplace's product details using code?

When it comes to scraping products from various websites, I have discovered a clever method using window.runParams.data in the Chrome console. This allows me to easily access all the information without having to go through countless clicks to extract it f ...

Issues with sending parameters via an initialisation function in JavaScript

I am struggling with passing arguments to a new object when using an initializer function. Let's look at my examples where I aim to create an object that returns an array. Object Ex1 works as expected: Ex1 = function() { myVar = []; myVar = ...

What is the process for utilizing datePipe in an Angular component?

How can I implement DatePipe in my Angular component? This is the code snippet that I am currently using. for (let i = 0; i < this.days.length; i++) { this.storeStart(this.days[i], null, null, null); } I have stored weekdays (Monday to Frid ...

What is the best way to implement a promise function in this scenario using JavaScript?

I am currently working on an app using AngularJS and the MediaFire JavaScript SDK to perform various tasks. One specific task I am struggling with is creating a folder during an upload process, which returns a 'folderkey'. I would like to use .t ...

Guide for transferring the body of a table to a different component without disrupting the design aesthetics

In an attempt to reorganize my large table component, I decided to separate its body section into a new component. However, every time I try to do this, the styling of the table breaks (likely due to the new HTML structure in the code). I'm currently ...

Angular application's NgbTooltip positioning

Within my Angular 2/4 project, I am utilizing ng-bootstrap along with its NgbTooltip component. Consider this HTML snippet: <div class="col-sm-8 text-ellipsis" ngbTooltip="'A very long text that does not fit'" placement="top" ...

Dynamically change the padding of buttons

I'm currently customizing a left toggler button that uses the bi-list icon. When hovered over, the icon increases by 5px, but this increase also affects the size of the button, causing responsiveness issues in my navbar. How can I dynamically adjust t ...

Concealing Components using Angular's ng-change Feature

I need help displaying or hiding an element in a form using ng-change or any other method you suggest. Here is the HTML snippet I am working with: <div ng-app ng-controller="Controller"> <select ng-model="myDropDown" ng-change="changeState( ...

The functionality of `import { Dialogs } from "@nativescript/core"; seems to be malfunctioning

For my project, I am in need of using Dialogs. Unfortunately, the import from @nativescript/core as mentioned in their documentation is not working. I keep encountering this error: Module '"@nativescript/core"' has no exported member &a ...

Having trouble with the input search function displaying the wrong date on the material date picker? You may need to convert it

I have customized the MomentDateAdapter to meet my specific requirements. Interestingly, when I choose a date from the calendar, the output is accurate. However, if I enter a date manually in the input field, the output is incorrect. When dealing with the ...

What is the process for transmitting an array of objects from Angular to a Web API Core using HttpGet?

In my TypeScript file, I have an array of objects called PropertiesParams: const PropertiesParams = [{ Name: 'FirstName', Filter: 'Like1' }, { Name: 'LastName', Filter: 'Like2' }, { Name: ...