【问题标题】:display category name instead id in mat table angular在 mat table angular 中显示类别名称而不是 id
【发布时间】:2020-09-15 10:01:14
【问题描述】:

您好,我使用角材料垫表来显示来自 api 的数据。我使用另一个 api 来处理类别。 我有两个模型,issuescategories。我暂时显示类别ID。 我需要显示相应的category 名称而不是它的id

有什么想法吗?

我的模型

export class Issue {
  IssueID: string;
  IssueTitle: string;
  IssueContent: string;
  CategoryID: number;
}


export class IssueCategory {
  public CategoryID: number;
  public CategoryName: string;
}

我的 html

...
  <ng-container matColumnDef="CategoryTitle">
    <mat-header-cell *matHeaderCellDef mat-sort-header>Category</mat-header-cell>
    <mat-cell *matCellDef="let row">{{row.categoryID}}</mat-cell>
  </ng-container>
...

编辑

我的 getAllIssues()

CategoryArray: TicketCategory[] = [];

getAllIssues(): void {
this.httpClient.get<Ticket[]>(this.API_URL).subscribe(data => {
this.dataChange.next(data);      

  this.data.map(issue => ({

    category: this.CategoryArray.find(category => category.CategoryID === issue.CategoryID)
  }));
  }
 }

谢谢!

【问题讨论】:

  • 我会创建一个组合类型,您可以在其中填写类别对象而不是 id,并将该组合类型用于您的表格

标签: angular typescript angular-material angular-material-table


【解决方案1】:

作为最简单的方法,您可以使用已经组合的数据作为数据源,例如:

// my-component.ts

@Component(...)
export class MyComponent {
  readonly categories: IssueCategory[] = [{
    CategoryID: 1,
    CategoryName: "First Category"
  }, {
    CategoryID: 2,
    CategoryName: "Second Category"
  }];

  readonly issues: Issue[] = [
    {IssueID: "first", IssueTitle: "First Issue", IssueContent: "first issue content", CategoryID: 1},
    {IssueID: "second", IssueTitle: "Second Issue", IssueContent: "second issue content", CategoryID: 2}
  ];

  readonly dataSource = this.issues.map(issue => ({
    title: issue.IssueTitle,
    content: issue.IssueContent,
    category: this.categories.find(category => category.CategoryID === issue.CategoryID)
  }));
}

或者您可以创建一个pipe,它将类别ID 转换为名称。在这种情况下,请记住您应该将管道包含在模块的声明数组中。

// category-by-id.pipe.ts

@Pipe({
  name: 'categoryById'
})
export class CategoryByIdPipe implements PipeTransform {
  transform(categoryId: number, categories: IssueCategory[]): string {
    const category = categories.find(category => category.CategoryID === categoryId);
    return category ? category.CategoryName : "";
  };
}
<!-- my-component.html -->

...
  <ng-container matColumnDef="category">
    <mat-header-cell *matHeaderCellDef> Category </mat-header-cell>
    <mat-cell *matCellDef="let element"> {{element.CategoryID | categoryById:categories}} </mat-cell>
  </ng-container>
...

StackBlitz 两个例子。希望对你有帮助。


Here is 另一个使用MatTableDataSource的例子

【讨论】:

  • 感谢您的回复@Valerin。我尝试从第一种方法中拟合您的代码。我已经有一个数据源,从那里我从 api 填充我的 mat-table。如何添加第二个?
  • @touinta 您能否添加有关该问题的更多详细信息?为什么不将第一个数据源替换为更新的数据源?
  • 我需要我的初始数据源,因为在那里我做了很多过滤、排序等工作...我可以尝试将您的代码添加到其中吗?....这很难,但我会给试一试……
  • @touinta 好的,如果您遇到问题,请告诉我。
  • 我更新了我的问题。我添加了我的服务功能,我在其中添加了一些代码。如何通过这个调用我的component.html中的类别名称?那可能吗?谢谢
猜你喜欢
  • 2012-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 2017-12-28
相关资源
最近更新 更多