【问题标题】:Getting the error: Could not find column with id "id"收到错误:找不到 ID 为“id”的列
【发布时间】:2018-12-13 09:00:04
【问题描述】:

我正在尝试用数据填充 mat-table,但是当我尝试这样做时出现错误:Could not find column with id "id"

这就是我在组件文件中的做法:

    export class ListAllTrucksComponent {
      displayedColumns: string[] = ['id', 'plate', 'status', 'type'];
      orders: Order[] = [];
      dataSource: MatTableDataSource<Order>;
    
      constructor(private orderService: OrderService) {
        orderService.getAll().subscribe((orders) => {
          for (const order of orders) {
            const newOrder = new Order(order[0], order[1], order[2], order[3]);
            this.orders.push(newOrder);
          }
          console.log(this.orders);
          this.dataSource = new MatTableDataSource<Order>(this.orders);
        });
      }
    
      applyFilter(filterValue: string) {
        this.dataSource.filter = filterValue.trim().toLowerCase();
      }
    }

这是我基本上从示例中复制的 html:

    <main>
      <div id="content">
        <mat-form-field>
          <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
        </mat-form-field>
    
        <table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
    
          <!-- Position Column -->
          <ng-container matColumnDef="position">
            <th mat-header-cell *matHeaderCellDef> No. </th>
            <td mat-cell *matCellDef="let element"> {{element.id}} </td>
          </ng-container>
    
          <!-- Name Column -->
          <ng-container matColumnDef="name">
            <th mat-header-cell *matHeaderCellDef> Name </th>
            <td mat-cell *matCellDef="let element"> {{element.plate}} </td>
          </ng-container>
    
          <!-- Weight Column -->
          <ng-container matColumnDef="weight">
            <th mat-header-cell *matHeaderCellDef> Weight </th>
            <td mat-cell *matCellDef="let element"> {{element.status}} </td>
          </ng-container>
    
          <!-- Symbol Column -->
          <ng-container matColumnDef="symbol">
            <th mat-header-cell *matHeaderCellDef> Symbol </th>
            <td mat-cell *matCellDef="let element"> {{element.type}} </td>
          </ng-container>
    
          <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
          <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
        </table>
      </div>
    </main>

当我 console.log orders 它返回这个:

【问题讨论】:

标签: angular angular-material


【解决方案1】:

您对matColumnDef="" 属性的定义是错误的。请参阅下面的示例,

    <!-- Position Column -->
    <ng-container matColumnDef="position"> <!--matColumnDef PROPERTY IS "position" HERE. -->
        <th mat-header-cell *matHeaderCellDef> No. </th>
        <td mat-cell *matCellDef="let element"> {{element.position}} </td> <!--AND "position" IS ALSO USED HERE.-->
    </ng-container>

所以你的代码需要是这样的:

    <main>
      <div id="content">
        <mat-form-field>
          <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
        </mat-form-field>
    
        <table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

          <!-- Position Column -->
          <ng-container matColumnDef="id">
            <th mat-header-cell *matHeaderCellDef> No. </th>
            <td mat-cell *matCellDef="let element"> {{element.id}} </td>
          </ng-container>
    
          <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
          <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
        </table>
      </div>
    </main>

【讨论】:

  • 你说得对,对不起,我不知道我是怎么错过的。谢谢!
  • 我遇到了同样的问题。感谢您分享的建议,因为它解决了问题。
猜你喜欢
  • 2020-12-08
  • 2019-11-11
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 1970-01-01
  • 2012-10-19
  • 1970-01-01
  • 2014-04-27
相关资源
最近更新 更多