立柱垫台 [英] Column oriented mat-table

查看:83
本文介绍了立柱垫台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的情况是,我从后端接收的数据是面向列的.此数据的示例如下:

I have a situation where the data I receive from my backend is column-oriented. An example of how this data looks like is this:

[
    { columnName: "ID", cells: [1, 2, 3, 4, 5] },
    { columnName: "Name", cells: ["a", "b", "c", "d", "e"] }
]

到目前为止,我已经设法像这样配置mat-table:

So far I have managed to configure my mat-table like this:

<table mat-table [dataSource]="data" class="mat-elevation-z8">
    <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
        <th mat-header-cell *matHeaderCellDef> {{column}} </th>
        <td mat-cell *matCellDef="let element">{{element | json}}</td>
    </ng-container>

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

这给了我以下结果:

实际上,我希望看到这样的表:

while in reality I'd like to see the table like this:

|------|------|
|  ID  | NAME |
|------|------|
|   1  |   a  |
|   2  |   b  |
|   3  |   c  |
|   4  |   d  |
|   5  |   e  |

是否可以通过某种方式来调整matRowDef,以便将cells属性定义为行?理想情况下,我只想在mat-table中进行更改,因此不需要操纵数据,以后再将其转换回去.

Is there some way to adjust the matRowDef so it defines the cells property as rows? Ideally I would just like to change this in the mat-table, so I don't need to manipulate my data and later convert it back.

推荐答案

您可以尝试根据需要修改现有响应:

You can try by modifying the existing response as per your need:

HTML代码:

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

    <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
        <th mat-header-cell *matHeaderCellDef> {{column}} </th>
        <td mat-cell *matCellDef="let element"> {{element[column]}} </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';

import { MatTableDataSource } from '@angular/material';

const ELEMENT_DATA: any[] = [
  { columnName: "ID", cells: [1, 2, 3, 4, 5] },
  { columnName: "Name", cells: ["a", "b", "c", "d", "e"] }
];

/**
 * @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 = []
  dataSource = new MatTableDataSource([]);

  constructor() {
    // Take Column names dynamically
    ELEMENT_DATA.forEach(x => {
      this.displayedColumns.push(x.columnName)
    })

    // Format the array as you want to display
    let newlyFormedArray = ELEMENT_DATA.reduce((array, { columnName, cells }) => {
      cells.forEach((cell, index) => {
        array[index] = Object.assign({ [columnName]: cell }, array[index])
      })
      return array;
    }, [])
    this.dataSource = new MatTableDataSource(newlyFormedArray);
  }
}

StackBlitz

这篇关于立柱垫台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