html Javascript $event.stopPropagation()不适用于mat-menu内的mat-checkbox

cld4siwp  于 2022-12-16  发布在  Java
关注(0)|答案(1)|浏览(115)

我正在努力保持一个材料菜单打开时,从一个复选框中选择它。任何建议将不胜感激。我已经尝试了解决方案,包括改变为垫选择列表,把菜单的每个元素内的div有(点击)="$event.stopPropagation();事件。预防默认值();“,并将点击事件从复选框发送到执行相同操作的助手方法。

示例.组件.ts

@Component({
  selector: 'example',
  templateUrl: 'example.component.html',
  styleUrls: ['example.compontent.scss'],
})
export class ExampleComponent implements OnInit {
  subscriptions: Subscriptions[] = [];
  userSettings: ColumnInfo[] = []; // { columnName: string, isSelected: boolean }

  constructor() { }

  ngOnInit() {
    this.setUserConfiguration(); // prefills userSettings
  }

  async setUserConfiguration(): Promise<void> {
  /* There's code here to pre-fill userSettings, as an example try: */
    this.userSettings = ['col1', 'col2', 'col3'].foreach(col => {
      return {  columnName: col, isSelected: false }
    });

  }

  filterColumn(column: string): boolean {
    return this.userSettings.findIndex(col => col.columnName === col) !== -1;
  }

  toggleColumn(column: ColumnInfo, $event: Event): void {
    $event.stopPropagation(); $event.preventDefault();
    column.isSelected = !column.isSelected;
  }

  someOtherFunction($event: Event): void {
    console.log($event);
  }

  public resetUserConfiguration(): void {
    this.getDefaultColumnConfiguration().then( // you can use setUserConfiguration for test
      res => this.userSettings = res as ColumnInfo[]
    );
  }
}
<ng-container matColumnDef="context" *ngIf="filterColumn('context')">
  <mat-header-cell *matHeaderCellDef style="color: rgb(0, 0, 0, 1);">
    <div class="ps-context-menu">
      <button mat-icon-button [matMenuTriggerFor]="tablemenu"
       test-id="open-tablemenu-btn">
        <mat-icon fontIcon="more_vert" aria-hidden="true"></mat-icon>
      </button>
      <mat-menu #tablemenu="matMenu" class="ps-ctx-no-padding filter-menu">
        <button mat-menu-item (click)=someOtherFunction($event) test-id="do-something-else">
          This button does something else
        </button>
        <button mat-menu-item test-id="column-properties-btn 
         [matMenuTriggerFor]="columnproperties">
          Column-properties
        </button>
        <mat-menu #columnproperties class="ps-ctx-no-padding filter-menu"
         test-id="column-properties-menu">
          <button mat-menu-item test-id="column-filter-btn [matMenuTriggerFor]="filtermenu">
            Column filter
          </button>
          <mat-menu #filtermenu class="ps-ctx-no-padding filter-menu"
           test-id="column-filter-menu">
           <div (click)="$event.stopPropagation();$event.preventDefault();"
            *ngFor="let column of userSettings">
             <mat-checkbox [hidden]="column.columnName==='context'"
              [checked]="column.isSelected" (click)="toggleColumn(column, $event)">
               {{column.columnName}}
             </mat-checkbox>
           </div>
          </mat-menu>
          <button mat-menu-item test-id="column-reset-btn"
           (click)="resetUserConfiguration()">
            Reset column-selection
          </button>
        </mat-menu>
      </mat-menu>
    </div>
  </mat-header-cell>
</ng-container>
ua4mk5z4

ua4mk5z41#

我最终意识到了这个问题,每次调用toggleColumn时,菜单所在的DOM都必须重新呈现,因此菜单“关闭”了。
在这个DOM之外设置菜单解决了这个问题,使用stopPropagation从来没有像我最初打算的那样出现问题。

相关问题