Angular MatTable排序因ngOnChange而中断

时间:2018-10-18 15:25:07

标签: angular angular-material

我有一张表,该表加载有初始数据,并且通过用户交互或自动从datasource动态更新来更新。如果我的@Inputs之一发生更改,我正在使用ngAfterViewInit来加载表并使用ngOnChanges来更新表(matTable是组件的组成部分,用于基于@Input显示表“设置”)。

当我应用ngOnChanges时,列“跳”起来好像是在对其自身进行排序。我什至看到默认排序列上的箭头会自动切换。没有它,就没有问题,但是表不会更新。

带有表格的组件:

ngAfterViewInit() {

    ... some special grouping of header is the user requested it ...

    ... extrapolate the columns from the @Input of the data passed ...

    this.setSorting();
    this.setPaging();

    ... set some vars such as if the table should sort and inject things like checkboxes ...

    this.cdr.detectChanges();
  }

  ngOnChanges() {
    this.setSorting();
    this.setPaging();
  }

  setSorting() {
    this.dataSource.sort = this.sort;

    ... additional sorting "rules" such as user defined sortingDataAccessor ...
  }

  setPaging() {
    this.dataSource.paginator = this.paginator;
  }

0 个答案:

没有答案