Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. It provides a lot of templates, components, theme design, an extensive icon library, and much more. In this article, we will see the Angular PrimeNG TreeTable Templating.
TreeTable is used to display hierarchical data in tabular format. The Events help us to know the actions that are taking place and we can keep a check on them and also show relevant updates accordingly. Using templates, custom content at the header, body and footer sections is supported.
Creating Angular application & Module Installation:
Step 1: Create an Angular application using the following command.
ng new geeks_angular
Step 2: After creating your project folder i.e. geeks_angular, move to it using the following command.
cd geeks_angular
Step 3: Install PrimeNG in your given directory.
npm install primeng --save npm install primeicons --save
Project Structure: The project structure will look like the following:
Steps to run the application: Run the below command to see the output
ng serve --save
Example 1: Below is the example code that illustrates the use of TreeTable Templating in Angular PrimeNG. In the following example, we have a simple TreeTable Template with a header and body.
- app.component.html:
< div style = "text-align: center;" >
< h1 style = "color: green;" >GeeksforGeeks</ h1 >
< h5 >Angular PrimeNG TreeTable Templating</ h5 >
</ div >
< p-treeTable [value]="gfg1"
[columns]="selectedColumns" dataKey = "name" >
< ng-template pTemplate = "caption" >
GeeksforGeeks
</ ng-template >
< ng-template pTemplate = "header" let-columns>
< tr >
< th * ngFor = "let col of columns" >
{{col.header}}
</ th >
</ tr >
</ ng-template >
< ng-template
pTemplate = "body"
let-rowNode
let-rowData = "rowData"
let-columns = "columns" >
< tr [ttRow]="rowNode" [ttRow]="rowNode"
[ttSelectableRow]="rowNode">
< td * ngFor = "let col of columns; let i = index" >
< p-treeTableToggler
[rowNode]="rowNode"
* ngIf = "i == 0" >
</ p-treeTableToggler >
{{rowData[col.field]}}
</ td >
</ tr >
</ ng-template >
</ p-treeTable >
|
- app.component.ts:
import { Component } from "@angular/core" ;
import { TreeNode } from "primeng/api" ;
import { MessageService } from "primeng/api" ;
@Component({ selector: "app-root" ,
templateUrl: "./app.component.html" ,
providers: [MessageService]
}) export class AppComponent { gfg1: TreeNode[];
cols: any[];
selectedColumns: any[];
ngOnInit() {
this .gfg1 = [
{
data: {
name: "A" ,
age: "40"
},
children: [
{
data: {
name: "B" ,
age: "16"
}
},
{
data: {
name: "C" ,
age: "14"
}
}
]
},
{
data: {
name: "D" ,
age: "55"
},
children: [
{
data: {
name: "E" ,
age: "20"
}
},
{
data: {
name: "F" ,
age: "24"
}
}
]
},
{
data: {
name: "G" ,
age: "32"
},
children: [
{
data: {
name: "H" ,
age: "20"
}
},
{
data: {
name: "I" ,
age: "24"
}
}
]
},
{
data: {
name: "J" ,
age: "64"
},
children: [
{
data: {
name: "K" ,
age: "20"
}
},
{
data: {
name: "L" ,
age: "24"
}
}
]
},
{
data: {
name: "M" ,
age: "12"
},
children: [
{
data: {
name: "N" ,
age: "20"
}
},
{
data: {
name: "O" ,
age: "24"
}
}
]
},
{
data: {
name: "P" ,
age: "34"
},
children: [
{
data: {
name: "Q" ,
age: "20"
}
},
{
data: {
name: "R" ,
age: "24"
}
}
]
},
{
data: {
name: "S" ,
age: "43"
},
children: [
{
data: {
name: "T" ,
age: "20"
}
},
{
data: {
name: "U" ,
age: "24"
}
}
]
}
];
this .cols = [
{ field: "name" , header: "First Name" },
{ field: "age" , header: "Age" }
];
this .selectedColumns = this .cols;
}
} |
- app.module.ts:
import { NgModule } from "@angular/core" ;
import { BrowserAnimationsModule } from "@angular/platform-browser/animations" ;
import { AppComponent } from "./app.component" ;
import { NodeService } from "./nodeservice" ;
import { TreeTableModule } from "primeng/treetable" ;
import { ButtonModule } from "primeng/button" ;
@NgModule({ imports: [
BrowserAnimationsModule,
TreeTableModule,
ButtonModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [NodeService]
}) export class AppModule {} |
Output:
Example 2: Below is another example code that illustrates the use of TreeTable Templating in Angular PrimeNG. In the following example, we have a simple TreeTable Template with a header, body, and footer section.
- app.component.html:
< div style = "text-align: center;" >
< h1 style = "color: green;" >GeeksforGeeks</ h1 >
< h5 >Angular PrimeNG TreeTable Templating</ h5 >
</ div >
< p-treeTable [value]="gfg1"
[columns]="selectedColumns" dataKey = "name" >
< ng-template pTemplate = "caption" >
GeeksforGeeks
</ ng-template >
< ng-template pTemplate = "header" let-columns>
< tr >
< th * ngFor = "let col of columns" >
{{col.header}}
</ th >
</ tr >
</ ng-template >
< ng-template
pTemplate = "body"
let-rowNode
let-rowData = "rowData"
let-columns = "columns" >
< tr [ttRow]="rowNode" [ttRow]="rowNode"
[ttSelectableRow]="rowNode">
< td * ngFor = "let col of columns; let i = index" >
< p-treeTableToggler
[rowNode]="rowNode"
* ngIf = "i == 0" >
</ p-treeTableToggler >
{{rowData[col.field]}}
</ td >
</ tr >
</ ng-template >
< ng-template pTemplate = "summary" >
< div style = "text-align:left" >
< p-button icon = "pi pi-code" ></ p-button >
</ div >
</ ng-template >
</ p-treeTable >
|
- app.component.ts:
import { Component } from "@angular/core" ;
import { TreeNode } from "primeng/api" ;
import { MessageService } from "primeng/api" ;
@Component({ selector: "app-root" ,
templateUrl: "./app.component.html" ,
providers: [MessageService]
}) export class AppComponent { gfg1: TreeNode[];
cols: any[];
selectedColumns: any[];
ngOnInit() {
this .gfg1 = [
{
data: {
name: "A" ,
age: "40"
},
children: [
{
data: {
name: "B" ,
age: "16"
}
},
{
data: {
name: "C" ,
age: "14"
}
}
]
},
{
data: {
name: "D" ,
age: "55"
},
children: [
{
data: {
name: "E" ,
age: "20"
}
},
{
data: {
name: "F" ,
age: "24"
}
}
]
},
{
data: {
name: "G" ,
age: "32"
},
children: [
{
data: {
name: "H" ,
age: "20"
}
},
{
data: {
name: "I" ,
age: "24"
}
}
]
},
{
data: {
name: "J" ,
age: "64"
},
children: [
{
data: {
name: "K" ,
age: "20"
}
},
{
data: {
name: "L" ,
age: "24"
}
}
]
},
{
data: {
name: "M" ,
age: "12"
},
children: [
{
data: {
name: "N" ,
age: "20"
}
},
{
data: {
name: "O" ,
age: "24"
}
}
]
},
{
data: {
name: "P" ,
age: "34"
},
children: [
{
data: {
name: "Q" ,
age: "20"
}
},
{
data: {
name: "R" ,
age: "24"
}
}
]
},
{
data: {
name: "S" ,
age: "43"
},
children: [
{
data: {
name: "T" ,
age: "20"
}
},
{
data: {
name: "U" ,
age: "24"
}
}
]
}
];
this .cols = [
{ field: "name" , header: "First Name" },
{ field: "age" , header: "Age" }
];
this .selectedColumns = this .cols;
}
} |
- app.module.ts:
import { NgModule } from "@angular/core" ;
import { BrowserAnimationsModule } from "@angular/platform-browser/animations" ;
import { AppComponent } from "./app.component" ;
import { NodeService } from "./nodeservice" ;
import { TreeTableModule } from "primeng/treetable" ;
import { ButtonModule } from "primeng/button" ;
@NgModule({ imports: [
BrowserAnimationsModule,
TreeTableModule,
ButtonModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [NodeService]
}) export class AppModule {} |
Output:
Reference: https://primefaces.org/primeng/treetable/templating