简体   繁体   English

Angular 13 树 Ng For 循环无法正常工作

[英]Angular 13 tree Ng For Loop Not working Properly

Angular tree Loop is not working, here's the sample of code I'm trying to use. Angular 树循环不起作用,这是我尝试使用的代码示例。 Do I need to write anything else in my code, so that it will work?我是否需要在我的代码中编写任何其他内容才能正常工作? Please help me on this one.请帮我解决这个问题。 Thank you.谢谢你。

My HTML Code我的 HTML 密码

<h1>Example 1</h1>

<div *ngFor="let job of dataFormated">
  <details class="tree-parent-cover" *ngIf="job.rowspan != -1" [attr.rowspan]="
  job.rowspan > 0 ? job.rowspan : null
">
    <summary>{{job.JobFamily}}

    </summary>
    <details class="tree-parent-inner-cover" *ngIf="job.rowspan1 != -2" [attr.rowspan]="
        job.rowspan1 > 0 ? job.rowspan1 : null
      ">
      <summary>{{job.MajorGroup}}
      </summary>
    </details>
  </details>

</div>



<h1>Example 2</h1>


<div *ngFor="let job of dataFormated">
  <details class="tree-parent-cover" *ngIf="job.rowspan != -1" [attr.rowspan]="
  job.rowspan > 0 ? job.rowspan : null
">
    <summary>{{job.JobFamily}}

    </summary>
       
  </details>

  <details class="tree-parent-inner-cover" *ngIf="job.rowspan1 != -2" [attr.rowspan]="
        job.rowspan1 > 0 ? job.rowspan1 : null
      ">
      <summary>{{job.MajorGroup}}
      </summary>
  </details>

</div>

My Ts Code As Below我的 Ts 代码如下

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
  allhirearchy = [
    {
      JobFamily: '1',
      MajorGroup: '19',
    },
    {
      JobFamily: '1',
      MajorGroup: '19',
    },
    {
      JobFamily: '1',
      MajorGroup: '20',
    },
    {
      JobFamily: '1',
      MajorGroup: '20',
    },
    {
      JobFamily: '1',
      MajorGroup: '21',
    },
    {
      JobFamily: '1',
      MajorGroup: '21',
    },
    {
      JobFamily: '2',
      MajorGroup: '30',
    },
    {
      JobFamily: '2',
      MajorGroup: '30',
    },
    {
      JobFamily: '2',
      MajorGroup: '31',
    },
  ];

  dataFormated: any;

  ngOnInit() {
    this.hierarchy();
  }

  hierarchy() {
    this.dataFormated = this.allhirearchy.map((x: any, i: any) => ({
      ...x,
      rowspan:
        i == 0 || this.allhirearchy[i - 1].JobFamily != x.JobFamily
          ? this.allhirearchy.filter((f: any) => f.JobFamily == x.JobFamily)
              .length
          : -1,
      rowspan1:
        i == 0 || this.allhirearchy[i - 1].MajorGroup != x.MajorGroup
          ? this.allhirearchy.filter((f: any) => f.MajorGroup == x.MajorGroup)
              .length
          : -2,
    }));
  }
}

the below picture will be the expected result下图将是预期的结果

在此处输入图像描述

this is my Stackblitz Code这是我的 Stackblitz 代码

https://stackblitz.com/edit/angular-zgn6se?file=src%2Fapp%2Fapp.component.html https://stackblitz.com/edit/angular-zgn6se?file=src%2Fapp%2Fapp.component.html

In the stackblitz code example 1 show the correct way of working but the loop is not working properly在 stackblitz 代码示例 1 中显示了正确的工作方式,但循环无法正常工作

in the example 2 the coding is looping while we put outside of the details tab在示例 2 中,当我们将细节选项卡放在外部时,编码是循环的

help me out to solve this issue in looping帮我解决循环中的这个问题

Here is the simplified version of your code:这是您的代码的简化版本:

  • Exmaple1例1
    在此处输入图像描述

This means if(con1 && cond2)这意味着if(con1 && cond2)

  • Exmaple2例子2
    在此处输入图像描述

This means if(cond1 || cond2) .这意味着if(cond1 || cond2)

So, with rowspan = -1, value = 20 :因此,对于rowspan = -1, value = 20

  • With example1: rowspan != -1 && rowspan != -2 evaluates to false.对于示例 1: rowspan != -1 && rowspan != -2计算结果为 false。
  • With example2: rowspan != -1 || rowspan != -2使用示例 2: rowspan != -1 || rowspan != -2 rowspan != -1 || rowspan != -2 evaluates to true. rowspan != -1 || rowspan != -2计算结果为真。

Hope the explanation is clear.希望解释清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM