【问题标题】:How to display mat-select options on top of dialog window?如何在对话框窗口顶部显示垫选择选项?
【发布时间】:2019-06-03 17:47:15
【问题描述】:

我发现在全局css文件中设置的解决方案:

.cdk-global-overlay-wrapper, .cdk-overlay-container {
  z-index: 10000;
}

破坏其他对话框窗口(例如,在 MatDialog 中的表单内部使用的 MatDatepicker 显示在它后面)。 在本地设置似乎没有效果,根据这篇帖子更改视图封装: Displaying a mat-select inside a modal dialog 仍然会破坏其他对话框窗口(与全局设置样式相同)。我可以通过其他方式实现它吗?

编辑:

示例 stackblitz,其中选择选项甚至不显示: https://stackblitz.com/edit/angular-jkxsig-en8bze?file=app/

【问题讨论】:

  • 您能否提供一些有关如何打开对话框的代码,我在 stackblitz 上创建了一个带有下拉菜单的简单对话框,它工作正常。
  • @than,您能否在对话框(最好在 ng-bootstrap)窗口中为您的 stackblitz 提供工作中的 MatSelect?

标签: angular angular-material angular-material-7


【解决方案1】:

好的,看来我找到了问题的原因和解决方案。 所以问题的原因是引导模式窗口(ngb-modal-window)出现在背景(ngb-modal-backdrop)下,如下所述:

Bootstrap modal appearing under background

设置模态窗口 z-index 没有任何效果,因为背景在堆叠上下文中总是更高,并且 bootstrap 框架总是在组件创建后将其 z-index 覆盖为 1050。 这个问题有两种解决方案,我在这里找到了:

Bootstrap modal appearing under background

1.) 使用 NgbModalOptions 禁用背景(将 backdrop 属性设置为 false)并为模式窗口添加类(设置 windowClass 属性)。然后为我们的窗口类设置非常低的z-index 值,就像这样,在全局style.css 文件中:

ngb-modal-window.test {
    z-index: 1;
}

缺点是,我们的引导模式现在没有背景。

2.) 在 html 层次结构中的 body 容器下直接添加模态窗口,或者只是将其附加到它,如下所述:

https://github.com/twbs/bootstrap/issues/23916

我还没有测试过,但它应该可以工作,在这里你可以找到如何将元素附加到 html 正文的信息:

https://hackernoon.com/angular-pro-tip-how-to-dynamically-create-components-in-body-ba200cc289e6

【讨论】:

    【解决方案2】:

    当我初始化 id 时它起作用了。

    import {Component} from '@angular/core';
    
    import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';
    
    export interface Food {
      value: string;
      viewValue: string;
    }
    
    @Component({
      selector: 'ngbd-modal-basic',
      templateUrl: './modal-basic.html'
    })
    export class NgbdModalBasic {
        foods: Food[] = [
        {value: 'steak-0', viewValue: 'Steak'},
        {value: 'pizza-1', viewValue: 'Pizza'},
        {value: 'tacos-2', viewValue: 'Tacos'}
      ];
      closeResult: string;
      ariaId = 'modal-basic-title';
    
      constructor(private modalService: NgbModal) {}
    
      open(content) {
        this.modalService.open(content, { ariaLabelledBy: 'modal-basic-title', size: 'lg', backdrop: 'static' }, ).result.then((result) => {
          this.closeResult = `Closed with: ${result}`;
        }, (reason) => {
          this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
        });
      }
    
      private getDismissReason(reason: any): string {
        if (reason === ModalDismissReasons.ESC) {
          return 'by pressing ESC';
        } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
          return 'by clicking on a backdrop';
        } else {
          return  `with: ${reason}`;
        }
      }
    }
    
    

    html

    <ng-template #content let-modal let-id="ariaId">
      <div class="modal-header">
        <h4 class="modal-title" id="{{ariaId}}">Profile update</h4>
        <button type="button" class="close" aria-label="Close" (click)="modal.dismiss('Cross click')">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form>
          <div class="form-group">
            <label for="dateOfBirth">Date of birth</label>
            <div class="input-group">
              <input id="dateOfBirth" class="form-control" placeholder="yyyy-mm-dd" name="dp" ngbDatepicker #dp="ngbDatepicker">
              <div class="input-group-append">
                <button class="btn btn-outline-secondary calendar" (click)="dp.toggle()" type="button"></button>
              </div>
            </div>
          </div>
          <h4>Basic mat-select</h4>
          <mat-form-field>
            <mat-label>Favorite food</mat-label>
            <mat-select>
              <mat-option *ngFor="let food of foods" [value]="food.value">
                {{food.viewValue}}
              </mat-option>
            </mat-select>
          </mat-form-field>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-outline-dark" (click)="modal.close('Save click')">Save</button>
      </div>
    </ng-template>
    
    <button class="btn btn-lg btn-outline-primary" (click)="open(content)">Launch demo modal</button>
    
    <hr>
    
    <pre>{{closeResult}}</pre>
    

    【讨论】:

    • 感谢您的建议,我检查了,但不幸的是它对我的情况没有帮助。我使用选项:'ariaLabelledBy:'modal-basic-title',大小:'lg''。
    • 我刚刚用modal-basic-title id 更新了答案
    • 我看不到模态/垫子选择行为的变化。也许创建我的 stackblitz 链接的分支并在此处发布?
    猜你喜欢
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    相关资源
    最近更新 更多