Angular Material将MatSort添加到自定义表

时间:2019-08-30 10:51:43

标签: angular angular-material2 mat-table

因此,我的应用程序中有一个组件,它包装了MatTable组件,并为我的所有表添加了一些通用功能(例如加载指示器等)

我通过以下方式使用此表

<ngx-progress-table ...>

  <ng-content matColumnDef="someColumn">
    ...
  </ng-content>

</ngx-progress-table>

NgxProgressTableComponent中的实现是

<mat-table ...>
  ...

  <mat-header-row />
  <mat-row />
</mat-table>

然后在组件中添加以下列:

ngAfterContentInit() {
  this.columnDefs.forEach(columnDef => this.table.addColumnDef(columnDef));
}

这很好用,但是我现在需要使用MatSort向表中添加排序功能。我已经在组件中的表声明中添加了matSort,但是在列def中添加了mat-sort-header时,出现了以下错误MatSortHeader must be placed within a parent element with the MatSort directive

我得到了错误的含义,因为我的列def位于未实现MatSort的父组件中,但是我找不到任何实现此目的的方法。

这是完全可能的,还是拥有一个完全扩展MatTable组件的组件会更好?

0 个答案:

没有答案