简体   繁体   English

angular 13中显示数据时如何只显示月份一位?

[英]How to display month only one point while showing data in angular 13?

I'm New To angular. In a Milestone section there is a year and month loop.我是 angular 的新手。在里程碑部分有一个年月循环。 In the month loop the month name should be displayed only once for the specific year.在月份循环中,月份名称对于特定年份应该只显示一次。 please help me to find the possible solution.请帮我找到可能的解决方案。 My Html & Ts Codes are displayed below我的 Html & Ts 代码如下所示

HTML Code

<section class="milestone-wrapper">
    <section class="milestone-wrap-info">
        <ul class="timeline">
            <li *ngFor="let milestone of formattedMileStoneData">
                <div class="direction-r">
                    <div class="flag-wrapper">
                        <span class="flag">{{milestone.Year}}</span>
                    </div>
                    <div class="desc" *ngFor="let month of milestone.Month">
                        <label>{{month.MonthName}}</label>
                        <p>{{month.Details}}</p>
                    </div>
                </div>
            </li>
        </ul>
    </section>
</section>

Here is my Data这是我的数据

"data": [
            {
                "Id": "42",
                "Year": "2021",
                "MonthName": "December",
                "Details": "Introduce Minimum Wage Allowance for government staff",
                "DisplayOrder": "3",
                "LanguageID": "1",
                "Hide": "0"
            },
            {
                "Id": "41",
                "Year": "2021",
                "MonthName": "December",
                "Details": "Complete the pay structure model used in the Pay Framework",
                "DisplayOrder": "2",
                "LanguageID": "1",
                "Hide": "0"
            },
            {
                "Id": "18",
                "Year": "2021",
                "MonthName": "October",
                "Details": "Formulate the Job Evaluation Guidelines",
                "DisplayOrder": "1",
                "LanguageID": "1",
                "Hide": "0"
            }];

Here is My Ts Code这是我的 Ts 代码

this.formattedMileStoneData = this.allMileStones.reduce(
          (a: any, b: any) => {
            const element = a.find((x: any) => x.Year == b.Year);
            if (element) element.Month.push(b);
            else a.push({ Year: b.Year, Month: [b] });
            return a;
          },
          []
        );

Here is the result这是结果

在此处输入图像描述

But the desired result Should be但期望的结果应该是

在此处输入图像描述

Please Help me out to solve this issue.请帮我解决这个问题。 Thanks in Advance.提前致谢。

Here's a basic codesandbox link I made that should do what you want.这是我制作的基本 codesandbox 链接,它应该可以满足您的需求。 https://codesandbox.io/s/xenodochial-rui-bgos9t?file=/src/app/app.component.html https://codesandbox.io/s/xenodochial-rui-bgos9t?file=/src/app/app.component.html

In the typescript I created two member variables called currentYear and currentMonth and assigned them to empty strings.在 typescript 中我创建了两个成员变量 currentYear 和 currentMonth 并将它们分配给空字符串。

Check the current year and check if it's equal to the year of the current iteration in the loop.检查当前年份并检查它是否等于循环中当前迭代的年份。 If they are equal, don't display the year.如果它们相等,则不显示年份。 If they are not equal, update the current year variable and display the year.如果它们不相等,则更新当前年份变量并显示年份。

  displayYear(year: string): boolean {
    if (year === this.currentYear) {
      return false;
    } else {
      this.currentYear = year;
      return true;
    }
  }

Similar to the year logic but for the month.与年逻辑类似,但针对的是月份。

  displayMonthName(year: string, month: string): boolean {
    if (year === this.currentYear && month === this.currentMonth) {
      return false;
    } else {
      this.currentYear = year;
      this.currentMonth = month;
      return true;
    }
  }

A simplified version of your code.您的代码的简化版本。

<section class="milestone-wrapper">
  <section class="milestone-wrap-info">
    <ul class="timeline">
      <li *ngFor="let milestone of data">
        <div class="direction-r">
          <div class="flag-wrapper">
            <span class="flag" *ngIf="displayYear(milestone.Year)"
              >{{milestone.Year}}</span
            >
          </div>
          <div class="desc">
            <label *ngIf="displayMonthName(milestone.Year, milestone.MonthName)"
              >{{milestone.MonthName}}</label
            >
            <p>{{milestone.Details}}</p>
          </div>
        </div>
      </li>
    </ul>
  </section>
</section>

I don't recommed using any it defeats the purpose of typescript.我不建议使用任何它会破坏 typescript 的目的。

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

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