Select is used to choose an item from a collection of options.
import { Select } from 'primeng/select';
Select is used as a controlled component with ngModel property along with an options collection. Label and value of an option are defined with the optionLabel and optionValue properties respectively. Note that, when options are simple primitive values such as a string array, no optionLabel and optionValue would be necessary.
<p-select [options]="cities" [(ngModel)]="selectedCity" optionLabel="name" placeholder="Select a City" class="w-full md:w-56" />
Select 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" class="card flex justify-center">
<p-select formControlName="selectedCity" [options]="cities" optionLabel="name" placeholder="Select a City" class="w-full md:w-56" />
</form>
An alternative way to highlight the selected option is displaying a checkmark instead.
<p-select [options]="cities" [(ngModel)]="selectedCity" [checkmark]="true" optionLabel="name" [showClear]="true" placeholder="Select a City" class="w-full md:w-56" />
When editable is present, the input can also be entered with typing.
<p-select [options]="cities" [(ngModel)]="selectedCity" placeholder="Select a City" [editable]="true" optionLabel="name" class="w-full md:w-56" />
Options can be grouped when a nested data structures is provided.
<p-select [options]="groupedCities" [(ngModel)]="selectedCity" placeholder="Select a City" [group]="true" class="w-full md:w-56">
<ng-template let-group pTemplate="group">
<div class="flex 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-select>
Both the selected option and the options list can be templated to provide customizated representation. Use selectedItem template to customize the selected label display and the item template to change the content of the options in the select panel. In addition when grouping is enabled, group template is available to customize the option groups. All templates get the option instance as the default local template variable.
<p-select [options]="countries" [(ngModel)]="selectedCountry" optionLabel="name" placeholder="Select a country" class="w-full md:w-56">
<ng-template #selectedItem let-selectedOption>
<div class="flex items-center gap-2" *ngIf="selectedOption">
<img
src="https://primefaces.org/cdn/primeng/images/demo/flag/flag_placeholder.png"
[class]="'flag flag-' + selectedOption.code.toLowerCase()"
style="width: 18px"
/>
<div>{{ selectedOption.name }}</div>
</div>
</ng-template>
<ng-template let-country #item>
<div class="flex 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>
<ng-template #dropdownicon>
<i class="pi pi-map"></i>
</ng-template>
<ng-template #header>
<div class="font-medium p-3">Available Countries</div>
</ng-template>
<ng-template #footer>
<div class="p-3">
<p-button label="Add New" fluid severity="secondary" text size="small" icon="pi pi-plus" />
</div>
</ng-template>
</p-select>
Select provides built-in filtering that is enabled by adding the filter property.
<p-select [options]="countries" [(ngModel)]="selectedCountry" optionLabel="name" [filter]="true" filterBy="name" [showClear]="true" placeholder="Select a Country" class="w-full md:w-56">
<ng-template pTemplate="selectedItem" let-selectedOption>
<div class="flex items-center gap-2">
<img src="https://primefaces.org/cdn/primeng/images/demo/flag/flag_placeholder.png" [class]="'flag flag-' + selectedCountry.code.toLowerCase()" style="width: 18px" />
<div>{{ selectedOption.name }}</div>
</div>
</ng-template>
<ng-template let-country pTemplate="item">
<div class="flex 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-select>
When showClear is enabled, a clear icon is added to reset the Select.
<p-select [options]="cities" [(ngModel)]="selectedCity" optionLabel="name" [showClear]="true" placeholder="Select a City" class="w-full md:w-56" />
Loading state can be used loading property.
<p-select [options]="cities" [(ngModel)]="selectedCity" [loading]="true" optionLabel="name" placeholder="Select a City" class="w-full md:w-56" />
VirtualScrolling 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 VirtualScrolling to avoid performance issues. Usage is simple as setting virtualScroll property to true and defining virtualScrollItemSize to specify the height of an item.
<p-select [options]="items" [(ngModel)]="selectedItem" placeholder="Select Item" [virtualScroll]="true" [virtualScrollItemSize]="38" class="w-full md:w-56" />
<p-select [options]="items" [(ngModel)]="selectedItem" placeholder="Select Item" [virtualScroll]="true" [virtualScrollItemSize]="38" [virtualScrollOptions]="options" class="w-full md:w-56" />
Specify the variant property as filled to display the component with a higher visual emphasis than the default outlined style.
<p-select [options]="cities" [(ngModel)]="selectedCity" variant="filled" optionLabel="name" placeholder="Select a City" class="w-full md:w-56" />
A floating label appears on top of the input field when focused. Visit FloatLabel documentation for more information.
<p-floatlabel class="w-full md:w-56">
<p-select [(ngModel)]="value1" inputId="over_label" [options]="cities" optionLabel="name" styleClass="w-full" />
<label for="over_label">Over Label</label>
</p-floatlabel>
<p-floatlabel class="w-full md:w-56" variant="in">
<p-select [(ngModel)]="value2" inputId="in_label" [options]="cities" optionLabel="name" styleClass="w-full" />
<label for="in_label">In Label</label>
</p-floatlabel>
<p-floatlabel class="w-full md:w-56" variant="on">
<p-select [(ngModel)]="value3" inputId="on_label" [options]="cities" optionLabel="name" styleClass="w-full" />
<label for="on_label">On Label</label>
</p-floatlabel>
IftaLabel is used to create infield top aligned labels. Visit IftaLabel documentation for more information.
<p-iftalabel class="w-full md:w-56">
<p-select [(ngModel)]="selectedCity" inputId="dd-city" [options]="cities" optionLabel="name" styleClass="w-full" />
<label for="dd-city">City</label>
</p-iftalabel>
Select provides small and large sizes as alternatives to the base.
<p-select [(ngModel)]="value1" [options]="cities" optionLabel="name" size="small" placeholder="Small" class="w-full md:w-56" />
<p-select [(ngModel)]="value2" [options]="cities" optionLabel="name" placeholder="Normal" class="w-full md:w-56" />
<p-select [(ngModel)]="value3" [options]="cities" optionLabel="name" size="large" placeholder="Large" class="w-full md:w-56" />
Invalid state style is added using the ng-invalid and ng-dirty class to indicate a failed validation.
<p-select [options]="cities" [(ngModel)]="selectedCity" optionLabel="name" [showClear]="true" placeholder="Select a City" class="ng-dirty ng-invalid w-full md:w-56" />
When disabled is present, the element cannot be edited and focused.
<p-select [options]="cities" [(ngModel)]="selectedCity" placeholder="Select a City" optionLabel="name" [disabled]="true" class="w-full md:w-56" />
Value to describe the component can either be provided with ariaLabelledBy or ariaLabel props. The select element has a combobox role in addition to aria-haspopup and aria-expanded attributes. If the editable option is enabled aria-autocomplete is also added. The relation between the combobox 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.
The popup list has an id that refers to the aria-controls attribute of the combobox element and uses listbox as the role. Each list item has an option role, an id to match the aria-activedescendant of the input element along with aria-label, aria-selected and aria-disabled attributes.
If filtering is enabled, filterInputProps can be defined to give aria-* props to the filter input element.
<span id="dd1">Options</span>
<p-select ariaLabelledBy="dd1"/>
<p-select ariaLabel="Options"/>
Key | Function |
---|---|
tab | Moves focus to the select element. |
space | Opens the popup and moves visual focus to the selected option, if there is none then first option receives the focus. |
down arrow | Opens the popup and moves visual focus to the selected option, if there is none then first option receives the focus. |
up arrow | Opens the popup and moves visual focus to the selected option, if there is none then last option receives the focus. |
Key | Function |
---|---|
tab | Moves focus to the next focusable element in the popup, if there is none then first focusable element receives the focus. |
shift + tab | Moves focus to the previous focusable element in the popup, if there is none then last focusable element receives the focus. |
enter | Selects the focused option and closes the popup. |
space | Selects the focused option and closes the popup. |
escape | Closes the popup, moves focus to the select element. |
down arrow | Moves focus to the next option, if there is none then visual focus does not change. |
up arrow | Moves focus to the previous option, if there is none then visual focus does not change. |
right arrow | If the select is editable, removes the visual focus from the current option and moves input cursor to one character left. |
left arrow | If the select is editable, removes the visual focus from the current option and moves input cursor to one character right. |
home | If the select is editable, moves input cursor at the end, if not then moves focus to the first option. |
end | If the select is editable, moves input cursor at the beginning, if not then moves focus to the last option. |
any printable character | Moves focus to the option whose label starts with the characters being typed if select is not editable. |
Key | Function |
---|---|
enter | Closes the popup and moves focus to the select element. |
escape | Closes the popup and moves focus to the select element. |