首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular: ng- mat-table中的内容

Angular: ng- mat-table中的内容
EN

Stack Overflow用户
提问于 2019-11-28 20:27:38
回答 1查看 1.2K关注 0票数 1

我尝试为具有多个排序的角度材料表创建一个包装器。因此,我喜欢为默认的table编写一个包装器组件,这是我的模板:

代码语言:javascript
复制
<div>
    <table mat-table [dataSource]="tableData.dataSource" matMultiSort (matSortChange)="_emitParentEvent">
        <ng-content></ng-content>
        <tr mat-header-row *matHeaderRowDef="tableData.displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: tableData.displayedColumns;"></tr>
    </table>
    <mat-paginator [pageSize]="tableData.pageSize" [pageIndex]="tableData.pageIndex"
        [pageSizeOptions]="tableData.pageSizeOptions" [length]="tableData.size ? tableData.size : 0"
        (page)="tableData.onPagnationEvent($event)">
    </mat-paginator>

</div>

下面是组件的代码:

代码语言:javascript
复制
@Component({
  selector: 'mat-multi-sort-table',
  templateUrl: './mat-multi-sort-table.component.html',
  styleUrls: ['./mat-multi-sort-table.component.scss']
})
export class MatMultiSortTableComponent implements OnInit {

  @Input() tableData: TableData<any>;
  @Output() matMulitSortChange: EventEmitter<MutliSortItem[]> = new EventEmitter<MutliSortItem[]>();

  @ViewChild(MatMultiSort, { static: false }) sort: MatMultiSort;

  constructor() { }

  ngOnInit() {
    this.tableData.dataSource.sort = this.sort;
  }

  _emitParentEvent() {
    const tmpItmes = [];
    for (let i = 0;*emphasized text* i < this.sort.actives.length; i++) {
      tmpItmes.push(new MutliSortItem(this.sort.actives[i], this.sort.directions[i]));
    }
    this.matMulitSortChange.emit(Object.assign([], tmpItmes));
  }
}

当我将一些内容传递给我的组件时,不会,如下所示;

代码语言:javascript
复制
<mat-multi-sort-table [tableData]="table" (matMulitSortChange)="console.log($event)">
    <ng-container matColumnDef="id">
      <th mat-header-cell *matHeaderCellDef mat-multi-sort-header="id"> ID </th>
      <td mat-cell *matCellDef="let row"> {{row.id}} </td>
    </ng-container>

    <ng-container matColumnDef="progress">
      <th mat-header-cell *matHeaderCellDef mat-multi-sort-header="progress"> Progress </th>
      <td mat-cell *matCellDef="let row"> {{row.progress}}% </td>
    </ng-container>

    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef mat-multi-sort-header="name"> Name </th>
      <td mat-cell *matCellDef="let row"> {{row.name}} </td>
    </ng-container>

    <ng-container matColumnDef="color">
      <th mat-header-cell *matHeaderCellDef mat-multi-sort-header="color"> Color </th>
      <td mat-cell *matCellDef="let row" [style.color]="row.color"> {{row.color}} </td>



    </ng-container>

    <ng-container matColumnDef="date">
      <th mat-header-cell *matHeaderCellDef mat-multi-sort-header="date"> Date </th>
      <td mat-cell *matCellDef="let row"> {{row.date | date:'YYYY-MM-DD HH:mm'}} </td>
    </ng-container>

</mat-multi-sort-table>

我得到以下错误:ERROR Error: "Could not find column with id "id"我发现,出现这个问题是因为表在初始化时需要它的定义,而ng-content并非如此。是否有解决此问题的方法?

EN

回答 1

Stack Overflow用户

发布于 2020-04-20 04:08:54

您有一个关于角度材质github的示例:https://github.com/angular/components/tree/master/src/components-examples/material/table/table-wrapped

你必须这样做:

代码语言:javascript
复制
export class WrapperTable<T> implements AfterContentInit {
  @ContentChildren(MatHeaderRowDef) headerRowDefs: QueryList<MatHeaderRowDef>;
  @ContentChildren(MatRowDef) rowDefs: QueryList<MatRowDef<T>>;
  @ContentChildren(MatColumnDef) columnDefs: QueryList<MatColumnDef>;

  @ViewChild(MatTable, {static: true}) table: MatTable<T>;

  ngAfterContentInit() {
    this.columnDefs.forEach(columnDef => this.table.addColumnDef(columnDef));
    this.rowDefs.forEach(rowDef => this.table.addRowDef(rowDef));
    this.headerRowDefs.forEach(headerRowDef => this.table.addHeaderRowDef(headerRowDef));
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59088912

复制
相关文章

相似问题

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