【问题标题】:Angular material table displaying empty rows显示空行的角材料表
【发布时间】:2019-05-06 18:02:06
【问题描述】:

我正在尝试将 Angular 的材料表集成到一个新项目中,但由于某种原因,数据没有显示,只有空行。我在另一个项目中使用了相同的逻辑并且它在那里工作,我花了很多时间寻找原因但没有任何工作。

我需要将表格添加到名为“cores”的延迟加载模块中,并导入材料表模块:

import { MatTableModule } from '@angular/material';
imports: [
    CommonModule,
    CoresRoutingModule,
    MatTableModule
  ]

我创建了一个返回“ICore”对象数组的服务:

getCores(): Observable<ICore[]> {
    return this.http.get<ICore[]>('/cores');
  }

ICore界面:

export interface ICore {
  id: number;
  name: string;
}

我的“index.component.html”文件中的 HTML:

<mat-table [dataSource]="dataSource">
  <ng-container matColumnDef="id">
    <mat-header-cell *matHeaderCellDef> Id </mat-header-cell>
    <mat-cell *matCellDef="let core"> {{ core.id }}</mat-cell>
  </ng-container>
  <ng-container matColumnDef="name">
    <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
    <mat-cell *matCellDef="let core"> {{ core.name }}</mat-cell>
  </ng-container>
  <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

“index.components.ts”文件中的逻辑:

dataSource = new MatTableDataSource<ICore>();
columnsToDisplay = ['id', 'name'];

constructor(private coreService: CoreService) { }

  ngOnInit() {
    this.coreService.getCores().subscribe(
      res => {
        this.dataSource.data = res;
        console.log(this.dataSource);
      },
      err => console.log(err)
    );
  }

从我的 console.log 我看到数据源已填充:

MatTableDataSource {_renderData: BehaviorSubject, _filter: BehaviorSubject, _internalPageChanges: Subject, _renderChangesSubscription: Subscriber, sortingDataAccessor: ƒ, …}
data: Array(3)
0: {id: 1, name: "core1"}
1: {id: 2, name: "core2"}
2: {id: 3, name: "core3"}

我也试过这个方法:

dataSource = new CoreDataSource(this.coreService);
columnsToDisplay = ['id', 'name'];

constructor(private coreService: CoreService) { }

export class CoreDataSource extends DataSource<any> {   
    constructor(private coreService: CoreService) {
        super();
    }

  connect(): Observable<ICore[]> {
    console.log(this.coreService.getCores());
    return this.coreService.getCores();   }

  disconnect() {} }

但结果相同。我觉得这是我忘记做但我找不到的愚蠢的事情。

提前致谢!

【问题讨论】:

  • 在您的模板中,您使用displayedColumns 作为列,但在您的组件中,该变量称为columnsToDisplay。这是您问题中的错字还是代码中的实际错误?
  • 天啊。我很尴尬,我检查了所有内容,但没有检查这个变量名。它解决了这个问题。谢谢你!。您可以添加答案,我会将其标记为已解决。
  • 发生在我们最好的人身上,有时您只需要另一双眼睛来查看您的代码。
  • @FabianKüng,你是对的。再次感谢。

标签: angular angular-material angular7


【解决方案1】:

确保使用相同的变量来显示您在组件中定义的列。

组件:

columnsToDisplay = ['id', 'name'];

模板:

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

【讨论】:

  • 谢谢,我使用的是 angular 5。文档说 。它什么也没显示
【解决方案2】:

由于您的数据源位于子变量数据中,因此不会以这种方式呈现。试试下面的代码

<mat-table [dataSource]="dataSource.data">
  <ng-container matColumnDef="id">
    <mat-header-cell *matHeaderCellDef> Id </mat-header-cell>
    <mat-cell *matCellDef="let core"> {{ core.id }}</mat-cell>
  </ng-container>
  <ng-container matColumnDef="name">
    <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
    <mat-cell *matCellDef="let core"> {{ core.name }}</mat-cell>
  </ng-container>
  <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
  <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>

【讨论】:

  • 相同的结果......这将是我尝试的第二种解决方案的问题...... :(
  • 你试过了吗 this.dataSource = res;
  • 是,但在这种情况下会显示错误:类型“ICore[]”缺少类型“MatTableDataSource”中的以下属性
猜你喜欢
  • 1970-01-01
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多