Click the button to collapse the dropdown menu - Angular Material

Currently, I have implemented the selectTo dropdown feature in angular material design.

Here is the code snippet that I am using:

<mat-form-field id="inputClick" appearance="outline" (click)="Valid()">
<mat-label>{{'GENERAL.TITLE' | translate}} *</mat-label>
<mat-select  [formControl]="toppings" multiple>
    <div class="drpInput">
        <mat-form-field class="mat-form-field-fluid" appearance="outline">
            <mat-label>{{'GENERAL.TITLE' | translate}} *</mat-label>
            <input autocomplete="off" matInput [placeholder]="'GENERAL.TITLE' | translate"
                (keyup)="onTextChange($event.target.value)">
        </mat-form-field>
    </div>
    <div class="oprionSelect">
            <mat-option  (click)="selectedUser(item.id)" *ngFor="let item of users" [value]="item.id">
                    <label>{{ item.displayName | translate }} </label><span class="mar">({{item.userName}})</span>
                </mat-option>
    </div>
    <mat-progress-bar *ngIf="loading" mode="indeterminate"></mat-progress-bar>
    <div class="row justofy-content-center text-center m-auto col-md-12 col-sm-12 col-lg-12 col-xl-12 col-lg12">
        <div class="col-md-4 col-sm-4 col-lg-4 col-xl-4 col-lg-4 right">
            <button mat-button color="primary" (click)="next()" *ngIf="nextBtn">Next</button>
        </div>
        <div class="col-md-4 col-sm-4 col-lg-4 col-xl-4 col-lg-4" *ngIf="count!=0">
            <button mat-button (click)="close()" color="warn">Select</button>

        </div>
        <div class="col-md-4 col-sm-4 col-lg-4 col-xl-4 col-lg-4 left">
            <button mat-button color="accent" (click)="prev()" *ngIf="prevBtn">Previous</button>
        </div>
    </div>
</mat-select>

After clicking the 'finish' button, my goal is to automatically close the dropdown menu. How can I accomplish this functionality?

Answer №1

MatSelect provides both open and close methods for your use. There are several different ways you can implement this functionality.

<mat-select #matSelect [formControl]="toppings" multiple>
   ....
   <button (click)="finish(matSelect)">Close</button>
</mat-select>

If you prefer not to pass the matSelect directly to the finish() method, you can reference it using ViewChild

@ViewChild('matSelect') matSelect;

finish() {
   this.matSelect.close();
}

You also have the option to close it directly from the HTML:

<mat-select #matSelect [formControl]="toppings" multiple>
   ....
   <button (click)="matSelect.close()">Close</button>
</mat-select>

Check out the Stackblitz Demo

To learn more, refer to the API documentation - Select | Angular Material

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

When Vue is used to hide or show elements, MDL styles may be inadvertently removed

When an element is hidden and shown using Vue, some MDL styles may disappear. Take a look at this example on CodePen: https://codepen.io/anon/pen/RBojxP HTML: <!-- MDL --> <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=M ...

Output information to the specified divID using Response.Write

Currently, I have knowledge of javascript and I am currently expanding my skills in ASP.NET C#. My goal is to achieve the following task using javascript: document.getElementById('divID-1').innerHTML= '<p>Wrong Password< ...

Tips for integrating NPM Modules with Flask

As I deploy my Python application with Flask using the render_template() function onto a webpage, I am also attempting to integrate some JavaScript modules using npm. Even though I have installed the necessary modules in the static folder along with all my ...

The type 'number | { percent: number; }' cannot be assigned to the type 'string | number | undefined' according to ts(2322) error message

