英文:
onClear event handling in primeng Calendar
问题
我正在尝试检测当按下退格键或删除键时,p-calendar 输入被清除。我的代码是:
<p-calendar
appendTo="body"
[(ngModel)]="endDateValue"
(onSelect)="emitAndSyncEndDate()"
(onClear)="onClear($event)"
></p-calendar>
**TS:**
onClear(e: any) {
console.log('clears', e);
}
它没有输出任何内容。这段代码不起作用。请指出我的错误。谢谢。
英文:
I'm trying to detect when the p-calendar input is cleared either by pressing backspace key or by pressing delete key. My code is:
<p-calendar
appendTo="body"
[(ngModel)]="endDateValue"
(onSelect)="emitAndSyncEndDate()"
(onClear)="onClear($event)"
></p-calendar>
TS:
onClear(e: any) {
console.log('clears', e);
}
It is not printing anything. The code is not working. Please point out my mistake here. Thanks.
答案1
得分: 1
你可以捕捉到两种不同的事件
-
当你点击 "X" 时,你会得到 "onClear"(要显示一个,你应该定义 [showClear]="true")
-
当你按下 "Clear" 时,你会得到 "onClearClick"(要显示一个,你应该定义 [showButtonBar]="true")
演示链接:https://stackblitz.com/edit/angular-42jfmc?file=src%2Fglobal_styles.css,src%2Fmain.ts,angular.json
英文:
You can catch 2 different Events
- When you click "X" you will get "onClear" (to show one you should define [showClear]="true")
- When you press "Clear" you will get onClearClick (to show one you should define [showButtonBar]="true")
The demo https://stackblitz.com/edit/angular-42jfmc?file=src%2Fglobal_styles.css,src%2Fmain.ts,angular.json
答案2
得分: 0
Calendar.onClear
事件没有任何参数,因此无法区分是通过键盘输入清除还是通过按钮点击清除。
但是,可以连接到组件的输入并监听其键盘输入事件:
import { AfterViewInit, Directive, EventEmitter, Output } from '@angular/core';
import { Calendar } from 'primeng/calendar';
export interface KeyboardClearEvent {
key: 'backspace' | 'delete';
}
@Directive({
selector: 'p-calendar[onKeyboardClear]',
})
export class CalendarOnKeyboardClearDirective implements AfterViewInit {
@Output()
readonly onKeyboardClear = new EventEmitter<KeyboardClearEvent>();
constructor(private readonly calendar: Calendar) {}
ngAfterViewInit(): void {
this.calendar.onInput.subscribe((event) => this.handleInput(event));
}
private handleInput(event: InputEvent): void {
if (!(<HTMLInputElement>event.target).value) {
switch (event.inputType) {
case 'deleteContentBackward':
this.onKeyboardClear.emit({ key: 'backspace' });
break;
case 'deleteContentForward':
this.onKeyboardClear.emit({ key: 'delete' });
break;
}
}
}
}
英文:
The Calendar.onClear
event does not have any parameters so you can't use this if you want to distinguish between clearing by keyboard input and clearing by a button click.
However, it's possible to hook up to the input of the component and listen to its keyboard input event:
import { AfterViewInit, Directive, EventEmitter, Output } from '@angular/core';
import { Calendar } from 'primeng/calendar';
export interface KeyboardClearEvent {
key: 'backspace' | 'delete';
}
@Directive({
selector: 'p-calendar[onKeyboardClear]',
})
export class CalendarOnKeyboardClearDirective implements AfterViewInit {
@Output()
readonly onKeyboardClear = new EventEmitter<KeyboardClearEvent>();
constructor(private readonly calendar: Calendar) {}
ngAfterViewInit(): void {
this.calendar.onInput.subscribe((event) => this.handleInput(event));
}
private handleInput(event: InputEvent): void {
if (!(<HTMLInputElement>event.target).value) {
switch (event.inputType) {
case 'deleteContentBackward':
this.onKeyboardClear.emit({ key: 'backspace' });
break;
case 'deleteContentForward':
this.onKeyboardClear.emit({ key: 'delete' });
break;
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论