DropdownDropdown is used to select an item from a collection of options. Custom content support and filtering are the notable features.

Dropdown - 图1

Documentation

CDK

VirtualScrolling enabled Dropdown depends on @angular/cdk's ScrollingModule so begin with installing CDK if not already installed.

  1. npm install @angular/cdk --save
  2.  

Import

  1. import {DropdownModule} from 'primeng/dropdown';
  2.  

Getting Started

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

Model Driven Forms

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

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

Filtering

Options can be filtered using an input field in the overlay by enabling the filter property. By default filtering is done against label of the SelectItem and filterBy property is available to choose one or more properties of the SelectItem API.

  1. <p-dropdown [options]="cities" [(ngModel)]="selectedCity" [filter]="true"></p-dropdown>
  2. <p-dropdown [options]="cities" [(ngModel)]="selectedCity" [filter]="true" filterBy="label,value.name"></p-dropdown>
  3.  

Grouping

Displaying options as grouped is enabled by providing a collection of SelectItemGroup instances and setting group property to true.

  1. <p-dropdown [options]="groupedCars" [(ngModel)]="selectedCar" placeholder="Select a Car" [group]="true"></p-dropdown>
  2.  
  1. import {SelectItem} from 'primeng/api';
  2. import {SelectItemGroup} from 'primeng/api';
  3. export class GroupDemo {
  4. selectedCar: string;
  5. groupedCars: SelectItemGroup[];
  6. constructor() {
  7. this.groupedCars = [
  8. {
  9. label: 'Germany',
  10. items: [
  11. {label: 'Audi', value: 'Audi'},
  12. {label: 'BMW', value: 'BMW'},
  13. {label: 'Mercedes', value: 'Mercedes'}
  14. ]
  15. },
  16. {
  17. label: 'USA',
  18. items: [
  19. {label: 'Cadillac', value: 'Cadillac'},
  20. {label: 'Ford', value: 'Ford'},
  21. {label: 'GMC', value: 'GMC'}
  22. ]
  23. },
  24. {
  25. label: 'Japan',
  26. items: [
  27. {label: 'Honda', value: 'Honda'},
  28. {label: 'Mazda', value: 'Mazda'},
  29. {label: 'Toyota', value: 'Toyota'}
  30. ]
  31. }
  32. ];
  33. }
  34. }
  35.  

Disabled Options

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

Custom Content

Both the selected option and the options list can be templated to provide customization on the default behavior which is displaying label property of an option. Use "selectedItem" template to customize the selected label display and the "item" template to change the content of the options in the dropdown panel. In addition when grouping is enabled, "group" template is available to customize the option groups. All templates get the option instance as the default local template variable. If the options are custom objects not SelectItem instances, use value property to access the custom object.

  1. <p-dropdown [options]="cars" [(ngModel)]="selectedCar" [style]="{'width':'150px'}">
  2. <ng-template let-item pTemplate="selectedItem">
  3. <img src="assets/showcase/images/demo/car/{{item.label}}.png" style="width:16px;vertical-align:middle" />
  4. <span style="vertical-align:middle">{{item.label}}</span>
  5. </ng-template>
  6. <ng-template let-car pTemplate="item">
  7. <div class="ui-helper-clearfix" style="position: relative;height:25px;">
  8. <img src="assets/showcase/images/demo/car/{{car.label}}.png" style="width:24px;position:absolute;top:1px;left:5px"/>
  9. <div style="font-size:14px;float:right;margin-top:4px">{{car.label}}</div> </div>
  10. </ng-template>
  11. </p-dropdown>
  12.  
  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.  

Virtual Scrolling

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 itemSize to specify the height of an item.

  1. <p-dropdown [options]="cities" [virtualScroll]="true" itemSize="30"></p-dropdown>
  2.  

Animation Configuration

Transition of the open and hide animations can be customized using the showTransitionOptions and hideTransitionOptions properties, example below disables the animations altogether.

  1. <p-dropdown [options]="cars" [(ngModel)]="selectedCar" [showTransitionOptions]="'0ms'" [hideTransitionOptions]="'0ms'"></p-dropdown>
  2.  

Properties

