2017-07-29 3 views
-1

どのようにtypescriptファイルからデータテーブルの列にテンプレートを渡すことについての任意のアイデアはありますか?私は補間を使ってそれを達成しようとしていますが、それは助けにはならない。何か案は?材料2 datatable動的行テンプレート#angular4

おかげで、 アルン・クマール

+0

これらをコンポーネントとして定義し、それらをホスト要素usinに追加する必要がありますg componentfactoryresolver。これは、1つのSO答えの実装が大きすぎます。この文書のセクションではhttps://angular.io/guide/dynamic-component-loaderについて説明しています。より簡単な方法は、テンプレート内のすべての可能な列を定義し、ngIfを使用して適切な列を表示させることです。 – bryan60

+0

使用しているコードスニペットを投稿してください。または、ここにテンプレートを作成してください(https://plnkr.co/edit/tpl:AvJOMERrnz94ekVua0u5?p=catalogue)。 [ここで質問する方法](https://stackoverflow.com/help/how-to-ask)の詳細を読むことができます:) – 0mpurdy

答えて

0

私はあなたの質問を理解し、多分このような何かしようとしていない完全に確認してください:

​​

をしてから列を選択する

/** Table columns */ 
columns = [ 
    { columnDef: 'userId', header: 'ID',  cell: (row: UserData) => `${row.id}`  }, 
    { columnDef: 'userName', header: 'Name',  cell: (row: UserData) => `${row.name}`  }, 
    { columnDef: 'progress', header: 'Progress', cell: (row: UserData) => `${row.progress}%` } 
]; 

/** Column definitions in order */ 
displayedColumns = this.columns.map(x => x.columnDef); 
-1
export class MyDatatableComponent implements OnInit { 
    displayedColumns = ['Id', 'Name', 'Address']; 
    dataSource = new MatTableDataSource(); 
    @ViewChild(MatPaginator) paginator: MatPaginator; 
    @ViewChild(MatSort) sort: MatSort; 

    ngAfterViewInit(){ 
     this.dataSource.paginator = this.paginator 
     this.dataSource.sort = this.sort 
    } 

    ngOnInit() { 
     this.dataSource = new MatTableDataSource(data); 
    } 
} 

<mat-table [dataSource]="dataSource" matSort> 
    <ng-container *ngFor="let cols of displayedColumns" [matColumnDef]="cols"> 

     <mat-header-cell *matHeaderCellDef mat-sort-header> {{cols}} </mat-header-cell> 

     <mat-cell *matCellDef="let row"> {{row[cols]}} </mat-cell> 
    </ng-container> 

    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row> 

    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row> 
</mat-table> 

<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>