【发布时间】:2021-08-29 12:37:51
【问题描述】:
我能够使用虚拟数据手动创建表格,但我的问题是我正在尝试组合多个数据源以实现此 HTML 表格结构。
完整示例请参见 STACKBLITZ。
数据看起来像这样(关注活动字段):
let data = {
id: '60bf06e6fc8f613117de1db9',
activities: {
'0': ['Power', 4, '', 2.5, '', 0, ''],
'1': ['Attitude', 2, '', 3, '', 0, ''],
'2': ['NR', 4.5, '', 1.5, '', 0, ''],
'3': ['FMS', 4, '', 4, '', 0, ''],
'4': ['Automation', 2.5, '', 2.5, '', 0, ''],
'5': ['Path', 4.5, '', 2.5, '', 0, ''],
'6': ['Systems', 2, '', 2.5, '', 0, ''],
'7': ['Environment', 4.5, '', 2.5, '', 0, ''],
'8': ['Planning', 2, '', 2.5, '', 0, ''],
'9': ['Co-ordinate', 4.5, '', 3, '', 0, ''],
'10': ['Prioritize', 2.5, '', 3, '', 0, ''],
'11': ['Workload', 4.5, '', 2.5, '', 0, ''],
'12': ['Crew', 4, '', 3, '', 0, ''],
'13': ['ATC', 2.5, '', 3, '', 0, ''],
'14': ['Identify', 4, '', 2.5, '', 0, ''],
'15': ['Ass. Risk', 2, '', 4.5, '', 0, ''],
'16': ['Checklist', 4, '', 2.5, '', 0, ''],
'17': ['Analysis', 3, '', 3, '', 0, '']
}}
活动字段共有 18 个活动。每个都由其 id 和一系列活动标识。例如'0': ['Power', 4, '', 2.5, '', 0, ''],。 '0' 表示活动的 ID Power。 4 代表 Attempt1 - Grade & '' 代表 Attempt1 - Note;等(请参阅屏幕截图以进行说明)。
完整的活动列表存储在不同的变量/文件中,并具有以下结构。
Component.ts
this.activities = [{
"id": 0,
"activity": "Power",
"subject": "Control",
"icon": "icon-link"
},
{
"id": 1,
"activity": "Attitude",
"subject": "Control",
"icon": "icon-link"
},{...}]
this.groupedActivities = customGroupByFunction(this.activities, 'subject');
this.colors = {Control: '#bfbfbf', 'AFCS': '#bfbfbf', ...}
this.activitiesListKey = Object.keys(this.activitiesList);
下面是我的html代码。
<table>
<caption>Session Summary</caption>
<tr>
<th style="width: 40%"></th>
<th style="width: auto"></th>
<!-- Loop through AttemptCount variable
to populate this heading -->
<th style="width: auto" colspan="2" *ngFor="let attempt of attemptCounts(3)">Attempt {{attempt}}</th>
</tr>
<tr>
<th style="width: 40%">Subject Grouping</th>
<th style="width: auto"></th>
<!-- Populate these heading using the formula
AttemptCount * 2 (columns - Grade & Note) -->
<ng-container *ngFor="let attemptLabel of attemptCounts(3)">
<th style="width: auto">Grade</th>
<th style="width: auto">Note</th>
</ng-container>
</tr>
<!-- Loop through all Activity Subjects
And create heading accordingly. 6 Main Subjects so far -->
<!-- Next Subject -->
<ng-container *ngFor="let id of activitiesListKey">
<tr>
<!-- Generate [Rowspan] = (Subject.Array.Length + 1) -->
<th style="width: 40%;" rowspan="4" [style.background]="colors[id]">
{{id}}</th>
<!-- Loop through each Subject.Array Elements
and populate -->
<tr style="width: auto">
<th>Power</th>
<td>2.6</td>
<td>Can Improve</td>
<td>5.0</td>
<td>Excellent</td>
<td>4.5</td>
<td>Regressed</td>
</tr>
<tr style="width: auto">
<th>Attitude</th>
<td>4.0</td>
<td>Fantastic</td>
<td>4.5</td>
<td>Getting Better</td>
<td>5.0</td>
<td>Nice</td>
</tr>
<tr style="width: auto">
<th>NR</th>
<td>2.6</td>
<td>Can Improve</td>
<td>5.0</td>
<td>Excellent</td>
<td>4.5</td>
<td>Regressed</td>
</tr>
<!-- </tr> -->
</ng-container>
</table>
注意: attemptCounts(n) 只是一个返回 n 元素数组的函数。例如attemptCounts(3) 将返回[0,1,2]
如果要使表格更易于生成,我愿意更改我的 data.activities 的结构。因此,如果有人有适用于不同数据模型的解决方案,请分享。
“越来越好”、“优秀”的值由教师从表单字段中输入。所以它们可以是常数,也可以不是。甚至只是空字符串。这样也行。
有人可以帮我动态生成这张表吗?我已经为此苦苦挣扎了好几天,似乎找不到适合我的解决方案。
所以,请帮助遇到困难的朋友。
【问题讨论】:
-
@GaurangDhorda,你可以这样做
this.groupedActivities = customGroupByFunction(this.activities, 'activity'); -
@robert,我刚刚更新了这个问题。谢谢
-
@AllJs stackblitz.com/edit/… 先把这个 stacblitz 分叉,然后在这个 stackblitz 中添加你所有的代码,然后在这里分享你更新的 stackblitz 代码链接。理解起来会更有帮助
-
这里是堆栈闪电战@robert stackblitz.com/edit/angular-ivy-xkatki?file=src/app/…。我还用指向 stackblitz 的链接更新了问题。谢谢
-
@AllJs 这里是演示 stackblitz.com/edit/… 看看。
标签: javascript html angular html-table ngfor