【发布时间】:2021-09-09 11:07:41
【问题描述】:
我有两张桌子(1 和 2)和两个按钮 A 和 B。
当我点击按钮 A 时,我希望向我显示 dataSource 和 按钮 B
当我点击 button B 时,会显示 maltr 和 button A
我已经用 Angular Material 创建了 2 个表格。
我应该怎么做才能找到解决方案?我可以使用 *ngIf
文件.html:
<button>A</button>
<button>B</button>
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.position}} </td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<br/>
<table mat-table [dataSource]="maltr" class="mat-elevation-z8">
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.position}} </td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </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';
export interface PeriodicElement {
name: string;
position: number;
}
const ELEMENT_DATA: PeriodicElement[] = [
{position: 1, name: 'Hydrogen'},
{position: 2, name: 'Helium'},
{position: 3, name: 'Lithium'},
{position: 4, name: 'Beryllium'},
{position: 5, name: 'Oxygen'}
];
/**
* @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: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = ELEMENT_DATA.slice(0, 1);
maltr = ELEMENT_DATA;
}
【问题讨论】:
-
分享你试过的代码
-
您打算添加更多表格吗?还是只有两个?
-
@H3AR7B3A7 只是两个
标签: angular angular-material mat-table