Listbox

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.

  • New York
  • Rome
  • London
  • Istanbul
  • Paris

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

  • New York
  • Rome
  • London
  • Istanbul
  • Paris

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

  • New York
  • Rome
  • London
  • Istanbul
  • Paris

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

  • Germany
  • Berlin
  • Frankfurt
  • Hamburg
  • Munich
  • USA
  • Chicago
  • Los Angeles
  • New York
  • San Francisco
  • Japan
  • Kyoto
  • Osaka
  • Tokyo
  • Yokohama

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

5 results are available
  • New York
  • Rome
  • London
  • Istanbul
  • Paris

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

  • Australia
  • Brazil
  • China
  • Egypt
  • France
  • Germany
  • India
  • Japan
  • Spain
  • United States

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

  • New York
  • Rome
  • London
  • Istanbul
  • Paris

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

  • New York
  • Rome
  • London
  • Istanbul
  • Paris

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

NameElement
p-listboxContainer element.
p-listbox-listList container.
p-listbox-itemAn item in the list.
p-listbox-headerHeader element.
p-listbox-filter-containerContainer of filter input in header.

Screen Reader

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

Keyboard Support

KeyFunction
tabMoves focus to the first selected option, if there is none then first option receives the focus.
up arrowMoves focus to the previous option.
down arrowMoves focus to the next option.
enterToggles the selected state of the focused option.
spaceToggles the selected state of the focused option.
homeMoves focus to the first option.
endMoves focus to the last option.
shift + down arrowMoves focus to the next option and toggles the selection state.
shift + up arrowMoves focus to the previous option and toggles the selection state.
shift + spaceSelects the items between the most recently selected option and the focused option.
control + shift + homeSelects the focused options and all the options up to the first one.
control + shift + endSelects the focused options and all the options down to the last one.
control + aSelects all options.