英文:
nested loop that should display in a table
问题
我想创建一个HTML表格,并显示每个国家的数据(PAYS_LIB)。
我想要获得这个结果:
<th>COUNTRY:</th>
BELGIUM
<th>LABEL:</th> | <th>ISINCODE: </th>
AB INBEV | NO0010571698
FRENCH
<th>LABEL:</th> | <th>ISINCODE: </th>
WWI | AO0010571614
目前,我有以下内容:
<th>COUNTRY:</th>
BELGIUM
FRENCH
<th>LABEL:</th> | <th>ISINCODE: </th>
AB INBEV | NO0010571698
<th>LABEL:</th> | <th>ISINCODE: </th>
WWI | AO0010571614
我不熟悉嵌套循环...
以下是JSON的示例:
REGROUPEMENT = [
{
TYPEVALUE: 11,
ASSETCATLABEL: 'Actions individuelles',
CURRENCY: 'EUR',
AMOUNT: 1646956.5,
PERCENTAGE: 79.22,
ELEMENT: [
{
LABEL: 'AB INBEV',
PAYS_LIB: 'BELGIUM',
ISINCODE: 'NO0010571698',
},
{
LABEL: 'WWI',
PAYS_LIB: 'FRENCH',
ISINCODE: 'AO0010571614',
},
],
},
];
以下是我的代码:
<div class="container text-center ">
<h2 class="pt-3 pb-3">HTML Table</h2>
<table *ngFor="let item of REGROUPEMENT">
<tr>
<th>TYPE</th>
<th>PRICE</th>
</tr>
<tr >
<td>{{ item.ASSETCATLABEL }}</td>
<td>{{ item.AMOUNT }}</td>
</tr>
<tr>
<th colspan="3">COUNTRY</th>
</tr>
<tr *ngFor="let elem of item.ELEMENT">
<td colspan="3"> {{ elem.PAYS_LIB }} </td>
</tr>
<tr>
<th>LABEL</th>
<th>ISINCODE</th>
</tr>
<tr *ngFor="let elem of item.ELEMENT">
<td> {{ elem.LABEL}}</td>
<td> {{ elem.ISINCODE}}</td>
</tr>
</table>
</div>
英文:
I want to create an HTML table and display data for each country (PAYS_LIB)
.
I want to obtain this result:
<th>COUNTRY:</th>
BELGIUM
<th>LABEL:</th> | <th>ISINCODE: </th>
AB INBEV | NO0010571698
FRENCH
<th>LABEL:</th> | <th>ISINCODE: </th>
WWI | AO0010571614
For now, I have this:
<th>COUNTRY:</th>
BELGIUM
FRENCH
<th>LABEL:</th> | <th>ISINCODE: </th>
AB INBEV | NO0010571698
<th>LABEL:</th> | <th>ISINCODE: </th>
WWI | AO0010571614
I'm not familiar with nested loops...
Here is an idea of the JSON
REGROUPEMENT = [
{
TYPEVALUE: 11,
ASSETCATLABEL: 'Actions individuelles',
CURRENCY: 'EUR',
AMOUNT: 1646956.5,
PERCENTAGE: 79.22,
ELEMENT: [
{
LABEL: 'AB INBEV',
PAYS_LIB: 'BELGIUM',
ISINCODE: 'NO0010571698',
},
{
LABEL: 'WWI',
PAYS_LIB: 'FRENCH',
ISINCODE: 'AO0010571614',
},
],
},
];
And my code
<div class="container text-center ">
<h2 class="pt-3 pb-3">HTML Table</h2>
<table *ngFor="let item of REGROUPEMENT">
<tr>
<th>TYPE</th>
<th>PRICE</th>
</tr>
<tr >
<td>{{ item.ASSETCATLABEL }}</td>
<td>{{ item.AMOUNT }}</td>
</tr>
<tr>
<th colspan="3">COUNTRY</th>
</tr>
<tr *ngFor="let elem of item.ELEMENT">
<td colspan="3"> {{ elem.PAYS_LIB }} </td>
</tr>
<tr>
<th>LABEL</th>
<th>ISINCODE</th>
</tr>
<tr *ngFor="let elem of item.ELEMENT">
<td> {{ elem.LABEL}}</td>
<td> {{ elem.ISINCODE}}</td>
</tr>
</table>
</div>
I also created a reproduction => https://stackblitz.com/edit/github-z6v4p8?file=src/app/todo/todo.component.ts
答案1
得分: 3
如果这是您想要的结果:
那么您的模板应该如下所示:
<table *ngFor="let item of REGROUPEMENT">
<tr>
<th>类型</th>
<th>价格</th>
</tr>
<tr>
<td>{{ item.ASSETCATLABEL }}</td>
<td>{{ item.AMOUNT }}</td>
</tr>
<tr>
<th colspan="2">国家</th>
</tr>
<ng-container *ngFor="let elem of item.ELEMENT">
<tr>
<td colspan=2>{{ elem.PAYS_LIB }}</td>
</tr>
<tr>
<th>标签</th>
<th>ISIN代码</th>
</tr>
<tr>
<td>{{ elem.LABEL }}</td>
<td>{{ elem.ISINCODE }}</td>
</tr>
</ng-container>
</table>
在这种方法中,我们在 ng-container
上使用了 *ngFor
属性。基本上,这告诉 Angular 重复 ng-container
内部的所有内容,但不包括 <ng-container/>
标签本身。然后,通过使用 colspan=2
,我们“拉伸”没有兄弟节点的列,以更好地与整个表格对齐,从而得到上面截图中的表格。
英文:
If this is the result you want:
Then your template should look like:
<table *ngFor="let item of REGROUPEMENT">
<tr>
<th>TYPE</th>
<th>PRICE</th>
</tr>
<tr>
<td>{{ item.ASSETCATLABEL }}</td>
<td>{{ item.AMOUNT }}</td>
</tr>
<tr>
<th colspan="2">COUNTRY</th>
</tr>
<ng-container *ngFor="let elem of item.ELEMENT">
<tr>
<td colspan=2>{{ elem.PAYS_LIB }}</td>
</tr>
<tr>
<th>LABEL</th>
<th>ISINCODE</th>
</tr>
<tr>
<td>{{ elem.LABEL }}</td>
<td>{{ elem.ISINCODE }}</td>
</tr>
</ng-container>
</table>
In this approach, we used the *ngFor
attribute on ng-container
. Basically, this tells Angular to repeat all that's inside ng-container
, excluding the <ng-container/>
tag itself. Then with the use of colspan=2
, we "stretch" columns with no siblings to better align with the whole table, resulting in the table in the screenshot above.
答案2
得分: 2
我假设您期望的结果在"French"之前缺少<th>
标签。
根据这个假设,使用ng-container可以解决您的问题。
<ng-container *ngFor="let elem of item.ELEMENT">
<tr>
<th colspan="3">COUNTRY</th>
</tr>
<tr>
<td colspan="3">{{ elem.PAYS_LIB }}</td>
</tr>
<tr>
<th>LABEL</th>
<th>ISINCODE</th>
</tr>
<tr *ngFor="let elem of item.ELEMENT">
<td>{{ elem.LABEL}}</td>
<td>{{ elem.ISINCODE}}</td>
</tr>
</ng-container>
英文:
I am presuming that your desired result is missing a <th>
before French.
Based on this presumption, the usage of an ng-container would solved your problem.
<ng-container *ngFor="let elem of item.ELEMENT">
<tr >
<th colspan="3">COUNTRY</th>
</tr>
<tr>
<td colspan="3"> {{ elem.PAYS_LIB }} </td>
</tr>
<tr>
<th>LABEL</th>
<th>ISINCODE</th>
</tr>
<tr *ngFor="let elem of item.ELEMENT">
<td> {{ elem.LABEL}}</td>
<td> {{ elem.ISINCODE}}</td>
</tr>
</ng-container>
答案3
得分: 2
欢迎来到Stack Overflow!
我认为您只想要将所有数据放在一个3列表格中?
只需将<td> {{ elem.LABEL}}</td>
和<td> {{ elem.LABEL}}</td>
的引用移到第一个循环中,像这样。
<div class="container text-center ">
<h2 class="pt-3 pb-3">HTML Table</h2>
<table *ngFor="let item of REGROUPEMENT">
<tr>
<th>TYPE</th>
<th>PRICE</th>
</tr>
<tr >
<td>{{ item.ASSETCATLABEL }}</td>
<td>{{ item.AMOUNT }}</td>
</tr>
<tr>
<th>COUNTRY</th>
<th>LABEL</th>
<th>ISINCODE</th>
</tr>
<tr *ngFor="let elem of item.ELEMENT">
<td> {{ elem.PAYS_LIB }} </td>
<td> {{ elem.LABEL}}</td>
<td> {{ elem.ISINCODE}}</td>
</tr>
</table>
</div>
这将生成如下表格:
COUNTRY LABEL ISINCODE
BELGIUM AB INBEV NO0010571698
FRENCH WWI AO0010571614
我无法确定这是否是您想要的,因为我不确定如何解释您的问题。您说:
<th>COUNTRY:</th>
BELGIUM
<th>LABEL:</th> | <th>ISINCODE: </th>
AB INBEV | NO0010571698
FRENCH
<th>LABEL:</th> | <th>ISINCODE: </th>
WWI | AO0010571614
... 但您是否真的希望BELGIUM
在一对标签内,例如<td>BELGIUM</td>
?您是否真的希望在同一个表格中有一个标题行,然后是数据行,然后是另一个标题行?这有点不寻常。
因此,我尽力猜测您想要什么。
也有可能您想要将所有BELGIUM项目放在一个表格中,然后将所有FRENCH项目放在另一个表格中。然而,如果是这种情况,请提供一个明确的示例,显示每个输出文本周围的标签。
英文:
Bienvenue à Stack Overflow!
I think you just want all the data in a 3-column table?
Just move the references to <td> {{ elem.LABEL}}</td>
and <td> {{ elem.LABEL}}</td>
to within the first loop, like this.
<div class="container text-center ">
<h2 class="pt-3 pb-3">HTML Table</h2>
<table *ngFor="let item of REGROUPEMENT">
<tr>
<th>TYPE</th>
<th>PRICE</th>
</tr>
<tr >
<td>{{ item.ASSETCATLABEL }}</td>
<td>{{ item.AMOUNT }}</td>
</tr>
<tr>
<th>COUNTRY</th>
<th>LABEL</th>
<th>ISINCODE</th>
</tr>
<tr *ngFor="let elem of item.ELEMENT">
<td> {{ elem.PAYS_LIB }} </td>
<td> {{ elem.LABEL}}</td>
<td> {{ elem.ISINCODE}}</td>
</tr>
</table>
</div>
This gives a table like this:
COUNTRY LABEL ISINCODE
BELGIUM AB INBEV NO0010571698
FRENCH WWI AO0010571614
I can't be certain of that being what you want, because I am unsure how to interpret your question. You say:
<th>COUNTRY:</th>
BELGIUM
<th>LABEL:</th> | <th>ISINCODE: </th>
AB INBEV | NO0010571698
FRENCH
<th>LABEL:</th> | <th>ISINCODE: </th>
WWI | AO0010571614
... but surely you would want BELGIUM
to be inside a pair of tags, e.g. <td>BELGIUM</td>
? And would you really want a heading row, then a data row, and then another heading row, all within the same table? That is a little unusual.
So I have used my imagination to guess what you wanted.
It is possible that you want to group all the BELGIUM items together, in one table, and then all the FRENCH ones, in another table. However, if that is the case please show an explicit example of what you want, with tags around each piece of output text.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论