【问题标题】:How to disable future weeks based on year Angular 8如何根据年份 Angular 8 禁用未来几周
【发布时间】:2022-02-19 05:01:33
【问题描述】:

我在 UI 中有年份和周的下拉菜单,根据所选年份,我显示周下拉列表中的周列表

现在我想禁用未来几周

前 2022 年, 显示 1、2、3、4、.....53 周等值列表的周下拉列表正在显示 但我希望用户只能选择直到当前日期 2 月的几周。

预期输出 - 用户无法选择未来几周,否则函数将仅显示当前周数而不是未来几周。

下面是我的代码,它根据年份计算周数。

任何人都可以根据我的逻辑帮助我禁用未来几周。

week(y = 0) {
    y = y ? y : new Date().getFullYear();
    let d, isLeap;
    d = new Date(y, 0, 1);
    isLeap = new Date(y, 1, 29).getMonth() === 1;
    let count = d.getDay() === 4 || isLeap && d.getDay() === 3 ? 53 : 52;
    this.numbers = Array(count).fill(0).map((x, i) => i + 1);
  }


   

【问题讨论】:

  • 请为您的示例提供 stackblitz 链接
  • @RomanKostetskyi,请参阅stackblitz.com/edit/…
  • @RomanKostetskyi,不知道在 stackblitz 中,由于材料模块,星期没有出现在下拉列表中,但在本地星期在我的机器上正确显示。

标签: javascript angular typescript date angular8


【解决方案1】:

我建议接下来的步骤:

  1. 创建函数getWeekNumberByDate
  2. 按年份获取上周数字(在下拉菜单中选择年份)
  3. 生成一系列周选项
  4. 获取当前周数
  5. 设置条件 disabled=currentWeekNumber

让我们编码吧。

因 cmets 而更新

app.component.ts

import { Component } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  public form: FormGroup = new FormGroup({
    year: new FormControl(''),
    week: new FormControl(''),
  });

  public yearOptions = this.getYearOptions();
  public weekOptions = [];
  public currentWeekNumber: number = this.getWeekNumberByDate(new Date());
  public currentYear = new Date().getFullYear();

  onSelectYear() {
    const selectedYear = this.form.value.year;
    this.weekOptions = this.getWeekOptions(selectedYear);
  }

  private getYearOptions() {
    const years = [];
    const fromYear = 2021;
    const toYear = new Date().getFullYear() + 2;

    for (let i = fromYear; i <= toYear; i++) {
      years.push(i);
    }
    return years;
  }

  private getWeekOptions(year: number) {
    const date = new Date();
    date.setFullYear(year);

    const lastDayInYear = new Date(new Date().getFullYear(), 11, 31);
    const lastWeekNumberInYear = this.getWeekNumberByDate(lastDayInYear);

    return Array(lastWeekNumberInYear)
      .fill(0)
      .map((x, i) => ({ weekNumber: i + 1, year: year }));
  }

  /*
   Function source here: 
   https://stackoverflow.com/a/6117889/7979902
  */
  private getWeekNumberByDate(d) {
    d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
    d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay() || 7));
    const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
    const weekNo = Math.ceil(((+d - +yearStart) / 86400000 + 1) / 7);
    return weekNo;
  }
}

app.component.html

<form [formGroup]="form">
  <div class="col-md-3">
    <mat-select
      class="smed-input"
      formControlName="year"
      placeholder="Select SMED Year*"
      (selectionChange)="onSelectYear()"
    >
      <mat-option *ngFor="let year of yearOptions" [value]="year">
        {{ year }}
      </mat-option>
    </mat-select>
  </div>
  <div class="col-md-3">
    <mat-select
      formControlName="week"
      class="smed-input"
      placeholder="Select Week*"
    >
      <mat-option
        *ngFor="let item of weekOptions"
        [value]="item.weekNumber"
        [disabled]="
          currentYear < item.year ||
          (currentYear === item.year && currentWeekNumber < item.weekNumber)
        "
        placeholder="Week Number*"
      >
        {{ item.weekNumber }}
      </mat-option>
    </mat-select>
  </div>
</form>

stackblitz的链接

更新了stackblitz的链接

【讨论】:

  • 您的代码运行良好,但过去一年的几周也被禁用。前 - 如果我在年份下拉列表中选择 2021,那么在第 6 周之后,所有周都将失效,这是错误的
  • 我希望启用过去一年的几周,只应禁用未来几周。
  • @Shubhampandey 更新
  • 感谢它现在按预期工作。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-17
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多