嵌套循环应该在表格中显示。

huangapple go评论68阅读模式
英文:

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 &lt;th&gt; before French.

Based on this presumption, the usage of an ng-container would solved your problem.

&lt;ng-container *ngFor=&quot;let elem of item.ELEMENT&quot;&gt;
  &lt;tr &gt;
  &lt;th colspan=&quot;3&quot;&gt;COUNTRY&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td  colspan=&quot;3&quot;&gt; {{ elem.PAYS_LIB }} &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
  &lt;th&gt;LABEL&lt;/th&gt;
  &lt;th&gt;ISINCODE&lt;/th&gt;
&lt;/tr&gt;

&lt;tr *ngFor=&quot;let elem of item.ELEMENT&quot;&gt;
  &lt;td&gt; {{ elem.LABEL}}&lt;/td&gt;
  &lt;td&gt; {{ elem.ISINCODE}}&lt;/td&gt;
&lt;/tr&gt;
&lt;/ng-container&gt;

Here is a fork of your example.

答案3

得分: 2

欢迎来到Stack Overflow!

我认为您只想要将所有数据放在一个3列表格中?

只需将&lt;td&gt; {{ elem.LABEL}}&lt;/td&gt;&lt;td&gt; {{ elem.LABEL}}&lt;/td&gt;的引用移到第一个循环中,像这样。

&lt;div class=&quot;container text-center &quot;&gt;
&lt;h2 class=&quot;pt-3 pb-3&quot;&gt;HTML Table&lt;/h2&gt;

&lt;table *ngFor=&quot;let item of REGROUPEMENT&quot;&gt;
  &lt;tr&gt;
    &lt;th&gt;TYPE&lt;/th&gt;
    &lt;th&gt;PRICE&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr &gt;
    &lt;td&gt;{{ item.ASSETCATLABEL }}&lt;/td&gt;
    &lt;td&gt;{{ item.AMOUNT }}&lt;/td&gt;
  &lt;/tr&gt;

  &lt;tr&gt;
    &lt;th&gt;COUNTRY&lt;/th&gt;
    &lt;th&gt;LABEL&lt;/th&gt;
    &lt;th&gt;ISINCODE&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr *ngFor=&quot;let elem of item.ELEMENT&quot;&gt;
    &lt;td&gt; {{ elem.PAYS_LIB }} &lt;/td&gt;
    &lt;td&gt; {{ elem.LABEL}}&lt;/td&gt;
    &lt;td&gt; {{ elem.ISINCODE}}&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;

这将生成如下表格:

COUNTRY	    LABEL	    ISINCODE
BELGIUM	    AB INBEV	NO0010571698
FRENCH	    WWI	        AO0010571614

我无法确定这是否是您想要的,因为我不确定如何解释您的问题。您说:

&lt;th&gt;COUNTRY:&lt;/th&gt;
BELGIUM
&lt;th&gt;LABEL:&lt;/th&gt; | &lt;th&gt;ISINCODE: &lt;/th&gt;
AB INBEV | NO0010571698
FRENCH
&lt;th&gt;LABEL:&lt;/th&gt; | &lt;th&gt;ISINCODE: &lt;/th&gt;
WWI | AO0010571614

... 但您是否真的希望BELGIUM在一对标签内,例如&lt;td&gt;BELGIUM&lt;/td&gt;?您是否真的希望在同一个表格中有一个标题行,然后是数据行,然后是另一个标题行?这有点不寻常。

因此,我尽力猜测您想要什么。

也有可能您想要将所有BELGIUM项目放在一个表格中,然后将所有FRENCH项目放在另一个表格中。然而,如果是这种情况,请提供一个明确的示例,显示每个输出文本周围的标签。

英文:

Bienvenue à Stack Overflow!

I think you just want all the data in a 3-column table?

Just move the references to &lt;td&gt; {{ elem.LABEL}}&lt;/td&gt; and <td> {{ elem.LABEL}}&lt;/td&gt; to within the first loop, like this.

    &lt;div class=&quot;container text-center &quot;&gt;
    &lt;h2 class=&quot;pt-3 pb-3&quot;&gt;HTML Table&lt;/h2&gt;

&lt;table *ngFor=&quot;let item of REGROUPEMENT&quot;&gt;
  &lt;tr&gt;
    &lt;th&gt;TYPE&lt;/th&gt;
    &lt;th&gt;PRICE&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr &gt;
    &lt;td&gt;{{ item.ASSETCATLABEL }}&lt;/td&gt;
    &lt;td&gt;{{ item.AMOUNT }}&lt;/td&gt;
  &lt;/tr&gt;

  &lt;tr&gt;
    &lt;th&gt;COUNTRY&lt;/th&gt;
    &lt;th&gt;LABEL&lt;/th&gt;
    &lt;th&gt;ISINCODE&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr *ngFor=&quot;let elem of item.ELEMENT&quot;&gt;
    &lt;td&gt; {{ elem.PAYS_LIB }} &lt;/td&gt;
    &lt;td&gt; {{ elem.LABEL}}&lt;/td&gt;
    &lt;td&gt; {{ elem.ISINCODE}}&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;

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:

&lt;th&gt;COUNTRY:&lt;/th&gt;
BELGIUM
&lt;th&gt;LABEL:&lt;/th&gt; | &lt;th&gt;ISINCODE: &lt;/th&gt;
AB INBEV | NO0010571698
FRENCH
&lt;th&gt;LABEL:&lt;/th&gt; | &lt;th&gt;ISINCODE: &lt;/th&gt;
WWI | AO0010571614

... but surely you would want BELGIUM to be inside a pair of tags, e.g. &lt;td&gt;BELGIUM&lt;/td&gt;? 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.

huangapple
  • 本文由 发表于 2023年1月9日 07:40:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定