Listbox is used to select one or more values from a list of items.
import { ListboxModule } from 'primeng/listbox';
Listbox 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-listbox
[options]="cities"
[(ngModel)]="selectedCity"
optionLabel="name"
[style]="{'width':'15rem'}"
[listStyle]="{'max-height': '220px'}" />
Listbox 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-listbox
[options]="cities"
formControlName="selectedCity"
optionLabel="name"
[style]="{ width: '15rem' }"
[listStyle]="{'max-height': '220px'}" />
</form>
ListBox allows choosing a single item by default, enable multiple property to choose more than one. When the optional metaKeySelection is present, behavior is changed in a way that selecting a new item requires meta key to be present.
<p-listbox
[options]="cities"
[(ngModel)]="selectedCities"
optionLabel="name"
[style]="{'width':'15rem'}"
[multiple]="true"
[metaKeySelection]="false"
[listStyle]="{'max-height': '220px'}" />
Options can be grouped when a nested data structures is provided.
<p-listbox
[options]="groupedCities"
[group]="true"
[(ngModel)]="selectedCountry"
[listStyle]="{ 'max-height': '250px' }"
[style]="{ width: '15rem' }">
<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-listbox>
ListBox provides built-in filtering that is enabled by adding the filter property.
<p-listbox
[options]="cities"
[(ngModel)]="selectedCity"
optionLabel="name"
[filter]="true"
[style]="{ width: '15rem' }"
[listStyle]="{'max-height': '220px'}" />
Custom content for an option is displayed with the pTemplate property that takes an option as a parameter.
<p-listbox
[options]="countries"
[(ngModel)]="selectedCountry"
optionLabel="name"
[listStyle]="{ 'max-height': '250px' }"
[style]="{ width: '15rem' }"
[listStyle]="{'max-height': '220px'}">
<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>
</p-listbox>
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-listbox
[options]="items"
[(ngModel)]="selectedItems"
[selectAll]="selectAll"
optionLabel="label"
[style]="{ width: '15rem' }"
[virtualScroll]="true"
[virtualScrollItemSize]="38"
[multiple]="true"
[metaKeySelection]="false"
(onSelectAllChange)="onSelectAllChange($event)"
(onChange)="onChange($event)"
scrollHeight="250px" />
Invalid state style is added using the ng-invalid and ng-dirty class to indicate a failed validation.
<p-listbox
[options]="cities"
[(ngModel)]="selectedCity"
optionLabel="name"
class="ng-invalid ng-dirty"
[style]="{ width: '15rem' }"
[listStyle]="{'max-height': '220px'}" />
When disabled is present, the element cannot be edited and focused.
<p-listbox
[options]="cities"
[(ngModel)]="selectedCity"
optionLabel="name"
[disabled]="true"
[style]="{ width: '15rem' }"
[listStyle]="{'max-height': '220px'}" />
Following is the list of structural style classes, for theming classes visit theming page.
Name | Element |
---|---|
p-listbox | Container element. |
p-listbox-list | List container. |
p-listbox-item | An item in the list. |
p-listbox-header | Header element. |
p-listbox-filter-container | Container of filter input in header. |
Value to describe the component can be provided ariaLabelledBy or ariaLabel props. The list element has a listbox role with the aria-multiselectable attribute that sets to true when multiple selection is enabled. Each list item has an option role with aria-selected and aria-disabled as their attributes.
<span id="lb">Options</span>
<p-listbox ariaLabelledBy="lb"/>
<p-listbox ariaLabel="City"/>
Key | Function |
---|---|
tab | Moves focus to the first selected option, if there is none then first option receives the focus. |
up arrow | Moves focus to the previous option. |
down arrow | Moves focus to the next option. |
enter | Toggles the selected state of the focused option. |
space | Toggles the selected state of the focused option. |
home | Moves focus to the first option. |
end | Moves focus to the last option. |
shift + down arrow | Moves focus to the next option and toggles the selection state. |
shift + up arrow | Moves focus to the previous option and toggles the selection state. |
shift + space | Selects the items between the most recently selected option and the focused option. |
control + shift + home | Selects the focused options and all the options up to the first one. |
control + shift + end | Selects the focused options and all the options down to the last one. |
control + a | Selects all options. |
API defines helper props, events and others for the PrimeNG Listbox module.
ListBox is used to select one or more values from a list of items.
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 : ListboxChangeEvent | Callback to invoke on value change. | |
onClick | event : ListboxClickEvent | Callback to invoke when option is clicked. | |
onDblClick | event : ListboxDoubleClickEvent | Callback to invoke when option is double clicked. | |
onFilter | event : ListboxFilterEvent | Callback to invoke when data is filtered. | |
onFocus | event : FocusEvent | Callback to invoke when component receives focus. | |
onBlur | event : FocusEvent | Callback to invoke when component loses focus. | |
onSelectAllChange | event : ListboxSelectAllChangeEvent | 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 change event.
Custom change event.
Defines the custom interfaces used by the module.
Filter options of listbox.