2017-07-12 8 views
1

を作成します。私は、私は次のような構造まばらなHTMLテーブル

export class Profiler { 
    constructor(
    public classz: string, 
    public methodProfilers: {[key: string]: MethodProfiler;} 
) {} 
} 

export class MethodProfiler { 
    constructor(
    public count: number, 
    public totalTime: number, 
    public lastTotalTime: number, 
    public maxTime: number, 
    public avgTime: number, 
    public avgMemory: number 
) {} 
} 

が、それは角度4 *ngforを使用して、このようなHTMLテーブルを作成することが可能である必要があり

#  Class  Method  A  b  c  d 
1  User  get  10  20  30 40 
       set  40  30  20 10 
       find  40  30  20 10 
2  Profile get  10  20  30 40 
       set  40  30  20 10 
       find  40  30  20 10 

を、以下のようなHTMLテーブルを作成したいですか?私はバックエンドからプロファイラのリストを取得しています。

getProfilerKeys(methodProfilers: Map<string, MethodProfiler>) { 
    return Object.keys(methodProfilers); 
    } 

<div class="table-responsive"> 
        <table class="table table-sm table-bordered"> 
         <thead class="thead-default"> 
          <tr> 
           <th class="text-center">Classz</th> 
           <th class="text-center">Method</th> 
           <th class="text-center">Count</th> 
           <th class="text-center">TotalTime</th> 
           <th class="text-center">LastTotalTime</th> 
           <th class="text-center">MaxTime</th> 
           <th class="text-center">AvgTime</th> 
           <th class="text-center">AvgMemory</th> 
          </tr> 
         </thead> 
         <tbody> 
          <tr class="text-center" *ngFor="let profiler of page.content;"> 
           <td>{{profiler.classz}}</td> 
           <td> 
            <table> 
             <tr *ngFor="let key of getProfilerKeys(profiler.methodProfilers);"> 
              <td>{{key}}</td> 
             </tr> 
            </table> 
           </td> 
           <td> 
            <table> 
             <tr *ngFor="let key of getProfilerKeys(profiler.methodProfilers);"> 
              <td *ngFor="let subkey of getProfilerKeys(profiler.methodProfilers[key]);">{{profiler.methodProfilers[key][subkey]}}</td> 
             </tr> 
            </table> 
           </td> 
          </tr> 
         </tbody> 
        </table> 
       </div> 

enter image description here

+0

あなたはgetProfilerKeys(profiler.methodProfilers)の 'は' 'getProfilerKeys(...)は'すべての変更を検出順番で繰り返し呼び出されるようにしますことに注意してください。メソッドへのバインディングは、通常、悪い考えです。むしろ、メソッド呼び出しの結果をフィールドに代入し、代わりにそのフィールドにバインドします。内側の '* ngFor'では、これは配列である必要があります。 –

+0

はい私はそれを知っていますが、私はルックアンドフィールを最初に実現し、バインディングを最適化すると思っています:) –

+0

それで問題は何ですか? –

答えて

0

あなたがその<ng-template>同等に*ngForを展開すると私見を達成することがより容易です。

プロファイルごとにtdを1つ作成し、のrowspan属性をアレイのProfilerKeysの数に設定します。

Iは、外側ループ(プロファイラオブジェクト)が別の行スパンtdを描画するために、そのindexを変えるときを検出する機能isNewIndex()を使用します。

import { Component } from '@angular/core'; 
import { Profiler, MethodProfiler } from './profile'; 

@Component({ 
    selector: 'my-app', 
    template: ` 
    <table class="table table-sm table-bordered" width="100%"> 
    <thead class="thead-default"> 
     <tr> 
     <th class="text-center">#</th> 
     <th class="text-center">Class</th> 
     <th class="text-center">Method</th> 
     <th class="text-center">Count</th> 
     <th class="text-center">TotalTime</th> 
     <th class="text-center">LastTotalTime</th> 
     <th class="text-center">MaxTime</th> 
     <th class="text-center">AvgTime</th> 
     <th class="text-center">AvgMemory</th> 
     </tr> 
    </thead> 
    <tbody> 
     <ng-template ngFor let-profiler [ngForOf]="profilers" let-i="index"> 
     <ng-template ngFor let-method [ngForOf]="getProfilerKeys(profiler.methodProfilers)"> 
      <tr class="text-center"> 
      <td *ngIf="isNewIndex(i)" [attr.rowspan]="getProfilerKeys(profiler.methodProfilers).length">{{i + 1}}</td> 
      <td *ngIf="isNewIndex(i, true)" [attr.rowspan]="getProfilerKeys(profiler.methodProfilers).length">{{profiler.classz}}</td> 
      <td>{{method}}</td> 
      <td>{{profiler.methodProfilers[method].count}}</td> 
      <td>{{profiler.methodProfilers[method].totalTime}}</td> 
      <td>{{profiler.methodProfilers[method].lastTotalTime}}</td> 
      <td>{{profiler.methodProfilers[method].maxTime}}</td> 
      <td>{{profiler.methodProfilers[method].avgTime}}</td> 
      <td>{{profiler.methodProfilers[method].avgMemory}}</td> 
      </tr> 
     </ng-template> 
     </ng-template> 
    </tbody> 
    </table> 
` 
}) 

export class AppComponent { 

    lastIndex = -1; 

    getProfilerKeys(methodProfilers: Map<string, MethodProfiler>) { 
    return Object.keys(methodProfilers); 
    } 

    // on last call to this function per row pass true for the updateIndex param 
    isNewIndex(thisIndex: number, updateIndex : boolean) { 
    if (thisIndex === this.lastIndex) return false; 
    if (updateIndex === true) this.lastIndex = thisIndex; 
    return true; 
    } 

    profilers = [ 
    new Profiler('User', 
     { 
     'get' : new MethodProfiler(10,20,30,40,50,60), 
     'set' : new MethodProfiler(1,2,3,4,5,6) 
     } 
    ), 
    new Profiler('Profile', 
     { 
     'get' : new MethodProfiler(60,50,40,30,20,10), 
     'set' : new MethodProfiler(6,5,4,3,2,1) 
     } 
    ) 
    ]; 
} 

デモ:https://plnkr.co/edit/B5YYn9Jxx9nQCzxlr2VS?p=preview

+0

ありがとうございました。作品。 :) –

関連する問題