Компоненты PrimeNG
DropDown

Выпадающий список

PrimeNG/Dropdown (opens in a new tab)

Выпадающий список используется для выбора элемента из списка опций.

С чего начать

Подключите модуль

import { DropdownModule } from 'primeng/dropdown';

В .ts файле

import {AfterViewInit, Component, OnInit} from '@angular/core';
import {SelectItem, SelectItemGroup} from 'primeng/api';
 
interface City {
  name: string;
  code: string;
}
 
@Component({
  selector: 'app-prime-dropdown',
  templateUrl: './prime-dropdown.component.html',
  styleUrls: ['./prime-dropdown.component.scss']
})
export class PrimeDropdownComponent implements OnInit, AfterViewInit {
 
  cities: City[] = [];
  selectedCity1?: City;
  selectedCity2?: City;
  selectedCity3: any;
  selectedCountry: any;
  countries: any[] = [];
  groupedCities: SelectItemGroup[] = [];
  items: SelectItem[];
  item = '';
 
  constructor() {
    this.items = [];
    for (let i = 0; i < 10000; i++) {
      this.items.push({ label: 'Item ' + i, value: 'Item ' + i });
    }
  }
 
  ngOnInit(): void {
 
  }
 
  ngAfterViewInit() {
    this.cities = [
      { name: 'New York', code: 'NY' },
      { name: 'Rome', code: 'RM' },
      { name: 'London', code: 'LDN' },
      { name: 'Istanbul', code: 'IST' },
      { name: 'Paris', code: 'PRS' }
    ];
 
    this.groupedCities = [
      {
        label: 'Germany', value: 'de',
        items: [
          { label: 'Berlin', value: 'Berlin' },
          { label: 'Frankfurt', value: 'Frankfurt' },
          { label: 'Hamburg', value: 'Hamburg' },
          { label: 'Munich', value: 'Munich' }
        ]
      },
      {
        label: 'USA', value: 'us',
        items: [
          { label: 'Chicago', value: 'Chicago' },
          { label: 'Los Angeles', value: 'Los Angeles' },
          { label: 'New York', value: 'New York' },
          { label: 'San Francisco', value: 'San Francisco' }
        ]
      },
      {
        label: 'Japan', value: 'jp',
        items: [
          { label: 'Kyoto', value: 'Kyoto' },
          { label: 'Osaka', value: 'Osaka' },
          { label: 'Tokyo', value: 'Tokyo' },
          { label: 'Yokohama', value: 'Yokohama' }
        ]
      }
    ];
 
    this.countries = [
      { name: 'Australia', code: 'AU' },
      { name: 'Brazil', code: 'BR' },
      { name: 'China', code: 'CN' },
      { name: 'Egypt', code: 'EG' },
      { name: 'France', code: 'FR' },
      { name: 'Germany', code: 'DE' },
      { name: 'India', code: 'IN' },
      { name: 'Japan', code: 'JP' },
      { name: 'Spain', code: 'ES' },
      { name: 'United States', code: 'US' }
    ];
  }
}

Встройте компонент с помощью тэга p-dropdown.

Single

Editable

Group

group.label

Content with Filters

selectedCountry.name country.name

Virtual Scroll (10000 Items)

Размеры

Float Label

Select something

Свойства

NameTypeDefaultDescription
optionsarraynullAn array of objects to display as the available options.
optionLabelstringlabelName of the label field of an option.
optionValuestringvalueName of the value field of an option.
optionDisabledstringdisabledName of the disabled field of an option.
optionGroupLabelstringlabelName of the label field of an option group.
optionGroupChildrenstringitemsName of the options field of an option group.
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.
filterValuestringnullWhen specified, filter displays with this value.
filterBystringnullWhen filtering is enabled, filterBy decides which field or fields (comma separated) to search against.
filterMatchModestringcontainsDefines how the items are filtered, valid values are "contains" (default) "startsWith", "endsWith", "equals", "notEquals", "in", "lt", "lte", "gt" and "gte".
filterPlaceholderstringnullPlaceholder text to show when filter input is empty.
filterLocalestringundefinedLocale to use in filtering. The default locale is the host environment's current locale.
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.
emptyMessagestringNo records found.Text to display when there is no data. Defaults to global value in i18n translation configuration.
emptyFilterMessagestringNo results foundText to display when filtering does not return any results. Defaults to global value in i18n translation configuration.
ariaLabelledBystringnullEstablishes relationships between the component and label(s) where its value should be one or more element IDs.
editablebooleanfalseWhen present, custom value instead of predefined options can be entered using the editable input field.
maxlengthnumbernullMaximum number of character allows in the editable input field.
appendToanynullTarget element to attach the overlay, valid values are "body" or a local ng-template variable of another element (note: use binding with brackets for template variables, e.g. [appendTo]="mydiv" for a div element having #mydiv as variable name).
tabindexnumbernullIndex of the element in tabbing order.
placeholderstringnullDefault text to display when no option is selected.
inputIdstringnullIdentifier of the accessible input element.
dataKeystringnullA property to uniquely identify a value in options.
autofocusbooleanfalseWhen present, it specifies that the component should automatically get focus on load.
autofocusFilterbooleanfalseApplies focus to the filter element when the overlay is shown.
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.
showTransitionOptionsstring.12s cubic-bezier(0, 0, 0.2, 1)Transition options of the show animation.
hideTransitionOptionsstring.1s linearTransition options of the hide animation.
ariaFilterLabelstringnullDefines a string that labels the filter input.
tooltipanynullAdvisory information to display in a tooltip on hover.
tooltipStyleClassstringnullStyle class of the tooltip.
tooltipPositionstringtopPosition of the tooltip, valid values are right, left, top and bottom.
tooltipPositionStylestringabsoluteType of CSS position.

События

NameParametersDescription
onClickevent: Click eventCallback to invoke when component is clicked.
onChangeevent.originalEvent: Browser event<br>event.value: Selected option valueCallback to invoke when value of dropdown changes.
onFilterevent.originalEvent: Browser event<br>event.filter: Filter value used in filtering.Callback to invoke when data is filtered.
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.
onClearevent: Animation eventCallback to invoke when dropdown clears the value.

Методы

NameParametersDescription
resetFilter-Resets filtering.
focus-Applies focus.
show-Displays the panel.
hide-Hides the panel.

Шаблоны

NameParameters
item$implicit: Data of the option
group$implicit: Group option
selectedItem$implicit: value
header-
empty-
emptyfilter-
footer-

Стилизация

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