简体   繁体   English

在输入字段中输入 3 个字符后如何显示 mat-menu?

[英]How to display mat-menu after entering 3 characters in input field?

I'm implement this input field so that when I enter 3 characters I want to display mat-menu as a value of mat-option but for some reason the mat-menu never gets display.我正在实现这个输入字段,这样当我输入 3 个字符时,我想将 mat-menu 显示为 mat-option 的值,但由于某种原因,mat-menu 永远不会显示。 Can anyone point me in the right direction?谁能指出我正确的方向? thanks a lot in advance!提前非常感谢!

Here's LIVE DEMO这是现场演示

<mat-autocomplete #auto="matAutocomplete">
    <mat-option *ngIf="isSpinnerLoading">

  <div *ngIf="!menuContent">Loading...</div>
  <div *ngIf="menuContent">
    show menu

    <mat-menu #menu="matMenu" class="menu-select">
            <div (click)="$event.stopPropagation()">
                <div *ngFor="let item of myList">
                    <mat-checkbox
                        class="mat-menu-item">
                        {{item}}
                    </mat-checkbox>
                </div>
            </div>
        </mat-menu>
  </div>
    </mat-option>
</mat-autocomplete>

This is what I'm trying to accomplish:这就是我想要完成的事情:

在此处输入图像描述

Problem问题

The problem that you are facing is that the mat-menu openMenu() function is not triggered when your content opens您面临的问题是打开内容时未触发mat-menu openMenu() function

Solution解决方案

We will need a way to trigger the menu to open Below is an approach that I have used我们需要一种方法来触发菜单打开下面是我使用的一种方法

Add the below markup in your html在您的 html 中添加以下标记

<span #menuTrigger="matMenuTrigger"  [matMenuTriggerFor]="menu">show menu</span>

In your TS file add below @ViewChild property to hold the matMenuTrigger在您的 TS 文件中添加下面的@ViewChild属性以保存matMenuTrigger

  @ViewChild("menuTrigger", { static: false }) set menuTrigger(
    content: MatMenuTrigger
  ) {
    if (content) {
      this.contentPlaceholder = content;
    }
  }

Finally amend your onKeyUp() function最后修改你的onKeyUp() function

  onKeyUp() {
    if (this.currentText.length >= 3) {
      console.log("testing");
      this.isSpinnerLoading = true;
      this.menuContent = false;
      setTimeout(() => {
        this.menuContent = true;
      }, 2000);
      setTimeout(() => {
        this.contentPlaceholder.openMenu();
      }, 2001);
    } else {
      this.isSpinnerLoading = false;
    }
  }

Note that we are triggering openMenu() after the this.menuContent() becomes true请注意,我们在this.menuContent()变为 true 后触发openMenu()

See Demo 看演示

  1. There is no need to use mat-menu, you can use mat-checkbox as an option.无需使用 mat-menu,您可以使用 mat-checkbox 作为选项。
  2. The ngFor must be used on the mat-option element otherwise, with the current implementation, you will end up with a single option. ngFor 必须在 mat-option 元素上使用,否则,在当前实现中,您最终将得到一个选项。
  3. The loading indicator (Loading...) can be displayed as a disabled mat-option.加载指示器 (Loading...) 可以显示为禁用的垫选项。
  4. In order to enable multiple selections the item should be an object that has a boolean that indicates whether the item is checked or not.为了启用多项选择,该项目应该是一个 object,它有一个 boolean 指示该项目是否被选中。

Here is a link of stackblitz.这是stackblitz的链接 It's not perfect but it can help.它并不完美,但它可以提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM