MultiSelect

MultiSelect is used to select multiple items from a collection.


import { MultiSelectModule } from 'primeng/multiselect';

MultiSelect 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. Default property name for the optionLabel is label and value for the optionValue. If optionValue is omitted and the object has no value property, the object itself becomes the value of an option. Note that, when options are simple primitive values such as a string array, no optionLabel and optionValue would be necessary.

Select Cities

<p-multiSelect 
    [options]="cities" 
    [(ngModel)]="selectedCities" 
    optionLabel="name" 
    placeholder="Select Cities" />

MultiSelect can also be used with reactive forms. In this case, the formControlName property is used to bind the component to a form control.

Istanbul

<form [formGroup]="formGroup">
    <p-multiSelect 
        [options]="cities" 
        formControlName="selectedCities" 
        optionLabel="name" 
        placeholder="Select Cities"/>
</form>

Selected values are displayed as a comma separated list by default, setting display as chip displays them as chips.

Select Cities

<p-multiSelect 
    [options]="cities" 
    [(ngModel)]="selectedCities" 
    placeholder="Select Cities"
    optionLabel="name" 
    display="chip" 
    [showClear]="true" />

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

Select Cities

<p-multiSelect 
    [options]="groupedCities" 
    [group]="true" 
    [(ngModel)]="selectedCities" 
    placeholder="Select Cities"
    scrollHeight="250px" 
    display="chip">
        <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-multiSelect>

Available options and the selected options support templating with pTemplate properties respectively. In addition, header, footer and filter sections can be templated as well.

Select Countries

<p-multiSelect 
    [options]="countries" 
    [(ngModel)]="selectedCountries" 
    placeholder="Select Countries" 
    optionLabel="name">
        <ng-template let-value pTemplate="selectedItems">
            <div class="inline-flex align-items-center gap-2 px-1" *ngFor="let option of value">
                <img 
                    src="https://primefaces.org/cdn/primeng/images/demo/flag/flag_placeholder.png" 
                    [class]="'flag flag-' + option.code.toLowerCase()" 
                    style="width: 18px" />
                <div>{{ option.name }},</div>
            </div>
            <div *ngIf="!value || value.length === 0">Select Countries</div>
        </ng-template>
        <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>
        <ng-template let-country pTemplate="footer">
            <div class="py-2 px-3">
                <b>
                    {{ selectedCountries ? selectedCountries.length : 0 }}
                </b> 
                item{{ (selectedCountries ? selectedCountries.length : 0) > 1 ? 's' : '' }} selected.
            </div>
        </ng-template>
</p-multiSelect>

MultiSelect provides built-in filtering that is enabled by adding the filter property.

Select Cities

<p-multiSelect 
    [options]="cities" 
    [(ngModel)]="selectedCities" 
    [filter]="true" 
    optionLabel="name" 
    placeholder="Select Cities" />

Loading state can be used loading property.

Loading...

<p-multiSelect 
    [options]="cities" 
    [(ngModel)]="selectedCities" 
    [loading]="true"
    optionLabel="name" 
    placeholder="Loading..." />

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.

Select Cities

<p-multiSelect
    [options]="items"
    [showToggleAll]="true"
    [selectAll]="selectAll"
    [(ngModel)]="selectedItems"
    optionLabel="label"
    [virtualScroll]="true"
    [virtualScrollItemSize]="43"
    class="multiselect-custom-virtual-scroll"
    placeholder="Select Cities"
    (onSelectAllChange)="onSelectAllChange($event)"
    #ms
>
    <ng-template pTemplate="headercheckboxicon" let-allSelected let-partialSelected="partialSelected">
        <i class="pi pi-check" *ngIf="allSelected"></i>
        <i class="pi pi-minus" *ngIf="partialSelected" [ngStyle]="{ color: 'var(--text-color)' }"></i>
    </ng-template>
</p-multiSelect>

A floating label appears on top of the input field when focused. Visit FloatLabel documentation for more information.

empty

<p-floatLabel>
    <p-multiSelect 
        inputId="float-label" 
        [options]="cities" 
        [(ngModel)]="selectedCities" 
        optionLabel="name" />
    <label for="float-label">MultiSelect</label>
</p-floatLabel>

Specify the variant property as filled to display the component with a higher visual emphasis than the default outlined style.

Select Cities

<p-multiSelect 
    [options]="cities" 
    [(ngModel)]="selectedCities" 
    variant="filled"
    optionLabel="name" 
    placeholder="Select Cities" />

Invalid state style is added using the ng-invalid and ng-dirty class to indicate a failed validation.

Select Cities

<p-multiSelect 
        [options]="cities" 
        [(ngModel)]="selectedCities" 
        class="ng-dirty ng-invalid" 
        optionLabel="name" />

When disabled is present, the element cannot be edited and focused.

Select Cities

<p-multiSelect 
        [options]="cities" 
        [(ngModel)]="selectedCities" 
        [disabled]="true" 
        optionLabel="name" />

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
p-multiselectContainer element.
p-multiselect-label-containerContainer of the label to display selected items.
p-multiselect-labelLabel to display selected items.
p-multiselect-triggerDropdown button.
p-multiselect-filter-containerContainer of filter input.
p-multiselect-panelOverlay panel for items.
p-multiselect-itemsList container of items.
p-multiselect-itemAn item in the list.
p-multiselect-openContainer element when overlay is visible.

Screen Reader

Value to describe the component can either be provided with ariaLabelledBy or ariaLabel props. The multiselect component has a combobox role in addition to aria-haspopup and aria-expanded attributes. The relation between the combobox and the popup is created with aria-controls attribute that refers to the id of the popup listbox.

The popup listbox uses listbox as the role with aria-multiselectable enabled. Each list item has an option role along with aria-label, aria-selected and aria-disabled attributes.

Checkbox component at the header uses a hidden native checkbox element internally that is only visible to screen readers. Value to read is defined with the selectAll and unselectAll keys of the aria property from the locale API.

If filtering is enabled, filterInputProps can be defined to give aria-* props to the input element.

Close button uses close key of the aria property from the locale API as the aria-label by default, this can be overridden with the closeButtonProps.


<span id="dd1">Options</span>
<p-multiSelect ariaLabelledBy="dd1"/>

<p-multiSelect ariaLabel="Options"/>

Closed State Keyboard Support

KeyFunction
tabMoves focus to the multiselect element.
spaceOpens the popup and moves visual focus to the selected option, if there is none then first option receives the focus.
down arrowOpens the popup and moves visual focus to the selected option, if there is none then first option receives the focus.
up arrowOpens the popup and moves visual focus to the selected option, if there is none then first option receives the focus.

Popup Keyboard Support

KeyFunction
tabMoves focus to the next focusable element in the popup, if there is none then first focusable element receives the focus.
shift + tabMoves focus to the previous focusable element in the popup, if there is none then last focusable element receives the focus.
enterToggles the selection state of the focused option.
spaceToggles the selection state of the focused option.
escapeCloses the popup, moves focus to the multiselect 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.
homeMoves focus to the first option.
endMoves focus to the last option.
any printable characterMoves focus to the option whose label starts with the characters being typed if dropdown is not editable.

Toggle All Checkbox Keyboard Support

KeyFunction
spaceToggles the checked state.
escapeCloses the popup.

Filter Input Keyboard Support

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

Close Button Keyboard Support

KeyFunction
enterCloses the popup and moves focus to the multiselect element.
spaceCloses the popup and moves focus to the multiselect element.
escapeCloses the popup and moves focus to the multiselect element.