简体   繁体   English

自定义Angular Material 2 DataTable表标题(按对象)

[英]Custom Angular Material 2 DataTable table header by object

I have a normal Material 2 DataTable, and I want to custom the table header by object 我有一个普通的Material 2 DataTable,我想按对象自定义表头

For Example: 例如:

      <mat-form-field style="width:100%">
        <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
      </mat-form-field>

      <div class="mat-elevation-z8">
        <table mat-table [dataSource]="dataSource" matSort>

            <ng-container matColumnDef="{{item}}" *ngFor="let item of old_title_list">
              <th mat-header-cell *matHeaderCellDef mat-sort-header> {{item}} </th>
              <td mat-cell *matCellDef="let row"> {{row[item]}} </td>
            </ng-container>

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

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

It successfully display the table, however the th is just presenting the item name defined in data_list object. 它成功地显示表,但是th只是呈现中定义的项目名称data_list对象。

So I expect I should change the code to something like below: 所以我希望我应该将代码更改为如下所示:

         <ng-container matColumnDef="{{item.data}}" *ngFor="let item of new_title_list">
              <th mat-header-cell *matHeaderCellDef mat-sort-header> {{item.title}} </th>
              <td mat-cell *matCellDef="let row">  {{ row[item.data] }} </td>
         </ng-container>

I expect it can get the header value by key but it doesn't. 我希望它可以通过键获取标头值,但事实并非如此。 What should I do ? 我该怎么办 ?

Sample data: 样本数据:

 var old_title_list = ['aaaaa', '1111', '2222', '3333'];

 var data_list: [{
   aaaaa: 'World',
   '1111': 'this is 1111',
   '2222': 'this is 2222',
   '3333': 'this is 3333',
 },{
   aaaaa: 'World', 
   '1111': 'this is 1111',
   '2222': 'this is 2222',
   '3333': 'this is 3333',
 },{
   aaaaa: 'World',
   '1111': 'this is 1111',
   '2222': 'this is 2222',
   '3333': 'this is 3333',
 }];


var new_title_list = [
    {data: 'aaaaa', title: 'Hello'},
    {data: '1111', title: '1'},
    {data: '2222', title: '2'},
    {data: '3333', title: '3'},
];

You item is object. 您的项目是对象。 You must get keys of object by item.keys(), after you can use array of keys to show column name. 您可以使用键数组显示列名之后,必须通过item.keys()获取对象的键。 You can try something like this: 您可以尝试如下操作:

<ng-container matColumnDef="{{item.keys()[i]}}" *ngFor="let item of data_list; let i = index">
              <th mat-header-cell *matHeaderCellDef mat-sort-header> {{item.keys()[i]}} </th>
              <td mat-cell *matCellDef="let row">  {{ (row[item.data] }} </td>
         </ng-container>

if you want use titles from title_list array you can use this code: 如果要使用title_list数组中的标题,可以使用以下代码:

<ng-container matColumnDef="{{title_list[i].title}}" *ngFor="let item of data_list; let i = index">
              <th mat-header-cell *matHeaderCellDef mat-sort-header> {{title_list[i].title}} </th>
              <td mat-cell *matCellDef="let row">  {{ (row[item.data] }} </td>
         </ng-container>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM