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" />
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">
<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.
<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.
<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.
<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.
<p-multiSelect
[options]="cities"
[(ngModel)]="selectedCities"
[filter]="true"
optionLabel="name"
placeholder="Select Cities" />
Loading state can be used loading property.
<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.
<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.
<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.
<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.
<p-multiSelect
[options]="cities"
[(ngModel)]="selectedCities"
class="ng-dirty ng-invalid"
optionLabel="name" />
When disabled is present, the element cannot be edited and focused.
<p-multiSelect
[options]="cities"
[(ngModel)]="selectedCities"
[disabled]="true"
optionLabel="name" />
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-multiselect | Container element. |
p-multiselect-label-container | Container of the label to display selected items. |
p-multiselect-label | Label to display selected items. |
p-multiselect-trigger | Dropdown button. |
p-multiselect-filter-container | Container of filter input. |
p-multiselect-panel | Overlay panel for items. |
p-multiselect-items | List container of items. |
p-multiselect-item | An item in the list. |
p-multiselect-open | Container element when overlay is visible. |
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"/>
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. |
API defines helper props, events and others for the PrimeNG MultiSelect module.
MultiSelect is used to select multiple items from a collection.
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 | |
---|---|---|---|
onChange | event : MultiSelectChangeEvent | Callback to invoke when value changes. | |
onFilter | event : MultiSelectFilterEvent | Callback to invoke when data is filtered. | |
onFocus | event : MultiSelectFocusEvent | Callback to invoke when multiselect receives focus. | |
onBlur | event : MultiSelectBlurEvent | Callback to invoke when multiselect loses focus. | |
onClick | event : Event | Callback to invoke when component is clicked. | |
onClear | value : void | Callback to invoke when input field is cleared. | |
onPanelShow | value : void | Callback to invoke when overlay panel becomes visible. | |
onPanelHide | value : void | Callback to invoke when overlay panel becomes hidden. | |
onLazyLoad | event : MultiSelectLazyLoadEvent | Callback to invoke in lazy mode to load new data. | |
onRemove | event : MultiSelectRemoveEvent | Callback to invoke in lazy mode to load new data. | |
onSelectAllChange | event : MultiSelectSelectAllChangeEvent | Callback to invoke when all data is selected. |
Defines methods that can be accessed by the component's reference.
Defines the templates used by the component.
Defines the custom events used by the component's emitters.
Custom change event.
Custom change event.
Custom filter event.
Custom focus event.
Custom blur event.
Custom lazy load event.
Custom remove event.
Defines the custom interfaces used by the module.
Callbacks to invoke on filter or reset.