问题描述
我有一种情况,我从后端收到的数据是面向列的.该数据的示例如下:
[{ columnName: "ID", 单元格: [1, 2, 3, 4, 5] },{ columnName: "Name", 单元格: ["a", "b", "c", "d", "e"] }]到目前为止,我已经设法像这样配置我的 mat-table:
<ng-container [matColumnDef]="column" *ngFor="let 列的显示列"><th mat-header-cell *matHeaderCellDef>{{column}} </th><td mat-cell *matCellDef="let element">{{element |json}}</td></ng-容器><tr mat-header-row *matHeaderRowDef="displayedColumns"></tr><tr mat-row *matRowDef="let row; columns:displayedColumns;"></tr>
这给了我以下结果:
而实际上我希望看到这样的表格:
|------|------||身份证 |姓名 ||------|------||1 |||2 |乙 ||3 || ||4 |d ||5 |电子 |
有什么方法可以调整 matRowDef 以便将单元格属性定义为行?理想情况下,我只想在 mat-table 中更改它,因此我不需要操作我的数据,然后再将其转换回来.
您可以尝试根据需要修改现有响应:
HTML 代码:
<ng-container [matColumnDef]="column" *ngFor="let 列的显示列"><th mat-header-cell *matHeaderCellDef>{{column}} </th><td mat-cell *matCellDef="let element">{{元素[列]}} </td></ng-容器><tr mat-header-row *matHeaderRowDef="displayedColumns"></tr><tr mat-row *matRowDef="let row; columns:displayedColumns;"></tr>
TS 代码:
import { Component } from '@angular/core';从 '@angular/material' 导入 { MatTableDataSource };const ELEMENT_DATA: 任何[] = [{ columnName: "ID", 单元格: [1, 2, 3, 4, 5] },{ columnName: "Name", 单元格: ["a", "b", "c", "d", "e"] }];/*** @title`<table mat-table>`的基本使用*/@成分({选择器:'表基本示例',styleUrls: ['table-basic-example.css'],templateUrl: 'table-basic-example.html',})导出类 TableBasicExample {显示列 = []数据源 = 新的 MatTableDataSource([]);构造函数(){//动态获取列名ELEMENT_DATA.forEach(x => {this.displayedColumns.push(x.columnName)})//格式化你想要显示的数组让 newFormedArray = ELEMENT_DATA.reduce((array, { columnName, cells }) => {cell.forEach((cell, index) => {array[index] = Object.assign({ [columnName]: cell }, array[index])})返回数组;}, [])this.dataSource = new MatTableDataSource(newlyFormedArray);}}
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"] }
]
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>
which gives me the following result:
while in reality I'd like to see the table like this:
|------|------|
| ID | NAME |
|------|------|
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 5 | e |
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 Code:
<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 Code:
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);
}
}
这篇关于列式垫桌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!