gpt4 book ai didi

angular-material - 如何更改 Angular Material 表中列的方向?

转载 作者:行者123 更新时间:2023-12-02 04:28:09 25 4
gpt4 key购买 nike

有谁知道这个例子中的表如何:https://stackblitz.com/angular/dlxbkjqaoba?file=app%2Ftable-basic-example.ts

使列水平定向,大致如图所示:enter image description here

最佳答案

您可以在类中的 ngOnInit() 方法中使用反转表来提供所需的数据结构,然后迭代 HTML 中的列列表以提供所需的列。下面是一个例子:

ts 文件:

import {Component, OnInit} from '@angular/core';

/**
* @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 implements OnInit {
inputColumns = ['position', 'name', 'weight', 'symbol'];
inputData = ELEMENT_DATA;

displayColumns: string[];
displayData: any[];
showTable: boolean;

ngOnInit() {
this.displayColumns = this.inputData.map(x => x.position.toString());
this.displayData = this.inputColumns.map(x => this.formatInputRow(x));

console.log(this.displayColumns);
console.log(this.displayData);

this.showTable = true;
}

formatInputRow(row) {
const output = {};

for (let i = 0; i < this.inputData.length; ++i) {
output[this.inputData[i].position] = this.inputData[i][row];
}

return output;
}
}

export interface PeriodicElement {
name: string;
position: number;
weight: number;
symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
{position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
{position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];


/** Copyright 2018 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license */

html 文件:

<table mat-table [dataSource]="displayData" class="mat-elevation-z8" *ngIf="showTable">

<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->

<!-- Position Column -->
<ng-container [matColumnDef]="column" *ngFor="let column of displayColumns">
<th mat-header-cell *matHeaderCellDef> {{ column }} </th>
<td mat-cell *matCellDef="let element"> {{ element[column] }} </td>
</ng-container>

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



<!-- Copyright 2018 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license -->

和工作 stackblitz: https://stackblitz.com/edit/angular-h6dvzc

编辑过的 stackblitz: https://stackblitz.com/edit/angular-h6dvzc-jjqdpk

关于angular-material - 如何更改 Angular Material 表中列的方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51043232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com