【发布时间】:2018-10-27 23:06:46
【问题描述】:
我的 Angular 6 应用需要显示一个表格列表,其中一个表格是对其组成元素的一组化学分析。
假设我有一种金属合金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 }
];
我想使用 Angular 6 以 HTML 格式在表格列表中显示这些数据,其中一个系列的每个分析都按如下方式分组:
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>
显然,这不是正确的做法,因为它为我的数据源的每个元素创建了一个表格。
【问题讨论】:
-
你需要改变数据结构
-
如果可能的话,我想保持数据结构不变,而是使用 Angular 来解决问题。
-
@JimP。您可以保留原始 DS 而不更改它,但最终您将从系列属性中获取值并将它们设置为键
-
@Jim P. 更多botes 的答案应该是正确的答案。图书馆的存在是有原因的。成为一个更好的程序员也意味着聪明并使用那里的资源......比如库/框架。你知道在你的情况下你正在使用 Angular ...如果你根本不想使用库,你应该使用 plan vanillaJS 构建你的 SPA
-
我已经知道了,但我一直在寻找不使用库的解决方案,因为我当前的上下文不允许我这样做。
标签: angular angular6 angular-ngfor