有一个由多个标题行组成的数据表示例

|          |  Song 1        | Song 2         | Song 3         |
|---------------------------|----------------|----------------|
|  Artist  | Perc % | Val $ | Perc % | Val $ | Perc % | Val $ |
|-------------------------------------------------------------|
| Q1       | 10%    | 200$  | 15%    | 250$  |        |       |  
| BW       | 10%    | 200$  | 10%    | 200$  |        |       |
| POD      |  5%    | 150$  | 10$    | 200$  |        |       |
|          |        |       |        |       |        |       |
| SUM      | 25%    | 550$  | 25%    | 650$  |        |       |


使用提供的数据集

{
    "data": [{
            "artistId": "A001",
            "artistName": "Q1",
            "songs": [{
                    "songId": "S001",
                    "songName": "Song 1",
                    "percentage": "10%",
                    "value": "$200"
                },
                {
                    "songId": "S002",
                    "songName": "Song 2",
                    "percentage": "15%",
                    "value": "$250"
                }
            ]
        },
        {
            "artistId": "A002",
            "artistName": "BW",
            "songs": [{
                    "songId": "S001",
                    "songName": "Song 1",
                    "percentage": "10%",
                    "value": "$200"
                },
                {
                    "songId": "S002",
                    "songName": "Song 2",
                    "percentage": "10%",
                    "value": "$200"
                }
            ]
        },
        {
            "artistId": "A003",
            "artistName": "POD",
            "songs": [{
                    "songId": "S001",
                    "songName": "Song 1",
                    "percentage": "5%",
                    "value": "$150"
                },
                {
                    "songId": "S002",
                    "songName": "Song 2",
                    "percentage": "10%",
                    "value": "$200"
                }
            ]
        }
    ],
    "summary": [{
            "songName": "Song 1",
            "totalPercentage": "25%",
            "totalValue": "$550"
        },
        {
            "songName": "Song 2",
            "totalPercentage": "25%",
            "totalValue": "$650"
        }
    ]
}

我想知道是否可以映射提供的数据集,以便使用角材表在样本表上获得类似的东西?不需要翻页.

推荐答案

您应该通过由艺术家将多行合并为一行来转换数据.

转换后的数据应如下所示:

[
    {
        "artistName": "Q1",
        "percentage_0": "10%",
        "value_0": "$200",
        "percentage_1": "15%",
        "value_1": "$250"
    },
    ...,
    {
        "artistName": "SUM",
        "percentage_0": "25%",
        "value_0": "$550",
        "percentage_1": "25%",
        "value_1": "$650"
    }
]
displayedColumns: string[] = ['artistName'];

firstHeaderColumns = ['_'];

transformDataSource: any[] = [];

ngOnInit() {
  let songs = this.data.summary.map((x) => x.songName);
  this.firstHeaderColumns = this.firstHeaderColumns.concat(songs);
  this.displayedColumns = this.displayedColumns.concat(
    ...songs.map((x: string, i: number) => [`percentage_${i}`, `value_${i}`])
  );

  this.transformDataSource= this.data.data.map((x: any) => {
    let obj: any = { artistName: x.artistName };

    for (let i = 0; i < songs.length; i++) {
      obj[`percentage_${i}`] = x.songs[i].percentage;
      obj[`value_${i}`] = x.songs[i].value;
    }

    return obj;
  });

  let summary: any = { artistName: 'SUM' };
  this.data.summary.forEach((x: any, i: number) => {
    summary[`percentage_${i}`] = x.totalPercentage;
    summary[`value_${i}`] = x.totalValue;
  });
  this.transformDataSource.push(summary);
}
<table mat-table [dataSource]="transformDataSource" class="mat-elevation-z8">
  <ng-container *ngFor="let column of displayedColumns">
    <ng-container matColumnDef="{{column}}">
      <th mat-header-cell *matHeaderCellDef>
        <ng-container
          [ngTemplateOutlet]="headerTemplate"
          [ngTemplateOutletContext]="{ $implicit: column }"
        >
        </ng-container>
      </th>
      <td mat-cell *matCellDef="let element">{{element[column]}}</td>
    </ng-container>
  </ng-container>

  <ng-container *ngFor="let headerColumn of firstHeaderColumns">
    <ng-container matColumnDef="{{headerColumn}}">
      <th
        mat-header-cell
        *matHeaderCellDef
        [attr.colspan]="headerColumn != '_' ? 2 : 1"
        [style.text-align]="'center'"
      >
        {{headerColumn != '_' ? headerColumn : '' }}
      </th>
    </ng-container>
  </ng-container>

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

<ng-template let-prop #headerTemplate>
  <ng-container [ngSwitch]="prop.split('_')[0]" [style.text-align]="'center'">
    <ng-container *ngSwitchCase="'artistName'">Artist</ng-container>
    <ng-container *ngSwitchCase="'percentage'">Perc %</ng-container>
    <ng-container *ngSwitchCase="'value'">Value %</ng-container>
  </ng-container>
</ng-template>

Demo @ StackBlitz

Javascript相关问答推荐

单击树元素时阻止焦点事件触发?

vue3类型脚本中可能未定义对象''

Python类实现的JavaScript吊坠是什么样子的?

带对角线分隔符的图像滑动器

react 页面更改类别时如何返回第0页

如何解决(不忽略)reaction详尽的linter规则,而不会在获取数据时导致无限的reender循环

我不明白这个react 组件有什么问题

使用CDO时如何将Vue组件存储在html之外?

在NextJS中使用计时器循环逐个打开手风琴项目?

在具有焦点和上下文的d3多线图表中,如何将上下文的刷新限制在特定日期?

如何通过在提交时工作的函数显示dom元素?

具有相同参数的JS类

使用TMS Web Core中的HTML模板中的参数调用过程

单击子元素时关闭父元素(JS)

如何用拉威尔惯性Vue依赖下拉?

获取Uint8ClampedArray中像素数组的宽度/高度

如何在coCos2d-x中更正此错误

以编程方式聚焦的链接将被聚焦,但样式不适用

Socket.IO在刷新页面时执行函数两次

react 路由DOM有条件地呈现元素