Angular 6 ngFor 按键分组的表列表

Angular 6 ngFor list of tables grouped by key

我的 Angular 6 应用程序需要显示 table 的列表,其中 table 是对其组成元素的一组化学分析。

假设我有一个金属 alloy A。我对它进行了不同的化合物分析,找出它的化学成分:Fe:0.001%,Cu:0.042%,等等

这是我的数据源,它只是一个带有 mocks 的 typescript 文件

import { Certificate } from './certificate';

export const CERTIFICATES: Certificate[] = [
    { serie: '1050 AJ', ident: 'Fe', moy_certified: 0.297 },
    { serie: '1050 AJ', ident: 'Cu', moy_certified: 0.04 },
    { serie: '1050 AJ', ident: 'Mn', moy_certified: 0.0374 }, 
    { serie: 'X332.0 AC', ident: 'V', moy_certified: 0.019 },
    { serie: 'X4002 AA', ident: 'Mn', moy_certified: 0.037 }
];

我想在 HTML 中使用 Angular 6 在 table 的列表中显示此数据,其中每个系列的分析都按如下方式分组:

Serie: 1050 AJ
-------------------------
| Element | Composition |
-------------------------
|    Fe   |    0.0297   |
-------------------------
|    Cu   |    0.04     |
-------------------------
|    Mn   |    0.0374   |

Serie: X332.0 AC
-------------------------
| Element | Composition |
-------------------------
|    V    |    0.019    |

Serie: X332.0 AC
-------------------------
| Element | Composition |
-------------------------
|    Mn   |    0.037    |

我的 HTML 文件现在看起来像这样

<ul class="cert-result">
    <li *ngFor="let certificate of certificates">
      <table>
        <tr>
          <th>Serie</th>
          <th>Element</th>
          <th>Composition</th>
        </tr>
        <tr>
          <td>{{certificate.serie}}</td>
          <td>{{certificate.ident}}</td>
          <td>{{certificate.moy_certifiee}}</td>
        </tr>
      </table>
    </li>
  </ul>

显然,这不是正确的方法,因为它为我的数据源的每个元素创建了一个 table。

你必须改变数据结构。

解决方案。

your data

export const CERTIFICATES: Certificate[] = [
    { serie: '1050 AJ', ident: 'Fe', moy_certified: 0.297 },
    { serie: '1050 AJ', ident: 'Cu', moy_certified: 0.04 },
    { serie: '1050 AJ', ident: 'Mn', moy_certified: 0.0374 }, 
    { serie: 'X332.0 AC', ident: 'V', moy_certified: 0.019 },
    { serie: 'X4002 AA', ident: 'Mn', moy_certified: 0.037 }
];

在您的组件中创建一个方法。让我们说 formatedData()

import { CERTIFICATES } from './certificate';


class AppComponent {
  //Todo...

  objectKey(obj) {
    return Object.keys(obj);
  }

  formatedCerts() {
      return CERTIFICATES.reduce((prev, now) => {
        if (!prev[now.serie]) {
          prev[now.serie] = [];
        }

        prev[now.serie].push(now);
        return prev;
      }, {});

    /*
       Now your data : { "1050 AJ": [ .... ], "X332.0 AC": [...], ... }
    */

  }

}

现在在模板中:

    <ul class="cert-result">
      <li *ngFor="let key of objectKey(formatedCerts())">
        <span>{{key}}</span>
        <table>
          <tr>
            <th>Élément</th>
            <th>Moy. Certifiée</th>
          </tr>
          <tr *ngFor="let certificate of formatedCerts()[key]">
            <td>{{certificate.ident}}</td>
            <td>{{certificate.moy_certifiee}}</td>
          </tr>
    </table>
      </li>
    </ul>

如果要优化,将formatedCerts()的数据存入变量

  <table >
    <tr>
      <th>Serie</th>
      <th>Element</th>
      <th>Composition</th>
    </tr>
    <ng-container *ngFor="let certificate of certs">
    <tr *ngIf="certificate.serie == '1050 AJ'">
      <td>{{certificate.serie}}</td>
      <td>{{certificate.ident}}</td>
      <td>{{certificate.moy_certified}}</td>
    </tr>
    <tr *ngIf="certificate.serie == 'X332.0 AC'">
      <td>{{certificate.serie}}</td>
      <td>{{certificate.ident}}</td>
      <td>{{certificate.moy_certified}}</td>
    </tr>
    <tr *ngIf="certificate.serie == 'X4002 AA'">
      <td>{{certificate.serie}}</td>
      <td>{{certificate.ident}}</td>
      <td>{{certificate.moy_certified}}</td>
    </tr>
    </ng-container>
</table>

演示:https://stackblitz.com/edit/angular-3tvwgv

不是很简洁。希望其他人可以提供更好的解决方案。

您可以在 angular 应用中使用下划线轻松存档此文件。

    groupedSeriesNames = []
        groupedSeries = []
            Certificate[] = [
                { serie: '1050 AJ', ident: 'Fe', moy_certified: 0.297 },
                { serie: '1050 AJ', ident: 'Cu', moy_certified: 0.04 },
                { serie: '1050 AJ', ident: 'Mn', moy_certified: 0.0374 }, 
                { serie: 'X332.0 AC', ident: 'V', moy_certified: 0.019 },
                { serie: 'X4002 AA', ident: 'Mn', moy_certified: 0.037 }
            ];

this.groupedSeries = _.groupBy(this.Certificate, certificate=>certificate.serie);

    this.groupedSeriesNames = Object.keys(this.groupedSeries)

certificate.serie 将成为他们的关键,您可以将 certificate.serie 更改为任何其他 属性,例如 iden 或您需要的任何内容

你的html

<ul class="cert-result">
    <li *ngFor="let key of groupedSeriesNames">
      <table *ngFor="let certificate of groupedSeries[key]">
        <tr>
          <th>Serie</th>
          <th>Element</th>
          <th>Composition</th>
        </tr>
        <tr>
          <td>{{certificate.serie}}</td>
          <td>{{certificate.ident}}</td>
          <td>{{certificate.moy_certifiee}}</td>
        </tr>
      </table>
    </li>
  </ul>