Presently, I am engaged in a project utilizing React and Typescript, where I am working on implementing a progress bar using react semantic-ui. I have encountered a typescript error in my code. Here is the segment causing the issue: import React, { Compo ...

Simple code styling tool

Seeking guidance to create a basic syntax highlighter using JavaScript or ClojureScript. While aware of existing projects like Codemirror and rainbow.js, I'm interested in understanding how they are constructed and looking for a simple example. Do th ...

Top method for organizing and filtering tables in Laravel on the client side?

In my Laravel web application, I have multiple tables with MySQL data stored. I want to provide users with the ability to sort and filter data on any column header dynamically, all processed on the client side. Although Laravel does not come with this feat ...

There is no index signature that includes a parameter of type 'string' in the specified type

I have a background in mobile app development and am looking to learn more about TypeScript. How can I declare a map object with the form [string:any]? The error is occurring at line: map[key] = value; Element implicitly has an 'any' type becaus ...

How can we split the state between unique React forms that are interconnected?

My issue is that, based on the value of a prop in my form (specifically step), I need to display different forms. Everything works fine; the form loads the text programmatically as needed and fills in the form accordingly. However, when the value of step c ...

Struggling to capture a "moment in time" of a form without losing any of the data

My form is highly dynamic, with interacting top-level elements triggering a complete transformation of the lower-level elements. I needed a method to maintain state so that if users partially entered data in one category, switched temporarily to another, a ...

Some design elements are present in the interface. The functionality of the data tables is working properly, however, upon reloading the page, the table header shrinks. Interestingly

[![enter image description here][1]][1] This is the JavaScript code along with my footer and header references. The issue I am facing is with the design - initially, it shows a buggy design but when I click on the header, it displays the correct design. & ...

Sending information from one page to another and then sending it once more

I am currently utilizing the following code to submit a form's data to : <script type="text/javascript"> $(document).ready(function(){ $("#textnextofkin").validate({ debug: false, rules: { ...

How can you use a selector to filter Cheerio objects within an `each` loop?

As I work on parsing a basic webpage using Cheerio, I began to wonder about the possibilities at hand: Consider a content structure like this: <tr class="human"> <td class="event"><a>event1</a></td> <td class="nam ...

Error 400 encountered when attempting to log in with React through Google on mobile devices

Currently, I am implementing the React Google Login package to handle user authentication on my website. Surprisingly, it functions perfectly on desktops; however, when tested on mobile devices, an annoying Error 400: redirect_uri_mismatch pops up. Despi ...

Is it possible to call a ref from a different component in React?

I'm currently working on a React chat application and I want the input field where messages are entered to be focused every time you click on the chat box. However, the challenge I'm facing is that the chat box in the main component is separate ...

Webpack bundling only a singular Typescript file rather than all of its dependencies

I'm currently facing a challenge while attempting to consolidate all the files in my Typescript project, along with their dependencies from node_modules, into a single file using Webpack. Despite trying multiple options, it seems that only the entry f ...

Highstock Highcharts showcase intricate date and time data displayed on the X-axis

Attempting to utilize a JavaScript timestamp as indicated in the documentation to apply it to the X-axis has proven to be a challenging task. Despite my efforts, I have been unable to successfully implement the date data for use with a datepicker. I have ...

identify when the React component has reached the bottom of the element by reading the

Is there a way to access the scroll handler's event without being able to access offsetTop, scrollTop, etc? class App extends React.Component { handleScroll = e => { console.log(e.target.scrollTop); }; componentDidMo ...

What steps can I take to resolve the issue in my code? I keep receiving a type error stating that it cannot read

I seem to be encountering an issue when running my code where I receive a 'cannot read property 'age' of null'. This error breaks my code, and I'm trying to figure out how to implement a check to ensure it only runs when I am signe ...

In Typescript, you can easily group a string into sections that consist of digits like 345-67, along with text containing a

I have a string that looks like this: "[111-11] text here with digits 111, [222-22-22]; 333-33 text here" and I am trying to parse it so that I can extract the code [111-11], [222-22-22], [333-33] along with their respective text descriptions. The challeng ...

A Guide to Setting the HTML Lang Attribute on a Specific Page in Next.js

I have a website built with Next.js that consists of several pages. Most of the pages are in English, but there is one page that is in French and does not have an English version. How can I assign the lang attribute to the HTML tag for this specific page ...