ListboxListbox is used to select one or more values from a list of items.

Listbox - 图1

Documentation

Import

  1. import {ListboxModule} from 'primeng/listbox';
  2.  

Getting Started

Listbox requires a value to bind and a collection of options. There are two alternatives of how to define the options property; one way is providing a collection of SelectItem instances whereas other way is providing an array of arbitrary objects along with the optionLabel property to specify the field name of the option. SelectItem API is designed to have more control on how the options are displayed such as grouping and disabling however in most cases an arbitrary object collection will suffice. Example below demonstrates both cases.

  1. <p-listbox [options]="cities1" [(ngModel)]="selectedCity1"></p-listbox>
  2. <p-listbox [options]="cities2" [(ngModel)]="selectedCity2" optionLabel="name"></p-listbox>
  3.  
  1. import {SelectItem} from 'primeng/api';
  2. export class MyModel {
  3. cities1: SelectItem[];
  4. cities2: City[];
  5. selectedCity1: City;
  6. selectedCity2: City;
  7. constructor() {
  8. //SelectItem API with label-value pairs
  9. this.cities1 = [
  10. {label:'Select City', value:null},
  11. {label:'New York', value:{id:1, name: 'New York', code: 'NY'}},
  12. {label:'Rome', value:{id:2, name: 'Rome', code: 'RM'}},
  13. {label:'London', value:{id:3, name: 'London', code: 'LDN'}},
  14. {label:'Istanbul', value:{id:4, name: 'Istanbul', code: 'IST'}},
  15. {label:'Paris', value:{id:5, name: 'Paris', code: 'PRS'}}
  16. ];
  17. //An array of cities
  18. this.cities2 = [
  19. {name: 'New York', code: 'NY'},
  20. {name: 'Rome', code: 'RM'},
  21. {name: 'London', code: 'LDN'},
  22. {name: 'Istanbul', code: 'IST'},
  23. {name: 'Paris', code: 'PRS'}
  24. ];
  25. }
  26. }
  27.  

Selection

Listbox allows selection of either single or multiple items whereas checkbox option displays a checkbox to indicate multiple selection. In single case, model should be a single object reference whereas in multiple case should be an array. Multiple items can either be selected using metaKey or toggled individually depending on the value of metaKeySelection property value which is true by default. On touch enabled devices metaKeySelection is turned off automatically.

  1. <p-listbox [options]="cities" [(ngModel)]="selectedCities"></p-listbox>
  2.  
  1. import {SelectItem} from 'primeng/api';
  2. export class MyModel {
  3. cities: SelectItem[];
  4. selectedCities: string[];
  5. constructor() {
  6. this.cities = [
  7. {label:'Select City', value:null},
  8. {label:'New York', value:{id:1, name: 'New York', code: 'NY'}},
  9. {label:'Rome', value:{id:2, name: 'Rome', code: 'RM'}},
  10. {label:'London', value:{id:3, name: 'London', code: 'LDN'}},
  11. {label:'Istanbul', value:{id:4, name: 'Istanbul', code: 'IST'}}
  12. {label:'Paris', value:{id:5, name: 'Paris', code: 'PRS'}}
  13. ];
  14. }
  15. }
  16.  

Updating Options

When itemLabel property is used, Listbox does not detect changes in the options so provide a new reference when you need to update the options such as adding a new option.

  1. addOption() {
  2. //fails
  3. this.options.push({name:'New York', code: 'NY'});
  4. //correct
  5. this.options = [...this.cities, {name:'New York', code: 'NY'}];
  6. }
  7.  

Disabled Options

Particular options can be prevented from selection using the disabled property of SelectItem API.

Filter

Filtering allows searching items in the list using an input field at the header. In order to use filtering, enable filter property. Default filtering mode is "contains" and alternative is "startsWith" configuted by filterMode property.

  1. <p-listbox [options]="cities" [(ngModel)]="selectedCities" filter="filter"></p-listbox>
  2.  

Model Driven Forms

Listbox can be used in a model driven form as well.

  1. <p-listbox [options]="cities" formControlName="cities"></p-listbox>
  2.  

Custom Content

Label of an option is used as the display text of an item by default, for custom content support define a ng-template where the local ng-template variable refers to an option in the options collection. Additionally a custom header and footer can be provided using p-header and p-footer element.

  1. <p-listbox [options]="cars" [(ngModel)]="selectedCar">
  2. <p-header>
  3. <i class="fa fa-car"></i>
  4. List of Car
  5. </p-header>
  6. <ng-template let-car let-i="index" pTemplate="item">
  7. <div class="ui-helper-clearfix">
  8. <img src="assets/showcase/images/demo/car/{{car.label}}.png" style="display:inline-block;margin:5px 0 0 5px" width="48">
  9. <span style="float:right;margin:20px 10px 0 0">{{i}} - {{car.value}}</span>
  10. </div>
  11. </ng-template>
  12. <p-footer>
  13. Selected: {{selectedCar||'none'}}
  14. </p-footer>
  15. </p-listbox>
  16.  
  1. import {SelectItem} from 'primeng/api';
  2. export class MyModel {
  3. cars: SelectItem[];
  4. selectedCar: string;
  5. constructor() {
  6. this.cars = [
  7. {label: 'Audi', value: 'Audi'},
  8. {label: 'BMW', value: 'BMW'},
  9. {label: 'Fiat', value: 'Fiat'},
  10. {label: 'Ford', value: 'Ford'},
  11. {label: 'Honda', value: 'Honda'},
  12. {label: 'Jaguar', value: 'Jaguar'},
  13. {label: 'Mercedes', value: 'Mercedes'},
  14. {label: 'Renault', value: 'Renault'},
  15. {label: 'VW', value: 'VW'},
  16. {label: 'Volvo', value: 'Volvo'}
  17. ];
  18. }
  19. }
  20.  

