Kay*_*Kay 14 angular-material angular
如何在mat表中添加自定义列.
例如,添加包含编辑图标的编辑列,其中包含包含当前元素的id的click事件.
<div class="example-container mat-elevation-z8">
<mat-table #table [dataSource]="dataSource">
<!--- 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="position">
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="weight">
<mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
</ng-container>
<!-- Color Column -->
<ng-container matColumnDef="symbol">
<mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
</div>
Run Code Online (Sandbox Code Playgroud)
Jua*_*ker 21
在您的TableBasicExample.ts上添加
export class TableBasicExample {
displayedColumns = ['position', 'name', 'weight', 'symbol', 'customColumn1'];
dataSource = new ExampleDataSource();
}
Run Code Online (Sandbox Code Playgroud)
并在您的html文件中添加列:
<ng-container matColumnDef="customColumn1">
<mat-header-cell *matHeaderCellDef> Custom Title</mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
</ng-container>
Run Code Online (Sandbox Code Playgroud)
小智 9
这是一种方法。这可能会帮助某人。
table.html
<ng-container matColumnDef="action">
<th mat-header-cell *matHeaderCellDef> Action </th>
<td mat-cell *matCellDef>
<ng-container *ngTemplateOutlet="templateRef"></ng-container>
</td>
</ng-container>
Run Code Online (Sandbox Code Playgroud)
table.component
@Input() templateRef: TemplateRef<any>;
Run Code Online (Sandbox Code Playgroud)
table-parent.html
<app-data-table [dataSource]="dataSource" [templateRef]="template">
<ng-template #template>
<button mat-icon-button color="warn">
<mat-icon>delete</mat-icon>
</button>
<button mat-icon-button color="primary">
<mat-icon>edit</mat-icon>
</button>
</ng-template>
</app-data-table>
Run Code Online (Sandbox Code Playgroud)
希望可以节省几个小时的搜索时间:)
小智 5
您要实现的功能也在这个包angular4-material-table 中实现,它添加了一些结构以允许行插入、编辑和删除。您有一个在此plunkr上添加自定义操作列的示例。
您可以使用所需的按钮/操作定义一列:
<mat-table [dataSource]="dataSource">
<ng-container matColumnDef="column1">
<mat-header-cell *matHeaderCellDef> column 1 title </mat-header-cell>
<mat-cell *matCellDef="let row">
<!-- col 1 -->
</mat-cell>
</ng-container>
<ng-container matColumnDef="column2">
<mat-header-cell *matHeaderCellDef> column 2 title </mat-header-cell>
<mat-cell *matCellDef="let row">
<!-- col 2 -->
</mat-cell>
</ng-container>
<ng-container matColumnDef="actionsColumn">
<mat-header-cell *matHeaderCellDef>
<button (click)="createNew()">Create new</button>
</mat-header-cell>
<mat-cell *matCellDef="let row">
<button (click)="edit()">Edit</button>
<button (click)="cancelOrDelete()">Cancel</button>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns">
</mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;">
</mat-row>
</mat-table>
Run Code Online (Sandbox Code Playgroud)
您必须在 displayColumns 字符串数组中包含该列:
displayedColumns = ['column1', 'column2', 'actionsColumn'];
Run Code Online (Sandbox Code Playgroud)
要为按钮/操作添加额外的列,您首先需要将该列添加到组件中显示的列列表中:
displayedColumns: string[] = ['name', 'email', 'phone', 'customColumn'];
Run Code Online (Sandbox Code Playgroud)
然后在 html 中添加一个额外的列并将按钮放在 mat-cell 中:
<ng-container matColumnDef="customColumn">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let person">
<button mat-button>Edit</button>
</td>
</ng-container>
Run Code Online (Sandbox Code Playgroud)
一切都完成了。就是这么简单。
| 归档时间: |
|
| 查看次数: |
33544 次 |
| 最近记录: |