简体   繁体   English

当 mat-table 在 NgIf 内部时 mat-paginator 中断

[英]mat-paginator breaks when mat-table is inside of NgIf

I have an angular project that is using mat-table and mat-paginator for a certain view, the problem is the view has a grid view and table view with a toggle, the grid view is default and table is hidden using an NgIf when the grid view is active.我有一个 angular 项目,它对某个视图使用mat-tablemat-paginator ,问题是该视图有一个网格视图和一个带有切换的表视图,网格视图是默认的,并且当网格视图处于活动状态。 If I set the default to the table view then pagination works fine unless I swap to grid view and back, if the default if set to grid it breaks when I swap to the table view.如果我将默认设置为表格视图,那么分页工作正常,除非我切换到网格视图并返回,如果默认设置为网格,当我切换到表格视图时它会中断。 I'm guessing its because the table is hidden when this code runs:我猜是因为当这段代码运行时表是隐藏的:

this.sliceList = new MatTableDataSource<Slice>(result);
this.sliceList.paginator = this.paginator;

I tried console logging this.sliceList and sliceList.paginator is undefined when the grid view is defaulted so I assuming this is the issue.当默认网格视图时,我尝试了控制台日志记录this.sliceListsliceList.paginator是未定义的,所以我假设这是问题所在。 How Can I fix this?我怎样才能解决这个问题?

according to this thread ,try use [hidden] instead of *ngIf.根据此线程,尝试使用 [hidden] 而不是 *ngIf。

<div [hidden]="condition">
  <mat-table [dataSource]="dataSource">
  ...
  </mat-table>
</div>

1st Solution第一个解决方案

Move mat-paginator from inside *ngIf div to outside将 mat-paginator 从 *ngIf div 内部移动到外部

2nd Solution第二种解决方案

use static false while declaring MatPaginator or MatSort在声明 MatPaginator 或 MatSort 时使用静态 false

@ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
@ViewChild(MatSort, {static: false}) sort: MatSort;

This solve my problem in ANgular 10这解决了我在 Angular 10 中的问题

Setting static to False does the trick.将 static 设置为 False 可以解决问题。

@ViewChild(MatPaginator, {static: false}) paginator: MatPaginator;
  @ViewChild(MatSort, {static: false}) sort: MatSort;

    ngAfterViewInit() {
        setTimeout(() => {
          this.matTableConfig.dataSource = new MatTableDataSource(this.matTableConfig.dataSource);
          this.matTableConfig.dataSource.paginator = this.paginator;
          this.matTableConfig.dataSource.sort = this.sort;
      });

Create in file.html the <div> with these IDs:在 file.html 中创建具有以下 ID 的<div>

div id="dataFound" style="display:none;"
div id="dataNotFound" style="display:none;"

In file.ts在文件.ts

document.getElementById("dataFound").style.display = 'none';    /* ngIf = false     
document.getElementById("dataNotFound").style.display = 'inline'; /* ngIf = true

For me I was using like this对我来说,我是这样使用的
Code Before:之前的代码:

 <div *ngIf="tableData?.data.length > 0; else noData">
      <mat-table [dataSource]="tableData" class="table-margin-0">
            ........
      </mat-table>
      <mat-paginator
            [pageSizeOptions]="[6, 10, 25, 50, 100]"
            showFirstLastButtons
      ></mat-paginator>
 </div>

Now using like this,现在像这样使用,
by adding [hidden] in paginator outside the *ngIf that works for me通过在对我*ngIf*ngIf之外的分页器中添加[hidden]

Here is the code that worked这是有效的代码
Code after:之后的代码:

<div *ngIf="tableData?.data.length > 0; else noData">
      <mat-table [dataSource]="tableData" class="table-margin-0">
            ........
      </mat-table>
 </div>
<mat-paginator
      [hidden]="tableData?.data.length === 0"
      [pageSizeOptions]="[6, 10, 25, 50, 100]"
      showFirstLastButtons
 ></mat-paginator>

loaddata hinders the funtionality of the mat paginator due to which ng if does not works in this condition. loaddata 阻碍了 mat 分页器的功能,因为 ng if 在这种情况下不起作用。 instead of using *ngIf use [hidden] = "condition on which u want to hide"而不是使用 *ngIf 使用 [hidden] = "您要隐藏的条件"

I had the same problem and after some research I was able to make it work using the @ViewChild annotation and a set function.我遇到了同样的问题,经过一些研究,我能够使用@ViewChild 注释和一组 function 使其工作。

In my component.ts file:在我的 component.ts 文件中:

  constructor(private cdRef: ChangeDetectorRef) { }

  private paginator: MatPaginator;
  /*
   We are using @ViewChild because of the ngIf with async pipe on the template
   When the data is emmited for the first time from the observable, we need to bind the
   pagination component with the datasource. This can be achieved with the code bellow.
  */
  @ViewChild(MatPaginator) set matPaginator(mp: MatPaginator) {
    if(mp) {
      this.paginator = mp;
      this.dataSource = new MatTableDataSource<any>(your_data);
      this.dataSource.paginator = this.paginator;
      this.cdRef.detectChanges();
     }
   }

and in my template file:在我的模板文件中:

  // my observable with the async pipe
  <div *ngIf="raceList$ | async as races">
    <div class="mat-table-width">
      <table mat-table [dataSource]="dataSource">
      ...
      </table>
      <mat-paginator [pageSizeOptions]="[10, 20, 30]"
                      showFirstLastButtons
                      aria-label="select page of races">
      </mat-paginator>
    </div>
  </div>

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

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