“onClear” 在 primeng 日历中的事件处理

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

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:

  &lt;p-calendar
  appendTo=&quot;body&quot;
  [(ngModel)]=&quot;endDateValue&quot;
  (onSelect)=&quot;emitAndSyncEndDate()&quot;
  (onClear)=&quot;onClear($event)&quot;
&gt;&lt;/p-calendar&gt;

TS:

  onClear(e: any) {
    console.log(&#39;clears&#39;, e);
  }

It is not printing anything. The code is not working. Please point out my mistake here. Thanks.

答案1

得分: 1

你可以捕捉到两种不同的事件

  1. 当你点击 "X" 时,你会得到 "onClear"(要显示一个,你应该定义 [showClear]="true")

  2. 当你按下 "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

  1. When you click "X" you will get "onClear" (to show one you should define [showClear]="true")

“onClear” 在 primeng 日历中的事件处理

  1. When you press "Clear" you will get onClearClick (to show one you should define [showButtonBar]="true")

“onClear” 在 primeng 日历中的事件处理

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 &#39;@angular/core&#39;;
import { Calendar } from &#39;primeng/calendar&#39;;

export interface KeyboardClearEvent {
  key: &#39;backspace&#39; | &#39;delete&#39;;
}

@Directive({
  selector: &#39;p-calendar[onKeyboardClear]&#39;,
})
export class CalendarOnKeyboardClearDirective implements AfterViewInit {
  @Output()
  readonly onKeyboardClear = new EventEmitter&lt;KeyboardClearEvent&gt;();

  constructor(private readonly calendar: Calendar) {}

  ngAfterViewInit(): void {
    this.calendar.onInput.subscribe((event) =&gt; this.handleInput(event));
  }

  private handleInput(event: InputEvent): void {
    if (!(&lt;HTMLInputElement&gt;event.target).value) {
      switch (event.inputType) {
        case &#39;deleteContentBackward&#39;:
          this.onKeyboardClear.emit({ key: &#39;backspace&#39; });
          break;
        case &#39;deleteContentForward&#39;:
          this.onKeyboardClear.emit({ key: &#39;delete&#39; });
          break;
      }
    }
  }
}

Demo

huangapple
  • 本文由 发表于 2023年6月12日 14:59:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454245.html
匿名

发表评论

匿名网友

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

确定