Выпадающий список
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
Свойства
Name | Type | Default | Description |
---|---|---|---|
options | array | null | An array of objects to display as the available options. |
optionLabel | string | label | Name of the label field of an option. |
optionValue | string | value | Name of the value field of an option. |
optionDisabled | string | disabled | Name of the disabled field of an option. |
optionGroupLabel | string | label | Name of the label field of an option group. |
optionGroupChildren | string | items | Name of the options field of an option group. |
name | string | null | Name of the input element. |
scrollHeight | string | 200px | Height of the viewport in pixels, a scrollbar is defined if height of list exceeds this value. |
style | string | null | Inline style of the element. |
panelStyle | string | null | Inline style of the overlay panel element. |
styleClass | string | null | Style class of the element. |
panelStyleClass | string | null | Style class of the overlay panel element. |
filter | boolean | false | When specified, displays an input field to filter the items on keyup. |
filterValue | string | null | When specified, filter displays with this value. |
filterBy | string | null | When filtering is enabled, filterBy decides which field or fields (comma separated) to search against. |
filterMatchMode | string | contains | Defines how the items are filtered, valid values are "contains" (default) "startsWith", "endsWith", "equals", "notEquals", "in", "lt", "lte", "gt" and "gte". |
filterPlaceholder | string | null | Placeholder text to show when filter input is empty. |
filterLocale | string | undefined | Locale to use in filtering. The default locale is the host environment's current locale. |
required | boolean | false | When present, it specifies that an input field must be filled out before submitting the form. |
disabled | boolean | false | When present, it specifies that the component should be disabled. |
readonly | boolean | false | When present, it specifies that the component cannot be edited. |
emptyMessage | string | No records found. | Text to display when there is no data. Defaults to global value in i18n translation configuration. |
emptyFilterMessage | string | No results found | Text to display when filtering does not return any results. Defaults to global value in i18n translation configuration. |
ariaLabelledBy | string | null | Establishes relationships between the component and label(s) where its value should be one or more element IDs. |
editable | boolean | false | When present, custom value instead of predefined options can be entered using the editable input field. |
maxlength | number | null | Maximum number of character allows in the editable input field. |
appendTo | any | null | Target 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). |
tabindex | number | null | Index of the element in tabbing order. |
placeholder | string | null | Default text to display when no option is selected. |
inputId | string | null | Identifier of the accessible input element. |
dataKey | string | null | A property to uniquely identify a value in options. |
autofocus | boolean | false | When present, it specifies that the component should automatically get focus on load. |
autofocusFilter | boolean | false | Applies focus to the filter element when the overlay is shown. |
resetFilterOnHide | boolean | false | Clears the filter value when hiding the dropdown. |
dropdownIcon | string | pi pi-chevron-down | Icon class of the dropdown icon. |
emptyFilterMessage | string | No results found | Text to display when filtering does not return any results. |
autoDisplayFirst | boolean | true | Whether to display the first item as the label if no placeholder is defined and value is null. |
group | boolean | false | Whether to display options as grouped when nested options are provided. |
showClear | boolean | false | When enabled, a clear icon is displayed to clear the value. |
baseZIndex | number | 0 | Base zIndex value to use in layering. |
autoZIndex | boolean | true | Whether to automatically manage layering. |
showTransitionOptions | string | .12s cubic-bezier(0, 0, 0.2, 1) | Transition options of the show animation. |
hideTransitionOptions | string | .1s linear | Transition options of the hide animation. |
ariaFilterLabel | string | null | Defines a string that labels the filter input. |
tooltip | any | null | Advisory information to display in a tooltip on hover. |
tooltipStyleClass | string | null | Style class of the tooltip. |
tooltipPosition | string | top | Position of the tooltip, valid values are right, left, top and bottom. |
tooltipPositionStyle | string | absolute | Type of CSS position. |
События
Name | Parameters | Description |
---|---|---|
onClick | event: Click event | Callback to invoke when component is clicked. |
onChange | event.originalEvent: Browser event<br>event.value: Selected option value | Callback to invoke when value of dropdown changes. |
onFilter | event.originalEvent: Browser event<br>event.filter: Filter value used in filtering. | Callback to invoke when data is filtered. |
onFocus | event: Browser event | Callback to invoke when dropdown gets focus. |
onBlur | event: Browser event | Callback to invoke when dropdown loses focus. |
onShow | event: Animation event | Callback to invoke when dropdown overlay gets visible. |
onHide | event: Animation event | Callback to invoke when dropdown overlay gets hidden. |
onClear | event: Animation event | Callback to invoke when dropdown clears the value. |
Методы
Name | Parameters | Description |
---|---|---|
resetFilter | - | Resets filtering. |
focus | - | Applies focus. |
show | - | Displays the panel. |
hide | - | Hides the panel. |
Шаблоны
Name | Parameters |
---|---|
item | $implicit: Data of the option |
group | $implicit: Group option |
selectedItem | $implicit: value |
header | - |
empty | - |
emptyfilter | - |
footer | - |
Стилизация
Name | Element |
---|---|
p-dropdown | Container element. |
p-dropdown-clearable | Container element when showClear is on. |
p-dropdown-label | Element to display label of selected option. |
p-dropdown-trigger | Icon element. |
p-dropdown-panel | Icon element. |
p-dropdown-items-wrapper | Wrapper element of items list. |
p-dropdown-items | List element of items. |
p-dropdown-item | An item in the list. |
p-dropdown-filter-container | Container of filter input. |
p-dropdown-filter | Filter element. |
p-dropdown-open | Container element when overlay is visible. |