Select

Select is used to choose an item from a collection of options.


import { SelectModule } 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" />

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">
    <p-select 
        formControlName="selectedCity" 
        [options]="cities"
        optionLabel="name"
        placeholder="Select a City" />
</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" />

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" />

Options can be grouped when a nested data structures is provided.


<p-select 
    [options]="groupedCities" 
    [(ngModel)]="selectedCity" 
    placeholder="Select a City" 
    [group]="true">
    <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"
    [showClear]="true"
    placeholder="Select a Country">
        <ng-template pTemplate="selectedItem">
            <div class="flex items-center gap-2" *ngIf="selectedCountry">
                <img 
                    src="https://primefaces.org/cdn/primeng/images/demo/flag/flag_placeholder.png"
                    [class]="'flag flag-' + selectedCountry.code.toLowerCase()"
                    style="width: 18px" />
                <div>{{ selectedCountry.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>

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">
        <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>

Custom filter can be applied with the filterTemplate.


<p-select 
    [options]="countries" 
    [(ngModel)]="selectedCountry"
    optionLabel="name"
    [filter]="true" 
    filterBy="name"
    [showClear]="true"
    placeholder="Select a Country" 
    styleClass="w-80">
        <ng-template pTemplate="filter" let-options="options">
            <div class="flex gap-1">
                <div class="p-inputgroup" (click)="$event.stopPropagation()">
                    <span class="p-inputgroup-addon"><i class="pi pi-search"></i></span>
                    <input 
                        type="text"
                        pInputText
                        placeholder="Filter" 
                        [(ngModel)]="filterValue"
                        (keyup)="customFilterFunction($event, options)" />
                </div>
                <button pButton icon="pi pi-times" (click)="resetFunction(options)" severity="secondary"></button>
            </div>
        </ng-template>
        <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" />

Loading state can be used loading property.


<p-select 
    [options]="cities" 
    [(ngModel)]="selectedCity" 
    [loading]="true"
    optionLabel="name" 
    placeholder="Select a City" />

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" />


<p-select 
    [options]="items" 
    [(ngModel)]="selectedItem" 
    placeholder="Select Item" 
    [virtualScroll]="true" 
    [virtualScrollItemSize]="38" 
    [virtualScrollOptions]="options" />

A floating label appears on top of the input field when focused.


<p-floatlabel>
    <p-select 
        [options]="cities"
        [(ngModel)]="selectedCity"
        optionLabel="name" 
        inputId="float-label" />
    <label for="float-label">Select a City</label>
</p-floatlabel>

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" />

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"
/>

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" />

Screen Reader

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"/>

Closed State Keyboard Support

KeyFunction
tabMoves 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.

Popup Keyboard Support

KeyFunction
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.
enterSelects the focused option and closes the popup.
spaceSelects the focused option and closes the popup.
escapeCloses the popup, moves focus to the select element.
down arrowMoves focus to the next option, if there is none then visual focus does not change.
up arrowMoves 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.
homeIf the select is editable, moves input cursor at the end, if not then moves focus to the first option.
endIf the select is editable, moves input cursor at the beginning, if not then moves focus to the last option.
any printable characterMoves focus to the option whose label starts with the characters being typed if select is not editable.

Filter Input Keyboard Support

KeyFunction
enterCloses the popup and moves focus to the select element.
escapeCloses the popup and moves focus to the select element.