首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >动态地将按钮添加到垫台角材料中。

动态地将按钮添加到垫台角材料中。
EN

Stack Overflow用户
提问于 2020-09-07 16:10:58
回答 2查看 3K关注 0票数 1

如何动态添加带有编辑和删除按钮的操作列。我有下面的设置

父组件中的数据源

代码语言:javascript
复制
columns: MatTable[] = [
    { columnDef: 'position', header: 'No.', cell: (element: any) => `${element.position}` },
    { columnDef: 'name', header: 'Name', cell: (element: any) => `${element.name}` },
    { columnDef: 'weight', header: 'Weight', cell: (element: any) => `${element.weight}` },
    { columnDef: 'symbol', header: 'Symbol', cell: (element: any) => `${element.symbol}` },
    { columnDef: 'action', header: 'action', cell: (element: any) => `${element.action}` }
  ];

const ELEMENT_DATA: PeriodicElement[] = [
  { position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H', action:`<a mat-raised-button href="https://www.google.com/" target="_blank">Link</a>` },
  { position: 2, name: 'Helium', weight: 4.0026, symbol: 'He', action:`<a (click)="openDialog('Update',element)">Edit</a> | <a (click)="openDialog('Delete',element)">Delete</a>` },
  { position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li', action:`<a (click)="openDialog('Update',element)">Edit</a> | <a (click)="openDialog('Delete',element)">Delete</a>` },
  { position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be', action:`<a (click)="openDialog('Update',element)">Edit</a> | <a (click)="openDialog('Delete',element)">Delete</a>` },
  { position: 5, name: 'Boron', weight: 10.811, symbol: 'B', action:`<a (click)="openDialog('Update',element)">Edit</a> | <a (click)="openDialog('Delete',element)">Delete</a>` },
  { position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C', action:`<a (click)="openDialog('Update',element)">Edit</a> | <a (click)="openDialog('Delete',element)">Delete</a>` },
  { position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N', action:`<a (click)="openDialog('Update',element)">Edit</a> | <a (click)="openDialog('Delete',element)">Delete</a>` },
  { position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O', action:`<a (click)="openDialog('Update',element)">Edit</a> | <a (click)="openDialog('Delete',element)">Delete</a>` },
  { position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F', action:`<a (click)="openDialog('Update',element)">Edit</a> | <a (click)="openDialog('Delete',element)">Delete</a>` },
  { position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne', action:`<a (click)="openDialog('Update',element)">Edit</a> | <a (click)="openDialog('Delete',element)">Delete</a>` },
];

 dataSource = ELEMENT_DATA;

 ngOnInit(): void {
    this.matTableConfig.columns = this.columns;
    this.matTableConfig.filter = false;
    this.matTableConfig.dataSource = this.dataSource;
  }

在子组件中

代码语言:javascript
复制
 @Input() matTableConfig: MatTableConfig;

 <table mat-table [dataSource]="matTableConfig?.dataSource" matSort>
            <ng-container *ngFor="let column of matTableConfig?.columns" matColumnDef={{column.columnDef}}>
                <th mat-header-cell *matHeaderCellDef mat-sort-header> {{ column.header }} </th>
                <td mat-cell *matCellDef="let element"> {{ column.cell(element) }} </td>
            </ng-container>
    
            <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
            <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    
            <!-- Row shown when there is no matching data. -->
            <tr class="mat-row" *matNoDataRow>
                <td class="mat-cell" colspan="columns.length">No data matching the filter</td>
            </tr>
        </table>
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63781017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档