Properties

NameTypeDefaultDescription
optionsarraynullAn array of selectitems to display as the available options.
optionLabelstringnullName of the label field of an option when an arbitrary objects instead of SelectItems are used as options.
multiplebooleanfalseWhen specified, allows selecting multiple values.
checkboxbooleanfalseWhen specified, allows selecting items with checkboxes.
filterbooleanfalseWhen specified, displays a filter input at header.
filterModestringcontainsDefines how the items are filtered, valid values are "contains" (default) and "startsWith".
filterValuestringnullWhen specified, filter displays with this value.
readonlybooleanfalseWhen present, it specifies that the element value cannot be changed.
disabledbooleanfalseWhen present, it specifies that the element should be disabled.
stylestringnullInline style of the container.
styleClassstringnullStyle class of the container.
listStylestringnullInline style of the list element.
metaKeySelectionbooleantrueDefines how multiple items can be selected, when true metaKey needs to be pressed to select or unselect an item and when set to false selection of each item can be toggled individually. On touch enabled devices, metaKeySelection is turned off automatically.
dataKeystringnullA property to uniquely identify a value in options.
showToggleAllbooleantrueWhether header checkbox is shown in multiple mode.
ariaFilterLabelstringnullDefines a string that labels the filter input.

Events

NameParametersDescription
onChangeevent.originalEvent: browser event event.value: single value or an array of values that are selected Callback to invoke when value of listbox changes.
onDblClickevent.originalEvent: browser event event.value: Clicked selecte item Callback to invoke when an item is double clicked.
onClickevent.originalEvent: browser event event.value: single value that are clicked Callback to invoke when listbox option clicks.

Styling

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

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

Dependencies

None.

Source

View on GitHub

  1. <h3 class="first">Basic</h3>
  2. <p-listbox [options]="cities" [(ngModel)]="selectedCity"}" optionLabel="name"></p-listbox>
  3. <p>Selected City: {{selectedCity ? selectedCity.name : 'none'}}</p>
  4. <h3>Advanced (Multiple, Checkbox and Filter)</h3>
  5. <p-listbox [options]="cities" [(ngModel)]="selectedCities" multiple="multiple" checkbox="checkbox" filter="filter" optionLabel="name">
  6. <p-header>
  7. <i class="fa fa-car"></i>
  8. Cars
  9. </p-header>
  10. </p-listbox>
  11. <p>Selected Cities: <span *ngFor="let c of selectedCities" style="margin-right: 10px">{{c.name}}</span></p>
  12. <h3>Content</h3>
  13. <p-listbox [options]="cars" [(ngModel)]="selectedCar" [listStyle]="{'max-height':'250px'}">
  14. <ng-template let-car pTemplate="item">
  15. <div class="ui-helper-clearfix">
  16. <img src="assets/showcase/images/demo/car/{{car.label}}.png" style="display:inline-block;margin:5px 0 0 5px" width="48">
  17. <span style="float:right;margin:20px 10px 0 0">{{car.value}}</span>
  18. </div>
  19. </ng-template>
  20. <p-footer>
  21. Selected: {{selectedCar||'none'}}
  22. </p-footer>
  23. </p-listbox>
  24. <button type="button" (click)="selectedCar=null" pButton icon="pi pi-times" label="Clear Selected Car"></button>
  25.  
  1. export class ListboxDemo {
  2. cities: City[];
  3. selectedCity: City;
  4. selectedCities: City[];
  5. cars: SelectItem[];
  6. selectedCar: string = 'BMW';
  7. constructor() {
  8. this.cities = [
  9. {name: 'New York', code: 'NY'},
  10. {name: 'Rome', code: 'RM'},
  11. {name: 'London', code: 'LDN'},
  12. {name: 'Istanbul', code: 'IST'},
  13. {name: 'Paris', code: 'PRS'}
  14. ];
  15. this.cars = [
  16. {label: 'Audi', value: 'Audi'},
  17. {label: 'BMW', value: 'BMW'},
  18. {label: 'Fiat', value: 'Fiat'},
  19. {label: 'Ford', value: 'Ford'},
  20. {label: 'Honda', value: 'Honda'},
  21. {label: 'Jaguar', value: 'Jaguar'},
  22. {label: 'Mercedes', value: 'Mercedes'},
  23. {label: 'Renault', value: 'Renault'},
  24. {label: 'VW', value: 'VW'},
  25. {label: 'Volvo', value: 'Volvo'}
  26. ];
  27. }
  28. }
  29.