TreeSelect
PrimeNG/TreeSelect (opens in a new tab)
Tree Select - это компонент формы для выбора из иерархических данных.
С чего начать
Подключите модуль
import { TreeSelectModule } from 'primeng/treeselect';
В .ts файле
import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
@Component({
selector: 'app-prime-treeselect',
templateUrl: './prime-treeselect.component.html',
styleUrls: ['./prime-treeselect.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class PrimeTreeselectComponent implements OnInit {
constructor() {}
nodes1: any[] = _nodes1;
nodes2: any[] = _nodes2;
nodes3: any[] = _nodes3;
nodes4: any[] = _nodes4;
selectedNodes1: any[] = [];
selectedNodes2: any[] = [];
selectedNodes3: any[] = [];
selectedNode: any;
ngOnInit(): void {}
}
const _nodes1 = [
{
label: 'Documents',
data: 'Documents Folder',
expandedIcon: 'pi pi-folder-open',
collapsedIcon: 'pi pi-folder',
children: [
{
label: 'Work',
data: 'Work Folder',
expandedIcon: 'pi pi-folder-open',
collapsedIcon: 'pi pi-folder',
children: [
{ label: 'Expenses.doc', icon: 'pi pi-file', data: 'Expenses Document' },
{ label: 'Resume.doc', icon: 'pi pi-file', data: 'Resume Document' },
],
},
{
label: 'Home',
data: 'Home Folder',
expandedIcon: 'pi pi-folder-open',
collapsedIcon: 'pi pi-folder',
children: [{ label: 'Invoices.txt', icon: 'pi pi-file', data: 'Invoices for this month' }],
},
],
},
{
label: 'Pictures',
data: 'Pictures Folder',
expandedIcon: 'pi pi-folder-open',
collapsedIcon: 'pi pi-folder',
children: [
{ label: 'barcelona.jpg', icon: 'pi pi-image', data: 'Barcelona Photo' },
{ label: 'logo.jpg', icon: 'pi pi-image', data: 'PrimeFaces Logo' },
{ label: 'primeui.png', icon: 'pi pi-image', data: 'PrimeUI Logo' },
],
},
{
label: 'Movies',
data: 'Movies Folder',
expandedIcon: 'pi pi-folder-open',
collapsedIcon: 'pi pi-folder',
children: [
{
label: 'Al Pacino',
data: 'Pacino Movies',
children: [
{ label: 'Scarface', icon: 'pi pi-video', data: 'Scarface Movie' },
{ label: 'Serpico', icon: 'pi pi-video', data: 'Serpico Movie' },
],
},
{
label: 'Robert De Niro',
data: 'De Niro Movies',
children: [
{ label: 'Goodfellas', icon: 'pi pi-video', data: 'Goodfellas Movie' },
{ label: 'Untouchables', icon: 'pi pi-video', data: 'Untouchables Movie' },
],
},
],
},
];
const _nodes2 = _nodes1;
const _nodes3 = _nodes1;
const _nodes4 = _nodes1;
Встройте компонент с помощью тэга p-treeSelect.
<p-treeSelect [(ngModel)]="selectedNode" [options]="nodes1" placeholder="Select Item"></p-treeSelect>
Selection Mode
Tree Selects предлагает варианты "single", "multiple" и "checkbox" для поведения выбора, которое определяется параметром SelectionMode.
<p-treeSelect [(ngModel)]="selectedValue1" [options]="nodes" selectionMode="single" placeholder="Select Item"></p-treeSelect>
<p-treeSelect [(ngModel)]="selectedValue2" selectionMode="multiple" [options]="nodes" placeholder="Select Items"></p-treeSelect>
<p-treeSelect [(ngModel)]="selectedValue3" selectionMode="checkbox" [options]="nodes" placeholder="Select Items"></p-treeSelect>
Chips Display
Список, разделенный запятыми, используется по умолчанию для отображения выбранных элементов, тогда как альтернативный режим chip предоставляется с использованием свойства display для визуализации элементов в виде токенов.
<p-treeSelect [(ngModel)]="selectedValue" selectionMode="multiple" display="chip" [options]="nodes" placeholder="Select Items"></p-treeSelect>
Single
<p-treeSelect [(ngModel)]="selectedNode" [options]="nodes1" placeholder="Select Item"></p-treeSelect>
Multiple
<p-treeSelect
[(ngModel)]="selectedNodes1"
[options]="nodes2"
[metaKeySelection]="false"
selectionMode="multiple"
placeholder="Select Item"
></p-treeSelect>
Checkbox
<p-treeSelect
[(ngModel)]="selectedNodes2"
[options]="nodes3"
display="chip"
[metaKeySelection]="false"
selectionMode="checkbox"
placeholder="Select Item"
></p-treeSelect>
Float Label
Basic
<span class="p-float-label">
<p-treeSelect [(ngModel)]="selectedNodes3" [options]="nodes4" [metaKeySelection]="false" selectionMode="multiple"></p-treeSelect>
<label for="basic">Basic</label>
</span>
TreeNode API
TreeNode API utilized by the TreeSelect
Name | Type | Default | Description |
---|---|---|---|
label | string | null | Label of the node. |
data | any | null | Data represented by the node. |
icon | string | null | Icon of the node to display next to content. |
expandedIcon | string | null | Icon to use in expanded state. |
collapsedIcon | string | null | Icon to use in collapsed state. |
children | TreeNode[] | null | An array of treenodes as children. |
leaf | boolean | null | Specifies if the node has children. Used in lazy loading. |
style | string | null | Inline style of the node. |
styleClass | string | null | Style class of the node. |
expanded | boolean | null | Whether the node is in an expanded or collapsed state. |
type | string | null | Type of the node to match ng-template type. |
parent | TreeNode | null | Parent of the node. |
draggable | boolean | null | Whether to disable dragging for a particular node even if draggableNodes is enabled. |
droppable | boolean | null | Whether to disable dropping for a particular node even if droppableNodes is enabled. |
selectable | boolean | null | Used to disable selection of a particular node. |
key | string | null | Unique key of the node.(<a href="https://github.com/primefaces/primeng-lts/issues/7237#issue-409791407 (opens in a new tab)" target="_blank" rel="noopener noreferrer">more</a>) |
Свойства
Name | Type | Default | Description |
---|---|---|---|
selection | any | null | Value of the component. |
options | array | null | An array of treenodes. |
scrollHeight | string | 400px | Height of the viewport, a scrollbar is defined if height of list exceeds this value. |
placeholder | string | null | Label to display when there are no selections. |
disabled | boolean | false | When present, it specifies that the component should be disabled. |
tabindex | string | null | Index of the element in tabbing order. |
inputId | string | null | Identifier of the underlying input element. |
ariaLabelledBy | string | null | Establishes relationships between the component and label(s) where its value should be one or more element IDs. |
selectionMode | string | null | Defines the selection mode, valid values "single", "multiple", and "checkbox". |
panelClass | string | null | Style class of the overlay panel. |
appendTo | string | body | A valid query selector or an HTMLElement to specify where the overlay gets attached. Special keywords are "body" for document body and "self" for the element itself. |
emptyMessage | string | No results found | Text to display when there are no options available. Defaults to value from PrimeVue locale configuration. |
display | string | comma | Defines how the selected items are displayed, valid values are "comma" and "chip". |
propagateSelectionUp | boolean | true | Whether checkbox selections propagate to ancestor nodes. |
propagateSelectionDown | boolean | true | Whether checkbox selections propagate to descendant nodes. |
metaKeySelection | boolean | true | Defines 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. |
События
Name | Parameters | Description |
---|---|---|
onShow | - | Callback to invoke when the overlay is shown. |
onHide | - | Callback to invoke when the overlay is hidden. |
onNodeSelect | node: Node instance | Callback to invoke when a node is selected. |
onNodeUnselect | node: Node instance | Callback to invoke when a node is unselected. |
onNodeExpand | node: Node instance | Callback to invoke when a node is expanded. |
onNodeCollapse | node: Node instance | Callback to invoke when a node is collapsed. |
Шаблоны
Name | Parameters |
---|---|
value | $implicit: Value of the component |
header | $implicit: Value of the component<br>options: TreeNode options |
footer | $implicit: Value of the component<br>options: TreeNode options |
empty | - |
Стилизация
Name | Element |
---|---|
p-treeselect | Container element. |
p-treeselect-label-container | Container of the label to display selected items. |
p-treeselect-label | Label to display selected items. |
p-treeselect-trigger | Dropdown button. |
p-treeselect-panel | Overlay panel for items. |
p-treeselect-items-wrapper | List container of items. |