【问题标题】:Angular 11 - mat-sort is not working in ng-modal tableAngular 11 - mat-sort 在 ng-modal 表中不起作用
【发布时间】:2021-09-26 09:42:55
【问题描述】:

mat-sort 和 paginator 在 ng-modal 表中不起作用。但在模态之外它工作正常。这个问题有解决办法吗?

【问题讨论】:

    标签: angular sorting angular-material ng-modal


    【解决方案1】:

    问题在于,当您尝试分配 mat-sort 时,mat-sort 无法访问 - 因为它实际上没有显示 -。您需要指定 mat-sort after 显示。如果您使用 mat-dialog,则在打开时使用 setTimeout()。你可以认为 setTimeout 就像你对 Angular 说的:“渲染后,别忘了执行这个指令”

    const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
      width: '250px',
      data: {name: this.name, animal: this.animal}
    });
    setTimeout(()=>{ //<--here you say to Angular the sort
       this.dataSource.sort = this.sort;
    })
    

    如果您使用的是 ng-bootstrap 模态

    open(content) {
        this.modalService.open(content, {ariaLabelledBy: 'modal-basic-title'}).result.then((result) => {
          this.closeResult = `Closed with: ${result}`;
        }, (reason) => {
          this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
        });
        setTimeout(()=>{ //<--here you say to Angular the sort
           this.dataSource.sort = this.sort;
        })
      }
    

    注意:请记住,由于 mat-table 始终不可见,所以 mat-sort ViewChild 应该是这样的

    @ViewChild(MatSort) sort: MatSort;
    

    不使用{static true} -static 仅当元素始终显示在组件中时才使用它-

    更新确实提出的解决方案不起作用。在模板中,“viewChild 是不可访问的。所以我唯一得到的是创建一个傻瓜指令

    @Directive({
      selector: '[fool]',
      exportAs: 'fool'
    })
    export class FoolDirective implements OnInit {
      ngOnInit()
      {
        setTimeout(()=>{
          (this.table.dataSource as any).sort=this.sort
        })
      }
      constructor(@Optional() private sort:MatSort,@Optional() private table:MatTable<any>){}
    }
    

    所以,我们只需要将这个指令添加到我们的表中

    <table mat-table [dataSource]="dataSource"
      matSort fool
      class="mat-elevation-z8">
     ....
    </table>
    

    查看 [stackblitz][1]

    Update2 要添加分页器,事情就更复杂了。我的想法是改进我们的指令傻瓜,添加一个 MatPaginator 作为 Optional 并作为输出发送

    export class FoolDirective implements OnInit {
      @Output() paginatorLoaded:EventEmitter<MatPaginator>=new EventEmitter<MatPaginator>()
      ngOnInit()
      {
        setTimeout(()=>{
          if (this.table && this.sort)
            (this.table.dataSource as any).sort=this.sort
          if (this.paginator)
            this.paginatorLoaded.emit(this.paginator)
        })
      }
      constructor(@Optional() private sort:MatSort,@Optional() private table:MatTable<any>,@Optional() private paginator:MatPaginator){}
    }
    

    那么,唯一的就是,在添加Paginator的时候写

    <mat-paginator  fool 
             (paginatorLoaded)="dataSource.paginator=$event"
    ....>
    </mat-paginator>
    

    唯一的改变是在styles.css中添加.css

    .cdk-overlay-container {
      z-index: 9000;
    }
    

    否则选择的页数不显示 - 它显示,但在弹出窗口下

    注意:我真的不太喜欢这些变通方法,也许是时候重新考虑并使用另一个弹出窗口了 - 例如材料或将 ngb-bootstrap 模态与组件或模板一起使用。 [1]:https://stackblitz.com/edit/angular-hbadro?file=src%2Fapp%2Ffool.directive.ts

    【讨论】:

    • 我试过了,还是不行。这是 stackblitz 链接:stackblitz.com/edit/…
    • 我的错!!抱歉,我更新了答案(这是我唯一能得到的解决方案)
    • 非常感谢您提供更新的答案。但我有最后一个问题。我如何使用分页器?据我了解,它遵循与排序相同的原则,但在这种情况下它不起作用。这是我的堆栈闪电战stackblitz.com/edit/…
    • 思考问题后,我再次更新答案
    猜你喜欢
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 2020-06-19
    • 2021-04-21
    相关资源
    最近更新 更多