英文:
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!
答案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[] = [
'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()]
}
}
And dropdown element here:
<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"
>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论