创建一个默认显示当前月份的下拉菜单 – Angular 和 TypeScript

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

Create a dropdown that shows the current month by default - Angular and TypeScript

问题

<p-dropdown
  [options]="months"
  [filter]="false"
  filterBy="nombre"
  [showClear]="true"
  placeholder="Mes"
  (onChange)="onChangeMonth($event)"
  (onSelect)="onSelectMonth(monthFilter)"
  [style]="{ width: '110px' }"
  class="dropdown-gestion"
  formControlName="city"
>
export class SaCalendarMonthComponent {
  monthFilter: Date = new Date();

  months: string[] = [
    'Enero',
    'Febrero',
    'Marzo',
    'Abril',
    'Mayo',
    'Junio',
    'Julio',
    'Agosto',
    'Septiembre',
    'Octubre',
    'Noviembre',
    'Diciembre'
  ];

  slectedMonth: any = null;

  onChangeMonth(event: any) {
    this.slectedMonth = event?.value;
  }

  onSelectMonth(value: Date) {
    this.monthFilter = value;
    const month = moment(this.monthFilter).format('MMMM');
  }
}
<p-calendar
  [(ngModel)]="monthFilter"
  view="month"
  dateFormat="mm"
  [readonlyInput]="true"
  [keepInvalid]="true"
  placeholder="Mes"
  pInputNumber
  [style]="{ width: '110px' }"
  (onSelect)="onSelectMonth(monthFilter)"
></p-calendar>
export class SaCalendarMonthComponent {
  monthFilter: Date = new Date();

  onSelectMonth(value: Date) {
    this.monthFilter = value;
    const month = moment(this.monthFilter).format('MMMM');
  }
}
英文:

I am trying to create a dropdown that by default shows me the current month. This is the code I have used:

    <p-dropdown
      [options]="months"
      [filter]="false"
      filterBy="nombre"
      [showClear]="true"
      placeholder="Mes"
      (onChange)="onChangeMonth($event)"
      (onSelect)="onSelectMonth(monthFilter)"
      [style]="{ width: '110px' }"
      class="dropdown-gestion"
      formControlName="city"
    >
export class SaCalendarMonthComponent {
  monthFilter: Date = new Date();

  months: string[] = [
    'Enero',
    'Febrero',
    'Marzo',
    'Abril',
    'Mayo',
    'Junio',
    'Julio',
    'Agosto',
    'Septiembre',
    'Octubre',
    'Noviembre',
    'Diciembre'
  ];

  slectedMonth: any = null;

  onChangeMonth(event: any) {
    this.slectedMonth = event?.value;
  }

  onSelectMonth(value: Date) {
    this.monthFilter = value;
    const month = moment(this.monthFilter).format('MMMM');
  }
}

I have also tried doing it with <p-calendar> but it shows me the month number, and I need it to show me the name.

<p-calendar
  [(ngModel)]="monthFilter"
  view="month"
  dateFormat="mm"
  [readonlyInput]="true"
  [keepInvalid]="true"
  placeholder="Mes"
  pInputNumber
  [style]="{ width: '110px' }"
  (onSelect)="onSelectMonth(monthFilter)"
></p-calendar>
export class SaCalendarMonthComponent {
  monthFilter: Date = new Date();

  onSelectMonth(value: Date) {
    this.monthFilter = value;
    const month = moment(this.monthFilter).format('MMMM');
  }
}

How could I fix it? Thank you very much!

Example

答案1

得分: 0

Sure, here's the translated part:

首先,你需要在你的 'SaCalendarMonthComponent' 类中实现 OnInit,这样你就可以拥有一个名为 ngOnInit 的函数,在组件渲染后会自动触发。然后,你需要在这里设置你的默认月份。最后,你需要在你的 p-dropdown 元素中添加 [(ngModel)],这样可以绑定值:

export class SaCalendarMonthComponent {
  monthFilter: Date = new Date();

  months: string[] = [
    'Enero',
    'Febrero',
    'Marzo',
    'Abril',
    'Mayo',
    'Junio',
    'Julio',
    'Agosto',
    'Septiembre',
    'Octubre',
    'Noviembre',
    'Diciembre'
  ];

  slectedMonth: any = null;

  onChangeMonth(event: any) {
    this.slectedMonth = event?.value;
  }

  onSelectMonth(value: Date) {
    this.monthFilter = value;
    const month = moment(this.monthFilter).format('MMMM');
  }
  
  async ngOnInit() {
        this.slectedMonth = this.months[new Date().getMonth()]
  }

}

还有下拉框元素:

<p-dropdown
      [options]="months"
      [filter]="false"
      filterBy="nombre"
      [(ngModel)]="slectedMonth"
      [showClear]="true"
      placeholder="Mes"
      (onChange)="onChangeMonth($event)"
      (onSelect)="onSelectMonth(monthFilter)"
      [style]="{ width: '110px' }"
      class="dropdown-gestion"
    >
英文:

Alright friend. First you need to implement in your 'SaCalendarMonthComponent' class OnInit, which enables you to have function called ngOnInit, which fires automatically after rendering your component. Then you have to set there your default month. Finally, you have to add [(ngModel)] to your p-dropdown element which enables you to bind values:

export class SaCalendarMonthComponent {
  monthFilter: Date = new Date();

  months: string[] = [
    &#39;Enero&#39;,
    &#39;Febrero&#39;,
    &#39;Marzo&#39;,
    &#39;Abril&#39;,
    &#39;Mayo&#39;,
    &#39;Junio&#39;,
    &#39;Julio&#39;,
    &#39;Agosto&#39;,
    &#39;Septiembre&#39;,
    &#39;Octubre&#39;,
    &#39;Noviembre&#39;,
    &#39;Diciembre&#39;
  ];

  slectedMonth: any = null;

  onChangeMonth(event: any) {
    this.slectedMonth = event?.value;
  }

  onSelectMonth(value: Date) {
    this.monthFilter = value;
    const month = moment(this.monthFilter).format(&#39;MMMM&#39;);
  }
  
  async ngOnInit() {
        this.slectedMonth = this.months[new Date().getMonth()]
  }

}

And dropdown element here:

&lt;p-dropdown
      [options]=&quot;months&quot;
      [filter]=&quot;false&quot;
      filterBy=&quot;nombre&quot;
      [(ngModel)]=&quot;slectedMonth&quot;
      [showClear]=&quot;true&quot;
      placeholder=&quot;Mes&quot;
      (onChange)=&quot;onChangeMonth($event)&quot;
      (onSelect)=&quot;onSelectMonth(monthFilter)&quot;
      [style]=&quot;{ width: &#39;110px&#39; }&quot;
      class=&quot;dropdown-gestion&quot;
    &gt;

huangapple
  • 本文由 发表于 2023年5月17日 21:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272873.html
匿名

发表评论

匿名网友

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

确定