【问题标题】:Create sparse Html table创建稀疏 Html 表
【发布时间】:2017-12-16 22:26:36
【问题描述】:

我想创建如下的 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

我有以下结构

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
  ) {}
}

是否可以使用 angular 4 *ngfor 创建这样的 Html 表?我正在从后端获取 Profiler 列表。

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>

【问题讨论】:

  • 您应该知道of getProfilerKeys(profiler.methodProfilers) 将导致getProfilerKeys(...) 在每次更改检测轮次时被重复调用。绑定到方法通常是个坏主意。而是将方法调用的结果分配给一个字段并绑定到该字段。对于内部*ngFor,这需要是一个数组。
  • 是的,我知道,但我想先实现外观,然后优化绑定:)
  • 那么问题出在哪里?
  • @GünterZöchbauer 问题是我无法获得我想要的外观和感觉。我想要类似我在帖子开头发布的表格,现在我得到了我在帖子后面发布的图片
  • 很难说,因为从这里不可能知道你的方法调用返回什么。您应该更新您的问题,以便它提供所有信息以重现。您最好使用静态数据结构,并且仅在布局工作后将其转换为方法调用或您在实际应用程序中想要的任何内容。

标签: html angular html-table


【解决方案1】:

如果您将*ngFor 扩展为等效的&lt;ng-template&gt;,则更容易实现恕我直言。

然后为每个 Profile 创建一个 td,并将 td 上的 rowspan 属性设置为数组中“ProfilerKeys”的数量。

我使用函数isNewIndex() 来检测外部循环(Profiler 对象)何时更改其index 以绘制另一个跨行的td。

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

【讨论】:

  • 非常感谢。作品。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 2012-01-10
  • 2011-03-23
  • 1970-01-01
相关资源
最近更新 更多