AutoComplete is an input component that provides real-time suggestions when being typed.
import { AutoCompleteModule } from 'primeng/autocomplete';
AutoComplete uses ngModel for two-way binding, requires a list of suggestions and a completeMethod to query for the results. The completeMethod gets the query text as event.query property and should update the suggestions with the search results.
<p-autoComplete
[(ngModel)]="selectedItem"
[suggestions]="suggestions"
(completeMethod)="search($event)" />
AutoComplete can also be used with reactive forms. In this case, the formControlName property is used to bind the component to a form control.
<form [formGroup]="formGroup">
<p-autoComplete
formControlName="selectedCountry"
[suggestions]="filteredCountries"
(completeMethod)="filterCountry($event)"
optionLabel="name" />
</form>
Enabling dropdown property displays a button next to the input field where click behavior of the button is defined using dropdownMode property that takes blank or current as possible values. blank is the default mode to send a query with an empty string whereas current setting sends a query with the current value of the input.
<p-autoComplete
[(ngModel)]="selectedCountry"
[dropdown]="true"
[suggestions]="filteredCountries"
(completeMethod)="filterCountry($event)"
optionLabel="name" />
AutoComplete can also work with objects using the optionLabel property that defines the label to display as a suggestion. The value passed to the model would still be the object instance of a suggestion. Here is an example with a Country object that has name and code fields such as {name: "United States", code:"USA"}.
<p-autoComplete
[(ngModel)]="selectedCountry"
[suggestions]="filteredCountries"
(completeMethod)="filterCountry($event)"
optionLabel="name" />
item template allows displaying custom content inside the suggestions panel. The local ng-template variable passed to the ng-template is an object in the suggestions array.
<p-autoComplete
[(ngModel)]="selectedCountryAdvanced"
[suggestions]="filteredCountries"
(completeMethod)="filterCountry($event)"
optionLabel="name">
<ng-template let-country pTemplate="item">
<div class="flex align-items-center gap-2">
<img
src="https://primefaces.org/cdn/primeng/images/demo/flag/flag_placeholder.png"
[class]="'flag flag-' + country.code.toLowerCase()"
style="width: 18px" />
<div>{{ country.name }}</div>
</div>
</ng-template>
</p-autoComplete>
Option grouping is enabled when group property is set to true. group template is available to customize the option groups. All templates get the option instance as the default local template variable.
<p-autoComplete
[(ngModel)]="selectedCity"
[group]="true"
[suggestions]="filteredGroups"
(completeMethod)="filterGroupedCity($event)"
placeholder="Hint: type 'a'">
<ng-template let-group pTemplate="group">
<div class="flex align-items-center">
<img
src="https://primefaces.org/cdn/primeng/images/demo/flag/flag_placeholder.png"
[class]="'mr-2 flag flag-' + group.value"
style="width: 20px" />
<span>{{ group.label }}</span>
</div>
</ng-template>
</p-autoComplete>
ForceSelection mode validates the manual input to check whether it also exists in the suggestions list, if not the input value is cleared to make sure the value passed to the model is always one of the suggestions.
<p-autoComplete
[(ngModel)]="selectedCountry"
[forceSelection]="true"
[suggestions]="filteredCountries"
(completeMethod)="filterCountry($event)"
optionLabel="name" />
Virtual scrolling is an efficient way of rendering the options by displaying a small subset of data in the viewport at any time. When dealing with huge number of options, it is suggested to enable virtual scrolling to avoid performance issues. Usage is simple as setting virtualScroll property to true and defining virtualScrollItemSize to specify the height of an item.
<p-autoComplete
[(ngModel)]="selectedItem"
[virtualScroll]="true"
[suggestions]="filteredItems"
[virtualScrollItemSize]="34"
(completeMethod)="filterItems($event)"
optionLabel="label"
[dropdown]="true" />
Multiple mode is enabled using multiple property used to select more than one value from the autocomplete. In this case, value reference should be an array.
<span class="p-fluid">
<p-autoComplete
[(ngModel)]="selectedItems"
[suggestions]="items"
(completeMethod)="search($event)"
[multiple]="true" />
</span>
A floating label appears on top of the input field when focused. Visit FloatLabel documentation for more information.
<p-floatLabel>
<p-autoComplete
[(ngModel)]="selectedItem"
[suggestions]="suggestions"
(completeMethod)="search($event)"
inputId="float-label" />
<label for="float-label">Float Label</label>
</p-floatLabel>
Specify the variant property as filled to display the component with a higher visual emphasis than the default outlined style.
<p-autoComplete
[(ngModel)]="selectedItem"
[suggestions]="suggestions"
(completeMethod)="search($event)"
variant="filled" />
When disabled is present, the element cannot be edited and focused.
<p-autoComplete
[(ngModel)]="selectedItem"
[suggestions]="suggestions"
(completeMethod)="search($event)"
[disabled]="true" />
When showClear is enabled, a clear icon is added to reset the Autocomplete.
<p-autoComplete
formControlName="country"
[dropdown]="true"
[showClear]="true"
[suggestions]="filteredCountries"
(completeMethod)="filterCountry($event)"
optionLabel="name" />
Invalid state style is added using the ng-invalid and ng-dirty class to indicate a failed validation.
<p-autoComplete
class="ng-invalid ng-dirty"
[(ngModel)]="selectedItem"
[suggestions]="suggestions"
(completeMethod)="search($event)" />
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-autocomplete | Container element |
p-autocomplete-panel | Overlay panel of suggestions. |
p-autocomplete-items | List container of suggestions. |
p-autocomplete-item | List item of a suggestion. |
p-autocomplete-token | Element of a selected item in multiple mode. |
p-autocomplete-token-icon | Close icon element of a selected item in multiple mode. |
p-autocomplete-token-label | Label of a selected item in multiple mode. |
p-autocomplete-loader | Loader icon. |
Value to describe the component can either be provided via label tag combined with inputId prop or using ariaLabelledBy, ariaLabel props. The input element has combobox role in addition to aria-autocomplete, aria-haspopup and aria-expanded attributes. The relation between the input and the popup is created with aria-controls and aria-activedescendant attribute is used to instruct screen reader which option to read during keyboard navigation within the popup list.
In multiple mode, chip list uses listbox role whereas each chip has the option role with aria-label set to the label of the chip.
The popup list has an id that refers to the aria-controls attribute of the input element and uses listbox as the role. Each list item has option role and an id to match the aria-activedescendant of the input element.
<label for="ac1">Username</label>
<p-autoComplete inputId="ac1"/>
<span id="ac2">Email</span>
<p-autoComplete ariaLabelledBy="ac2" />
<p-autoComplete ariaLabel="City" />
Key | Function |
---|---|
tab | Moves focus to the input element when popup is not visible. If the popup is open and an item is highlighted then popup gets closed, item gets selected and focus moves to the next focusable element. |
up arrow | Highlights the previous item if popup is visible. |
down arrow | Highlights the next item if popup is visible. |
enter | Selects the highlighted item and closes the popup if popup is visible. |
home | Highlights the first item if popup is visible. |
end | Highlights the last item if popup is visible. |
escape | Hides the popup. |
Key | Function |
---|---|
backspace | Deletes the previous chip if the input field is empty. |
left arrow | Moves focus to the previous chip if available and input field is empty. |
Key | Function |
---|---|
left arrow | Moves focus to the previous chip if available. |
right arrow | Moves focus to the next chip, if there is none then input field receives the focus. |
backspace | Deletes the chips and adds focus to the input field. |
API defines helper props, events and others for the PrimeNG AutoComplete module.
AutoComplete is an input component that provides real-time suggestions when being typed.
Defines the input properties of the component.
Defines emit that determine the behavior of the component based on a given condition or report the actions that the component takes.
name | parameters | description | |
---|---|---|---|
completeMethod | event : AutoCompleteCompleteEvent | Callback to invoke to search for suggestions. | |
onSelect | event : AutoCompleteSelectEvent | Callback to invoke when a suggestion is selected. | |
onUnselect | event : AutoCompleteUnselectEvent | Callback to invoke when a selected value is removed. | |
onFocus | event : Event | Callback to invoke when the component receives focus. | |
onBlur | event : Event | Callback to invoke when the component loses focus. | |
onDropdownClick | event : AutoCompleteDropdownClickEvent | Callback to invoke to when dropdown button is clicked. | |
onClear | event : Event | Callback to invoke when clear button is clicked. | |
onKeyUp | event : KeyboardEvent | Callback to invoke on input key up. | |
onShow | event : Event | Callback to invoke on overlay is shown. | |
onHide | event : Event | Callback to invoke on overlay is hidden. | |
onLazyLoad | event : AutoCompleteLazyLoadEvent | Callback to invoke on lazy load data. |
Defines the templates used by the component.
Defines the custom events used by the component's emitters.
Custom complete event.
Custom click event.
Custom select event.
Custom unselect event.
Custom lazy load event.