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

Listbox

Поле списка используется для выбора одного или нескольких значений из списка элементов.

С чего начать

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

import {ListboxModule} from 'primeng/listbox';

В .ts файле

import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { SelectItemGroup } from 'primeng/api';
 
interface City {
  name: string;
  code: string;
}
 
interface Country {
  name: string;
  code: string;
}
 
@Component({
  selector: 'app-prime-listbox',
  templateUrl: './prime-listbox.component.html',
  styleUrls: ['./prime-listbox.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class PrimeListboxComponent implements OnInit {
  groupedCities: SelectItemGroup[];
 
  cities: City[];
 
  countries: Country[];
 
  selectedCity: City;
 
  selectedCountries: Country[];
 
  constructor() {
    this.cities = [
      { name: 'New York', code: 'NY' },
      { name: 'Rome', code: 'RM' },
      { name: 'London', code: 'LDN' },
      { name: 'Istanbul', code: 'IST' },
      { name: 'Paris', code: 'PRS' },
    ];
 
    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' },
    ];
 
    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' },
        ],
      },
    ];
  }
 
  ngOnInit(): void {}
}

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

Single

Advanced with Templating, Filtering and Multiple Selection

country.name

Advanced with Group, Filtering and Multiple Selection

group.label

Свойства

NameTypeDefaultDescription
ariaFilterLabelstringnullDefines a string that labels the filter input.
checkboxbooleanfalseWhen specified, allows selecting items with checkboxes.
dataKeystringnullA property to uniquely identify a value in options.
disabledbooleanfalseWhen present, it specifies that the element should be disabled.
filterbooleanfalseWhen specified, displays a filter input at header.
filterMatchModestringcontainsDefines how the items are filtered, valid values are "contains" (default) "startsWith", "endsWith", "equals", "notEquals", "in", "lt", "lte", "gt" and "gte".
filterValuestringnullWhen specified, filter displays with this value.
filterLocalestringundefinedLocale to use in filtering. The default locale is the host environment's current locale.
filterPlaceHolderstringnullDefines placeholder of the filter input.
emptyFilterMessagestringNo results foundText to display when filtering does not return any results.
listStylestringnullInline style of the list element.
listStyleClassstringnullStyle class 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.
multiplebooleanfalseWhen specified, allows selecting multiple values.
readonlybooleanfalseWhen present, it specifies that the element value cannot be changed.
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.
optionsarraynullAn array of selectitems 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.
groupbooleanfalseWhether to display options as grouped when nested options are provided.
showToggleAllbooleantrueWhether header checkbox is shown in multiple mode.
stylestringnullInline style of the container.
styleClassstringnullStyle class of the container.

События

NameParametersDescription
onChangeevent.originalEvent: browser event<br>event.value: single value or an array of values that are selectedCallback to invoke when value of listbox changes.
onDblClickevent.originalEvent: browser event<br>event.value: single value or an array of values that are selected event.option: option that are clickedCallback to invoke when an item is double clicked.
onClickevent.originalEvent: browser event<br>event.value: single value or an array of values that are selected event.option: option that are clickedCallback to invoke when listbox option clicks.

Шаблоны

NameParameters
item$implicit: Data of the option<br>index: Index of the option
group$implicit: Group option
header-
empty-
emptyfilter-
footer-

Стилизация

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