NameTypeDefaultDescription
optionsarraynullAn array of objects 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.
namestringnullName of the input element.
scrollHeightstring200pxHeight of the viewport in pixels, a scrollbar is defined if height of list exceeds this value.
stylestringnullInline style of the element.
panelStylestringnullInline style of the overlay panel element.
styleClassstringnullStyle class of the element.
panelStyleClassstringnullStyle class of the overlay panel element.
filterbooleanfalseWhen specified, displays an input field to filter the items on keyup.
filterBystringlabelWhen filtering is enabled, filterBy decides which field or fields (comma separated) to search against.
filterPlaceholderstringnullPlaceholder text to show when filter input is empty.
requiredbooleanfalseWhen present, it specifies that an input field must be filled out before submitting the form.
disabledbooleanfalseWhen present, it specifies that the component should be disabled.
readonlybooleanfalseWhen present, it specifies that the component cannot be edited.
editablebooleanfalseWhen present, custom value instead of predefined options can be entered using the editable input field.
appendToanynullTarget element to attach the overlay, valid values are "body" or a local ng-template variable of another element.
tabindexnumbernullIndex of the element in tabbing order.
placeholderstringnullDefault text to display when no option is selected.
inputIdstringnullIdentifier of the accessible input element.
selectIdstringnullIdentifier of the native element.
dataKeystringnullA property to uniquely identify a value in options.
autofocusbooleanfalseWhen present, it specifies that the component should automatically get focus on load.
resetFilterOnHidebooleanfalseClears the filter value when hiding the dropdown.
dropdownIconstringpi pi-chevron-downIcon class of the dropdown icon.
emptyFilterMessagestringNo results foundText to display when filtering does not return any results.
autoDisplayFirstbooleantrueWhether to display the first item as the label if no placeholder is defined and value is null.
groupbooleanfalseWhether to display options as grouped when nested options are provided.
showClearbooleanfalseWhen enabled, a clear icon is displayed to clear the value.
baseZIndexnumber0Base zIndex value to use in layering.
autoZIndexbooleantrueWhether to automatically manage layering.
showTransitionOptionsstring225ms ease-outTransition options of the show animation.
hideTransitionOptionsstring195ms ease-inTransition options of the hide animation.
ariaFilterLabelstringnullDefines a string that labels the filter input.

Events

NameParametersDescription
onClickevent: Click eventCallback to invoke when component is clicked.
onChangeevent.originalEvent: Browser event event.value: Selected option value Callback to invoke when value of dropdown changes.
onFocusevent: Browser eventCallback to invoke when dropdown gets focus.
onBlurevent: Browser eventCallback to invoke when dropdown loses focus.
onShowevent: Animation eventCallback to invoke when dropdown overlay gets visible.
onHideevent: Animation eventCallback to invoke when dropdown overlay gets hidden.

Methods

NameParametersDescription
resetFilter-Resets filtering.
focus-Applies focus.
  1. <p-dropdown #dd [options]="cars" [(ngModel)]="selectedCar" filter="true"></p-dropdown>
  2. <button type="button" pButton label="Clear" (click)="clearFilter(dd)"></button>
  3.  
  1. clearFilter(dropdown: Dropdown) {
  2. dropdown.resetFilter();
  3. }
  4.  

Styling

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

NameElement
ui-dropdownContainer element.
ui-dropdown-clearableContainer element when showClear is on.
ui-dropdown-labelElement to display label of selected option.
ui-dropdown-triggerIcon element.
ui-dropdown-panelIcon element.
ui-dropdown-items-wrapperWrapper element of items list.
ui-dropdown-itemsList element of items.
ui-dropdown-itemAn item in the list.
ui-dropdown-filter-containerContainer of filter input.
ui-dropdown-filterFilter element.
ui-dropdown-openContainer element when overlay is visible.

Dependencies

None.

Source

