OrganizationChartOrganizationChart visualized hierarchical organization data.

Org Chart - 图1

Documentation

Import

  1. import {OrganizationChartModule} from 'primeng/organizationchart';
  2.  

Getting Started

OrganizationChart requires a model of TreeNode as its value. More information about TreeNode API is available at documentation of tree component.

  1. import {TreeNode} from 'primeng/api';
  2.  
  1. <p-organizationChart [value]="data"></p-organizationChart>
  2.  
  1. export class MyComponents implements OnInit {
  2. data: TreeNode[];
  3. ngOnInit() {
  4. this.data = [{
  5. label: 'Root',
  6. children: [
  7. {
  8. label: 'Child 1',
  9. children: [
  10. {
  11. label: 'Grandchild 1.1'
  12. },
  13. {
  14. label: 'Grandchild 1.2'
  15. }
  16. ]
  17. },
  18. {
  19. label: 'Child 2',
  20. children: [
  21. {
  22. label: 'Child 2.1'
  23. },
  24. {
  25. label: 'Child 2.2'
  26. }
  27. ]
  28. }
  29. ]
  30. }];
  31. }
  32.  

Templating

Label of the treenode is displayed inside the node content by default and templating enables enhanced customization. TreeNode API has type property which is used to match the pTemplate type so templating can be done per node as well. In example below, nodes with type "leaf" are displayed with bold text. Note that a pTemplate whose type is "default" applies to all nodes that have no type property defined.

  1. <p-organizationChart [value]="data"
  2. styleClass="company">
  3. <ng-template let-node pTemplate="leaf">
  4. <span style="font-weight:bold">{{node.label}}</span>
  5. </ng-template>
  6. <ng-template let-node pTemplate="default">
  7. {{node.label}}
  8. </ng-template>
  9. </p-organizationChart>
  10.  
  1. export class MyComponents implements OnInit {
  2. data: TreeNode[];
  3. ngOnInit() {
  4. this.data = [{
  5. label: 'Root',
  6. children: [
  7. {
  8. label: 'Child 1',
  9. children: [
  10. {
  11. label: 'Grandchild 1.1', type: 'leaf'
  12. },
  13. {
  14. label: 'Grandchild 1.2', type: 'leaf'
  15. }
  16. ]
  17. },
  18. {
  19. label: 'Child 2',
  20. children: [
  21. {
  22. label: 'Child 2.1', type: 'leaf'
  23. },
  24. {
  25. label: 'Child 2.2', type: 'leaf'
  26. }
  27. ]
  28. }
  29. ]
  30. }];
  31. }
  32.  

Expand/Collapse State

In order to display a treenode as expanded by default, set "expanded" property as true in your model.

Selection

OrganizationChart supports 2 selection methods; single or multiple. Selection is enabled by setting selectionMode property and providing a single TreeNode or an array of TreeNodes to reference the selections depending on the selection mode.

  1. <p-organizationChart [value]="data" selectionMode="single" [(selection)]="selectedNode"></p-organizationChart>
  2.  
  1. export class MyComponents implements OnInit {
  2. data: TreeNode[];
  3. ngOnInit() {
  4. this.data = [{
  5. label: 'Root',
  6. children: [
  7. {
  8. label: 'Child 1,
  9. children: [
  10. {
  11. label: 'Grandchild 1.1', type: 'leaf'
  12. },
  13. {
  14. label: 'Grandchild 1.2', type: 'leaf'
  15. }
  16. ]
  17. },
  18. {
  19. label: 'Child 2',
  20. children: [
  21. {
  22. label: 'Child 2.1', type: 'leaf'
  23. },
  24. {
  25. label: 'Child 2.2', type: 'leaf'
  26. }
  27. ]
  28. }
  29. ]
  30. }];
  31. }
  32.  

Properties

NameTypeDefaultDescription
valuenullTreeNode[]An array of nested TreeNodes.
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
selectionModestringnullDefines the selection mode, valid values "single" and "multiple".
selectionanynullA single treenode instance or an array to refer to the selections.

Events

NameParametersDescription
onNodeSelectevent.originalEvent: browser event event.node: Selected node instance.Callback to invoke when a node is selected.
onNodeUnselectevent.originalEvent: browser event event.node: Unselected node instance.Callback to invoke when a node is unselected.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-organizationchartContainer element.
ui-organizationchart-tableTable container of a node.
ui-organizationchart-linesConnector lines container.
ui-organizationchart-nodesContained of node children.
ui-organizationchart-line-rightRight side line of a node connector.
ui-organizationchart-line-leftLeft side line of a node connector.
ui-organizationchart-line-topTop side line of a node connector.

Dependencies

None.

Source

View on GitHub

  1. <p-toast [style]="{marginTop: '80px'}"></p-toast>
  2. <h3 class="first">Advanced</h3>
  3. <p>Organization with advanced customization.</p>
  4. <p-organizationChart [value]="data1" selectionMode="single" [(selection)]="selectedNode" (onNodeSelect)="onNodeSelect($event)"
  5. styleClass="company">
  6. <ng-template let-node pTemplate="person">
  7. <div class="node-header ui-corner-top">{node.label}</div>
  8. <div class="node-content">
  9. <img src="assets/showcase/images/demo/organization/{node.data.avatar}" width="32">
  10. <div>{node.data.name}</div>
  11. </div>
  12. </ng-template>
  13. <ng-template let-node pTemplate="department">
  14. {node.label}
  15. </ng-template>
  16. </p-organizationChart>
  17. <h3>Basic</h3>
  18. <p>Hierarchical data with zero configuration.</p>
  19. <p-organizationChart [value]="data2"></p-organizationChart>
  20.  
  1. @Component({
  2. templateUrl: './organizationchartdemo.html',
  3. providers: [MessageService],
  4. styles: [`
  5. .company.ui-organizationchart .ui-organizationchart-node-content.ui-person {
  6. padding: 0;
  7. border: 0 none;
  8. }
  9. .node-header,.node-content {
  10. padding: .5em .7em;
  11. }
  12. .node-header {
  13. background-color: #495ebb;
  14. color: #ffffff;
  15. }
  16. .node-content {
  17. text-align: center;
  18. border: 1px solid #495ebb;
  19. }
  20. .node-content img {
  21. border-radius: 50%;
  22. }
  23. .department-cfo {
  24. background-color: #7247bc;
  25. color: #ffffff;
  26. }
  27. .department-coo {
  28. background-color: #a534b6;
  29. color: #ffffff;
  30. }
  31. .department-cto {
  32. background-color: #e9286f;
  33. color: #ffffff;
  34. }
  35. .ui-person .ui-node-toggler {
  36. color: #495ebb !important;
  37. }
  38. .department-cto .ui-node-toggler {
  39. color: #8a0a39 !important;
  40. }
  41. `],
  42. encapsulation: ViewEncapsulation.None
  43. })
  44. export class OrganizationChartDemo implements OnInit {
  45. data1: TreeNode[];
  46. data2: TreeNode[];
  47. selectedNode: TreeNode;
  48. constructor(private messageService: MessageService) {}
  49. ngOnInit() {
  50. this.data1 = [{
  51. label: 'CEO',
  52. type: 'person',
  53. styleClass: 'ui-person',
  54. expanded: true,
  55. data: {name:'Walter White', 'avatar': 'walter.jpg'},
  56. children: [
  57. {
  58. label: 'CFO',
  59. type: 'person',
  60. styleClass: 'ui-person',
  61. expanded: true,
  62. data: {name:'Saul Goodman', 'avatar': 'saul.jpg'},
  63. children:[{
  64. label: 'Tax',
  65. styleClass: 'department-cfo'
  66. },
  67. {
  68. label: 'Legal',
  69. styleClass: 'department-cfo'
  70. }],
  71. },
  72. {
  73. label: 'COO',
  74. type: 'person',
  75. styleClass: 'ui-person',
  76. expanded: true,
  77. data: {name:'Mike E.', 'avatar': 'mike.jpg'},
  78. children:[{
  79. label: 'Operations',
  80. styleClass: 'department-coo'
  81. }]
  82. },
  83. {
  84. label: 'CTO',
  85. type: 'person',
  86. styleClass: 'ui-person',
  87. expanded: true,
  88. data: {name:'Jesse Pinkman', 'avatar': 'jesse.jpg'},
  89. children:[{
  90. label: 'Development',
  91. styleClass: 'department-cto',
  92. expanded: true,
  93. children:[{
  94. label: 'Analysis',
  95. styleClass: 'department-cto'
  96. },
  97. {
  98. label: 'Front End',
  99. styleClass: 'department-cto'
  100. },
  101. {
  102. label: 'Back End',
  103. styleClass: 'department-cto'
  104. }]
  105. },
  106. {
  107. label: 'QA',
  108. styleClass: 'department-cto'
  109. },
  110. {
  111. label: 'R&D',
  112. styleClass: 'department-cto'
  113. }]
  114. }
  115. ]
  116. }];
  117. this.data2 = [{
  118. label: 'F.C Barcelona',
  119. expanded: true,
  120. children: [
  121. {
  122. label: 'F.C Barcelona',
  123. expanded: true,
  124. children: [
  125. {
  126. label: 'Chelsea FC'
  127. },
  128. {
  129. label: 'F.C. Barcelona'
  130. }
  131. ]
  132. },
  133. {
  134. label: 'Real Madrid',
  135. expanded: true,
  136. children: [
  137. {
  138. label: 'Bayern Munich'
  139. },
  140. {
  141. label: 'Real Madrid'
  142. }
  143. ]
  144. }
  145. ]
  146. }];
  147. }
  148. onNodeSelect(event) {
  149. this.messageService.add({severity: 'success', summary: 'Node Selected', detail: event.node.label});
  150. }
  151. }
  152.