我正在尝试对mat-table中的api派生数据进行排序,但数据是以ISO代码的形式发送的,我可以使用如下所示的库进行转换:
https://www.npmjs.com/package/iso-639-1
this.dataSource = new MatTableDataSource(response);<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操作的标识符。
对如何解决这个问题有什么建议吗?
发布于 2021-08-13 06:57:38
向您的响应添加/替换新属性,并显示/排序此属性
我想你有一些类似的东西:
this.dataService.getData().subscribe(response)=>{
this.dataSource = new MatTableDataSource(response);
}您可以使用管道(Map)来转换响应
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);
}https://stackoverflow.com/questions/68764732
复制相似问题