View on GitHub

  1. <h3 class="first">Single</h3>
  2. <p-dropdown [options]="cities" [(ngModel)]="selectedCity" placeholder="Select a City" optionLabel="name" [showClear]="true"></p-dropdown>
  3. <p>Selected City: {{selectedCity ? selectedCity.name : 'none'}}</p>
  4. <h3>Editable</h3>
  5. <p-dropdown [options]="cars" [(ngModel)]="selectedCar1" editable="true" placeholder="Select a Brand"></p-dropdown>
  6. <p>Selected Car: {{selectedCar1 || 'none'}}</p>
  7. <h3>Content with Filters</h3>
  8. <p-dropdown [options]="cars" [(ngModel)]="selectedCar2" filter="true">
  9. <ng-template let-item pTemplate="selectedItem">
  10. <img src="assets/showcase/images/demo/car/{{item.label}}.png" style="width:16px;vertical-align:middle" />
  11. <span style="vertical-align:middle; margin-left: .5em">{{item.label}}</span>
  12. </ng-template>
  13. <ng-template let-car pTemplate="item">
  14. <div class="ui-helper-clearfix" style="position: relative;height: 25px;">
  15. <img src="assets/showcase/images/demo/car/{{car.label}}.png" style="width:24px;position:absolute;top:1px;left:5px"/>
  16. <div style="font-size:14px;float:right;margin-top:4px">{{car.label}}</div>
  17. </div>
  18. </ng-template>
  19. </p-dropdown>
  20. <p>Selected Car: {{selectedCar2 || 'none'}}</p>
  21. <h3>Group</h3>
  22. <p-dropdown [options]="groupedCars" [(ngModel)]="selectedCar3" placeholder="Select a Car" [group]="true">
  23. <ng-template let-group pTemplate="group">
  24. <img src="assets/showcase/images/demo/flag/{{group.value}}" style="width:20px;vertical-align:middle" />
  25. <span style="margin-left:.25em">{{group.label}}</span>
  26. </ng-template>
  27. </p-dropdown>
  28. <p>Selected Car: {{selectedCar3 || 'none'}}</p>
  29. <h3>Virtual Scroll (10000 Items)</h3>
  30. <p-dropdown [options]="items" [(ngModel)]="item" placeholder="Select Item" [virtualScroll]="true" [itemSize]="31" [filter]="false"></p-dropdown>
  31.  
  1. export class DropdownDemo {
  2. cities: City[];
  3. selectedCity: City;
  4. cars: SelectItem[];
  5. selectedCar1: string;
  6. selectedCar2: string = 'BMW';
  7. selectedCar3: string;
  8. groupedCars: SelectItemGroup[];
  9. items: SelectItem[];
  10. item: string;
  11. constructor() {
  12. this.cities = [
  13. {name: 'New York', code: 'NY'},
  14. {name: 'Rome', code: 'RM'},
  15. {name: 'London', code: 'LDN'},
  16. {name: 'Istanbul', code: 'IST'},
  17. {name: 'Paris', code: 'PRS'}
  18. ];
  19. this.cars = [
  20. {label: 'Audi', value: 'Audi'},
  21. {label: 'BMW', value: 'BMW'},
  22. {label: 'Fiat', value: 'Fiat'},
  23. {label: 'Ford', value: 'Ford'},
  24. {label: 'Honda', value: 'Honda'},
  25. {label: 'Jaguar', value: 'Jaguar'},
  26. {label: 'Mercedes', value: 'Mercedes'},
  27. {label: 'Renault', value: 'Renault'},
  28. {label: 'VW', value: 'VW'},
  29. {label: 'Volvo', value: 'Volvo'}
  30. ];
  31. this.groupedCars = [
  32. {
  33. label: 'Germany', value: 'germany.png',
  34. items: [
  35. {label: 'Audi', value: 'Audi'},
  36. {label: 'BMW', value: 'BMW'},
  37. {label: 'Mercedes', value: 'Mercedes'}
  38. ]
  39. },
  40. {
  41. label: 'USA', value: 'usa.png',
  42. items: [
  43. {label: 'Cadillac', value: 'Cadillac'},
  44. {label: 'Ford', value: 'Ford'},
  45. {label: 'GMC', value: 'GMC'}
  46. ]
  47. },
  48. {
  49. label: 'Japan', value: 'japan.png',
  50. items: [
  51. {label: 'Honda', value: 'Honda'},
  52. {label: 'Mazda', value: 'Mazda'},
  53. {label: 'Toyota', value: 'Toyota'}
  54. ]
  55. }
  56. ];
  57. this.items = [];
  58. for (let i = 0; i < 10000; i++) {
  59. this.items.push({label: 'Item ' + i, value: 'Item ' + i});
  60. }
  61. }
  62. }
  63.