Sagar Yadav
Sagar Yadav

Reputation: 147

How to attach matMenuTriggerFor dynamically to DOM element in Angular material menu?

While adding Angular material menu in Angular table I am getting performance issue. Angular material table with thousands of rows create thousands of mat menu components. Due to this page loading takes too long.

This is my sample code.

    <table>
      <tr *ngFor="let el of [1,2,3,4,5]">
        <td>Text line of menu {{el}}</td>
        <td style="width: 5%">
        <button mat-icon-button [matMenuTriggerFor]="abc.menu" class="center-block">
            <i class="material-icons">more_vert</i>
        </button>

        <app-desposition #abc="menuComponent" [user]="row.policyObj"></app-desposition>
        </td>
  </tr>
</table>

The <app-desposition> is other Angular component with mat-menu template.

This is template of <app-desposition>

<mat-menu #menu="matMenu" class="d-menu-container">

  <div class="desposition-menu">

    <div (click)="$event.stopPropagation()">
          <mat-input-container class="col-md-12">
              <textarea [(ngModel)]="message" matInput id="message" name="body" rows="2" placeholder="Click here to write comment"></textarea>
          </mat-input-container>
      </div>

    <mat-radio-group [(ngModel)]="selectedDesposition" (click)="$event.stopPropagation()">   

      <div mat-menu-item *ngFor="let menu of menuItems">
        <mat-radio-button class="radioText" [value]="menu.getName()">
          <span class="wrap-mat-radio-label" style="font-style:unset; font-size: 12px">{{menu.getName()}}</span>
        </mat-radio-button> 
      </div>
    </mat-radio-group>
  </div>

  <div mat-menu-item (click)="$event.stopPropagation()">
      <span *ngIf="uiStatus.getSuccess() else uiElse" style="text-color:green">{{uiStatus.getMessage()}}</span>

      <ng-template #uiElse><span style="text-color:red">{{uiStatus.getMessage()}}</span></ng-template>
  </div>

  <div mat-menu-item  (click)="$event.stopPropagation()">
    <button  mat-raised-button color="warn" style="width:100%"  (click)="onSelect()">
      SAVE
    </button>  
  </div>

</mat-menu>

I want deposition component to create dynamically(I can do this), But, I don't know how to attach [matMenuTriggerFor] to menu which inside of dynamically created component.

Please someone help?

For reference @jpavel did something similar to me on stackbliz. Here is link. Angular Material 2 MatMenu - Dynamically creation

Upvotes: 1

Views: 5269

Answers (1)

arslan
arslan

Reputation: 1144

You can set it with ViewChild Check this example https://stackblitz.com/edit/dynamic-nested-menus?file=app%2Fmenu-item%2Fmenu-item.component.html

Upvotes: 1

Related Questions