首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >垫子表不工作的分页器

垫子表不工作的分页器
EN

Stack Overflow用户
提问于 2019-07-07 04:52:16
回答 1查看 162关注 0票数 0

我是angular和typescript的新手,我正在尝试建立一个带有分页器的表格。分页器数据集来自REST api。到目前为止,API工作正常,我成功地获得了数据。

不幸的是,分页器目前不起作用。

导入是可以的。在从REST服务获得数据后,我正在分配数据,但仍然收到以下错误消息:

代码语言:javascript
运行
复制
ERROR TypeError: Cannot set property 'paginator' of undefined

我做错了什么?

代码语言:javascript
运行
复制
 availableLanguages: ILanguage[];
  displayedColumns = ['language'];
  dataSource: MatTableDataSource<ILanguage>;

  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

  constructor(public service: DialectService) {}

  ngOnInit(): void {
    this.service.getLanguages()
      .subscribe((resp: ILanguage[]) => {
        this.availableLanguages = resp;
        this.dataSource = new MatTableDataSource(resp);
      });
  }


  /**
   * Set the paginator and sort after the view init since this component will
   * be able to query its view for the initialized paginator and sort.
   */
  ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }



  applyFilter(filterValue: string) {
    filterValue = filterValue.trim(); // Remove whitespace
    filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
    this.dataSource.filter = filterValue;
    console.log(this.dataSource.filter, filterValue);
  }
}
代码语言:javascript
运行
复制
<!--   -->
<div class="example-header">
  <mat-form-field>
    <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
  </mat-form-field>
</div>

<div class="example-container mat-elevation-z8">

  <mat-table [dataSource]="dataSource" matSort>

    <ng-container matColumnDef="language">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Language </mat-header-cell>
      <mat-cell *matCellDef="let row"> {{row}} </mat-cell>
    </ng-container>


    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;">
    </mat-row>
  </mat-table>

  <mat-paginator [pageSizeOptions]="[1, 2, 25, 100]"></mat-paginator>
</div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-07 05:28:41

尝试将所有数据集初始化代码移动到ngAfterViewInit中。如下所示。

代码语言:javascript
运行
复制
availableLanguages: ILanguage[];
displayedColumns = ['language'];
dataSource: MatTableDataSource<ILanguage>;

@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;

constructor(public service: DialectService) {}

ngOnInit(): void { }

ngAfterViewInit() {
    this.service.getLanguages()
        .subscribe((resp: ILanguage[]) => {
            this.availableLanguages = resp;
            this.dataSource = new MatTableDataSource(resp);
            this.dataSource.paginator = this.paginator;
            this.dataSource.sort = this.sort;
        });
}

因为对this.service.getLanguages()的调用是异步的,所以在触发ngAfterViewInit生命周期钩子时它还没有完成。因此,当ngAfterViewInit尝试将paginator分配给this.dataSource时,它仍然是未定义的。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56917587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档