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.
<p-multiselect [options]="cities" [(ngModel)]="selectedCities" optionLabel="name" placeholder="Select Cities" [maxSelectedLabels]="3" styleClass="w-full md:w-80" />
MultiSelect 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-multiselect [options]="cities" formControlName="selectedCities" optionLabel="name" placeholder="Select Cities" [maxSelectedLabels]="3" styleClass="w-full md:w-80" />
</form>
Selected values are displayed as a comma separated list by default, setting display as chip displays them as chips.
<p-multiselect [options]="cities" [(ngModel)]="selectedCities" placeholder="Select Cities" optionLabel="name" display="chip" styleClass="w-full md:w-80" />
Options can be grouped when a nested data structures is provided.
<p-multiselect [options]="groupedCities" [group]="true" [(ngModel)]="selectedCities" placeholder="Select Cities" scrollHeight="250px" display="chip" styleClass="w-full md:w-80">
<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-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.
<p-multiselect
[options]="countries"
[(ngModel)]="selectedCountries"
placeholder="Select Countries"
optionLabel="name"
styleClass="w-full md:w-80"
display="chip"
>
<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>
<ng-template pTemplate="dropdownicon">
<i class="pi pi-map"></i>
</ng-template>
<ng-template pTemplate="filtericon">
<i class="pi pi-map-marker"></i>
</ng-template>
<ng-template pTemplate="header">
<div class="font-medium px-3 py-2">Available Countries</div>
</ng-template>
<ng-template pTemplate="footer">
<div class="p-3 flex justify-between">
<p-button label="Add New" severity="secondary" text size="small" icon="pi pi-plus" />
<p-button label="Remove All" severity="danger" text size="small" icon="pi pi-times" />
</div>
</ng-template>
</p-multiselect>
MultiSelect provides built-in filtering that is enabled by adding the filter property.
<p-multiselect [options]="cities" [(ngModel)]="selectedCities" [filter]="true" optionLabel="name" placeholder="Select Cities" [maxSelectedLabels]="3" styleClass="w-full md:w-80" />
Loading state can be used loading property.
<p-multiselect [options]="cities" [(ngModel)]="selectedCities" [loading]="true" optionLabel="name" placeholder="Loading..." styleClass="w-full md:w-80" />
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-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)" [maxSelectedLabels]="3" styleClass="w-full md:w-80" #ms>
<ng-template #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.
<p-floatlabel class="w-full md:w-80">
<p-multiselect id="over_label" [(ngModel)]="value1" [options]="cities" optionLabel="name" filter [maxSelectedLabels]="3" styleClass="w-full" />
<label for="over_label">Over Label</label>
</p-floatlabel>
<p-floatlabel class="w-full md:w-80" variant="in">
<p-multiselect id="in_label" [(ngModel)]="value2" [options]="cities" optionLabel="name" filter [maxSelectedLabels]="3" styleClass="w-full" />
<label for="in_label">In Label</label>
</p-floatlabel>
<p-floatlabel class="w-full md:w-80" variant="on">
<p-multiselect id="on_label" [(ngModel)]="value3" [options]="cities" optionLabel="name" filter [maxSelectedLabels]="3" 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-80">
<p-multiselect [(ngModel)]="selectedCities" inputId="ms_cities" [options]="cities" optionLabel="name" [filter]="true" [maxSelectedLabels]="3" styleClass="w-full" />
<label for="ms_cities">Cities</label>
</p-iftalabel>
MultiSelect provides small and large sizes as alternatives to the base.
<p-multiselect [(ngModel)]="value1" [options]="cities" optionLabel="name" [maxSelectedLabels]="3" styleClass="w-full md:w-80" size="small" placeholder="Small" />
<p-multiselect [(ngModel)]="value2" [options]="cities" optionLabel="name" [maxSelectedLabels]="3" styleClass="w-full md:w-80" placeholder="Normal" />
<p-multiselect [(ngModel)]="value3" [options]="cities" optionLabel="name" [maxSelectedLabels]="3" styleClass="w-full md:w-80" size="large" placeholder="Large" />
Specify the variant property as filled to display the component with a higher visual emphasis than the default outlined style.
<p-multiselect [options]="cities" [(ngModel)]="selectedCities" variant="filled" optionLabel="name" placeholder="Select Cities" [maxSelectedLabels]="3" styleClass="w-full md:w-80" />
Invalid state style is added using the ng-invalid and ng-dirty class to indicate a failed validation.
<p-multiselect [options]="cities" [(ngModel)]="selectedCities" class="ng-dirty ng-invalid" optionLabel="name" [maxSelectedLabels]="3" styleClass="w-full md:w-80" />
When disabled is present, the element cannot be edited and focused.
<p-multiselect [options]="cities" [(ngModel)]="selectedCities" [disabled]="true" optionLabel="name" styleClass="w-full md:w-80" />
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 overriden with the closeButtonProps.
<span id="dd1">Options</span>
<p-multiselect ariaLabelledBy="dd1"/>
<p-multiselect ariaLabel="Options"/>
Key | Function |
---|---|
tab | Moves focus to the multiselect 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 first 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 | Toggles the selection state of the focused option. |
space | Toggles the selection state of the focused option. |
escape | Closes the popup, moves focus to the multiselect 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. |
home | Moves focus to the first option. |
end | Moves focus to the last option. |
any printable character | Moves focus to the option whose label starts with the characters being typed if dropdown is not editable. |
Key | Function |
---|---|
space | Toggles the checked state. |
escape | Closes the popup. |
Key | Function |
---|---|
enter | Closes the popup and moves focus to the multiselect element. |
escape | Closes the popup and moves focus to the multiselect element. |
Key | Function |
---|---|
enter | Closes the popup and moves focus to the multiselect element. |
space | Closes the popup and moves focus to the multiselect element. |
escape | Closes the popup and moves focus to the multiselect element. |