Selection and events

Selection is enabled by setting selectionMode to one of possible values: single or multiple. In the single mode, a single TreeNode is expected as the value of the selection property. In the multiple mode, an array is expected. For example:

<p-organizationChart [value]="data"
selectionMode="single" [(selection)]="selectedNode">
</p-organizationChart>

The organization chart supports two events:

Name Parameters Description
onNodeSelect
  • event.originalEvent: Browser event
  • event.node: Selected node instance
Callback invoked when a node is selected by a click.
onNodeUnselect
  • event.originalEvent: Browser event
  • event.node: Unselected node instance
Callback invoked when a node is unselected by a click.

 

Let's extend the previous developed organization chart as shown here:

<p-organizationChart [value]="data" styleClass="company"
selectionMode="single" [(selection)]="selectedNode"
(onNodeSelect)="onNodeSelect($event)">
...
</p-organizationChart>

In the demo application on GitHub, we defined a VCard interface representing a person's VCard:

export interface VCard {
id: string;
fullName: string;
birthday: string;
address: string;
email: string;
}

All VCard instances are lazily fetched in the onNodeSelect callback. After that, a VCard to the clicked person (node) is shown in the PrimeNG dialog:

display: boolean = false;
selectedVCard
: VCard;
private availableVCards: VCard[];

onNodeSelect(event: any) {
if (this.availableVCards == null) {
this.vcardService.getVCards().subscribe(
(vcards: VCard[]) => {
this.availableVCards = vcards;
this.showInfo(event);
});
} else {
this.showInfo(event);
}
}

private showInfo(event: any) {
this.selectedVCard = null;

this.availableVCards.some((element: VCard) => {
if (event.node.data && element.id === event.node.data.id) {
this.selectedVCard = element;
return true;
}
});

if (this.selectedVCard) {
// show VCard in dialog
this.display = true;
} else {
// show node label in growl
this.msgs = [];
this.msgs.push({severity: 'Label', summary: event.node.label});
}
}

The dialog itself looks like the following:

<p-dialog header="VCard" [(visible)]="display"
modal="modal" width="320" [responsive]="true">
<i class="fa fa-address-card-o"></i>
<ul style="padding: 0.2em 0.8em;">
<li>Full name: {{selectedVCard?.fullName}}</li>
<li>Birthday: {{selectedVCard?.birthday}}</li>
<li>Address: {{selectedVCard?.address}}</li>
<li>E-mail: {{selectedVCard?.email}}</li>
</ul>
</p-dialog>

The result is truly amazing:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.218.226