项数组作为 NgFor 的数据源

Item Array as Datasource for NgFor

只是想问一下是否可以在对象数组中搜索一个项目并将其用作我的 angular table 的数据源?你能分享一下语法吗?

基本上,我有一个由 Id 及其 json 文件组成的对象数组。填充其数据。寻找与它匹配的项目,并将其用作 ngfor 网格中的数据源。

我有下面的代码,但是returns编译错误。

谢谢

ts 文件

jsonData: Array<{ id: string, json: object }> = [];
ngOnInit(): void {
let json= [
  {
    "Id": "1",
    "Col1": "val11",
    "Col2": "val12
  },
  {
    "Id": "2",
    "Col1": "val21",
    "Col2": "val22
  }
]

this.jsonData.push({ id: "100", json: json });
}

html 文件

<ng-container *ngFor="let row of this.jsonData.find(e => e.id === "101").json;let i=row.Id">
        row.Col1 //etc..
</ng-container>

我使用了不同的方法来检查这是否对您有帮助。我正在从构造函数本身(ts 文件)过滤数据

这里是代码linkhttps://stackblitz.com/edit/angular-wn7vwb?file=app%2Ftable-basic-example.ts

HTML

<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.position}} </td>
  </ng-container>

  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <th mat-header-cell *matHeaderCellDef> Name </th>
    <td mat-cell *matCellDef="let element"> {{element.name}} </td>
  </ng-container>

  <!-- Weight Column -->
  <ng-container matColumnDef="weight">
    <th mat-header-cell *matHeaderCellDef> Weight </th>
    <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
  </ng-container>

  <!-- Symbol Column -->
  <ng-container matColumnDef="symbol">
    <th mat-header-cell *matHeaderCellDef> Symbol </th>
    <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
  </ng-container>

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

ts

import { Component } from '@angular/core';

const ELEMENT_DATA: PeriodicElement[] = [
  { position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
  { position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' },
  { position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' },
  { position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be' },
  { position: 5, name: 'Boron', weight: 10.811, symbol: 'B' },
  { position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C' },
  { position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N' },
  { position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O' },
  { position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F' },
  { position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne' },
];

/**
 * @title Basic use of `<table mat-table>`
 */
@Component({
  selector: 'table-basic-example',
  styleUrls: ['table-basic-example.css'],
  templateUrl: 'table-basic-example.html',
})
export class TableBasicExample {
  displayedColumns = ['position', 'name', 'weight', 'symbol'];
  dataSource = ELEMENT_DATA;

  constructor() {
    this.dataSource = this.dataSource.filter((res) => res.name == 'Helium');
  }
}

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}