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.  

    Updating Options

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

    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

    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.  

    Events

    Styling

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

    Dependencies

    None.

    Source

    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>Selected Cities: <span *ngFor="let c of selectedCities" style="margin-right: 10px">{{c.name}}</span></p>
    11. <h3>Content</h3>
    12. <p-listbox [options]="cars" [(ngModel)]="selectedCar" [listStyle]="{'max-height':'250px'}">
    13. <div class="ui-helper-clearfix">
    14. <img src="assets/showcase/images/demo/car/{{car.label}}.png" style="display:inline-block;margin:5px 0 0 5px" width="48">
    15. <span style="float:right;margin:20px 10px 0 0">{{car.value}}</span>
    16. </div>
    17. </ng-template>
    18. <p-footer>
    19. Selected: {{selectedCar||'none'}}
    20. </p-footer>
    21. </p-listbox>
    22. <button type="button" (click)="selectedCar=null" pButton icon="pi pi-times" label="Clear Selected Car"></button>
    23.  
    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.