首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MatSort by language iso codes

MatSort by language iso codes
EN

Stack Overflow用户
提问于 2021-08-12 22:16:36
回答 1查看 36关注 0票数 0

我正在尝试对mat-table中的api派生数据进行排序,但数据是以ISO代码的形式发送的,我可以使用如下所示的库进行转换:

https://www.npmjs.com/package/iso-639-1

代码语言:javascript
复制
this.dataSource = new MatTableDataSource(response);
代码语言:javascript
复制
<ng-container matColumnDef="name">
    <mat-header-cell *matHeaderCellDef>NAME</mat-header-cell>
        <mat-cell *matCellDef="let element">
            {{ getNameByCode(element.code) }}
        </mat-cell>
</ng-container>

通过这个方法调用,表中显示的名称是正确的,但是dataSource不会改变,所以它是按ISO而不是按名称排序的。我可以在将名称提交给dataSource之前对其进行转换,但我希望避免这样做,因为ISO代码是我稍后用于PUT操作的标识符。

对如何解决这个问题有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-13 06:57:38

向您的响应添加/替换新属性,并显示/排序此属性

我想你有一些类似的东西:

代码语言:javascript
复制
this.dataService.getData().subscribe(response)=>{
    this.dataSource = new MatTableDataSource(response);
}

您可以使用管道(Map)来转换响应

代码语言:javascript
复制
this.dataService.getData().pipe(
  //to add a new property "codeFormatted"
  map((res:any[])=>{
     res.forEach(x=>x.codeFormatted=this.getNameByCode(x.code)
     return res;
  })
  //or to replace
  //  map((res:any[])=>{
  //   res.forEach(x=>x.code=this.getNameByCode(x.code)
  //   return res;
  //})
).subscribe(response)=>{
    this.dataSource = new MatTableDataSource(response);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68764732

复制
相关文章

相似问题

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