简体   繁体   English

按下回车后清除 p-autocomplete primeng

[英]Clearing p-autocomplete primeng after hitting enter

Using PrimeNG auto-complete, I have the following html.使用 PrimeNG 自动完成,我有以下 html。

<p-autoComplete
    id="autocomplete"
    [(ngModel)]="termToSearch"
    [minLength]=1
    [suggestions]="suggestedData"
    (completeMethod)="search($event)"
    placeholder="Search by name..."
    (onSelect)="goToDetailPage($event)"
    (onClear)="clearInput()"
    (keydown.enter)="onSearchSubmit($event)"
    [forceSelection]=false>
 </p-autocomplete>

when the user hits the 'enter' key instead of selecting one of the options in the dropdown, I have a onSearchSubmit function.当用户点击“输入”键而不是选择下拉列表中的选项之一时,我有一个onSearchSubmit function。

That function looks like this: function 看起来像这样:

clearInput() {
    this.termToSearch = null;
  }

  clearSuggestions() {
    this.suggestedData = null;
  }

 onSearchSubmit(event) {
    this.clearSuggestions();
    this.clearInput();
    // before we do a general search for whatever was entered, hide the suggestions dropdown
    this.router.navigate(["/search/results"], {queryParams: { q: event.target.value }});
}

For the most part, this works, yet at the same time it doesn't.在大多数情况下,这行得通,但同时也行不通。 It clears the suggestion list, but it doesn't hide the entire suggestion dropdown, so the UI ends up looking like this:它会清除建议列表,但不会隐藏整个建议下拉列表,因此 UI 最终看起来像这样:

在此处输入图像描述

  1. Whenever need to clear the suggestion list use this每当需要清除建议列表时使用这个

    this.suggestedData = []

  2. To close the suggestion dropdown manually use primeNg autocomplete component .hide()要手动关闭建议下拉菜单,请使用 primeNg 自动完成组件 .hide ()

    AutoComplete.hide()

Implementation In HTML HTML 中的实现

<p-autoComplete
    id="autocomplete"
    #autocomplete
    [(ngModel)]="termToSearch"
    [minLength]=1
    [suggestions]="suggestedData"
    (completeMethod)="search($event)"
    placeholder="Search by name..."
    (onSelect)="goToDetailPage($event)"
    (onClear)="clearInput()"
    (keydown.enter)="autocomplete.hide();onSearchSubmit($event)"
    [forceSelection]=false>
 </p-autocomplete>

In its TS add import { AutoComplete } from 'primeng/autocomplete';在其 TS 中添加import { AutoComplete } from 'primeng/autocomplete';

@ViewChild('autocomplete') autocomplete:AutoComplete;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM