Angular PrimeNG TreeTable ContextMenu
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 ContextMenu.
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.
Creating Angular application & Module Installation:
Step 1: Create an Angular application using the following command.
ng new Beginner_angular
Step 2: After creating your project folder i.e. Beginner_angular, move to it using the following command.
cd Beginner_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 ContextMenu Toggle in Angular PrimeNG. In the following example, we have a simple context menu to toggle the cell.
- app.component.html:
HTML
< div style = "text-align: center;" > < h1 style = "color: green;" >w3wiki</ h1 > < h5 >Angular PrimeNG TreeTable ContextMenu</ h5 > </ div > < p-treeTable [value]="gfg1" [columns]="selectedColumns" dataKey = "name" [(contextMenuSelection)]="selectedNode" [contextMenu]="gf"> < 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 [ttContextMenuRow]="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 > < p-contextMenu #gf [model]="gfg2"></ p-contextMenu > |
- app.component.ts:
Javascript
import { Component, OnInit } from "@angular/core" ; import { NodeService } from "./nodeservice" ; import { TreeNode } from "primeng/api" ; import { MessageService, MenuItem } from "primeng/api" ; @Component({ selector: "app-root" , templateUrl: "./app.component.html" , providers: [MessageService] }) export class AppComponent { gfg1: TreeNode[]; cols: any[]; selectedColumns: any[]; gfg2: MenuItem[]; selectedNode: TreeNode; constructor( private nodeService: NodeService, private messageService: MessageService ) {} 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; this .gfg2 = [ { label: "Expand" , icon: "pi pi-sort" , command: (event) => this .toggleGeekFile( this .selectedNode) } ]; } toggleGeekFile(node) { node.expanded = !node.expanded; this .gfg1 = [... this .gfg1]; } } |
- app.module.ts:
Javascript
import { NgModule } from "@angular/core" ; import { BrowserModule } from "@angular/platform-browser" ; import { FormsModule } from "@angular/forms" ; import { HttpClientModule } from "@angular/common/http" ; import { BrowserAnimationsModule } from "@angular/platform-browser/animations" ; import { RouterModule } from "@angular/router" ; import { AppComponent } from "./app.component" ; import { NodeService } from "./nodeservice" ; import { TreeTableModule } from "primeng/treetable" ; import { ButtonModule } from "primeng/button" ; import { DialogModule } from "primeng/dialog" ; import { MultiSelectModule } from "primeng/multiselect" ; import { InputTextModule } from "primeng/inputtext" ; import { ToastModule } from "primeng/toast" ; import { ContextMenuModule } from "primeng/contextmenu" ; @NgModule({ imports: [ BrowserModule, BrowserAnimationsModule, TreeTableModule, ToastModule, DialogModule, ButtonModule, MultiSelectModule, InputTextModule, ContextMenuModule, HttpClientModule, FormsModule, RouterModule.forRoot([ { path: "" , component: AppComponent } ]) ], declarations: [AppComponent], bootstrap: [AppComponent], providers: [NodeService] }) export class AppModule {} |
Output:
Example 2: Below is another example code that illustrates the use of ContextMenu Toggle in Angular PrimeNG. In the following example, we have a simple context menu to view the cell and show the value of the cell as a toast message.
- app.component.html:
HTML
< div style = "text-align: center;" > < h1 style = "color: green;" >w3wiki</ h1 > < h5 >Angular PrimeNG TreeTable ContextMenu</ h5 > </ div > < p-treeTable [value]="gfg1" [columns]="selectedColumns" dataKey = "name" [(contextMenuSelection)]="selectedNode" [contextMenu]="gf"> < 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 [ttContextMenuRow]="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 > < p-contextMenu #gf [model]="gfg2"></ p-contextMenu > < p-toast [style]="{marginTop: '80px'}"></ p-toast > |
- app.component.ts:
Javascript
import { Component } from "@angular/core" ; import { NodeService } from "./nodeservice" ; import { TreeNode } from "primeng/api" ; import { MessageService, MenuItem } from "primeng/api" ; @Component({ selector: "app-root" , templateUrl: "./app.component.html" , providers: [MessageService] }) export class AppComponent { gfg1: TreeNode[]; cols: any[]; selectedColumns: any[]; gfg2: MenuItem[]; selectedNode: TreeNode; constructor(private messageService: MessageService) {} 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; this .gfg2 = [ { label: "View it Geek" , icon: "pi pi-search" , command: (event) => this .viewGeekFile( this .selectedNode) } ]; } viewGeekFile(node) { this .messageService.add({ severity: "success" , summary: "Selected Cell is: " , detail: node.data.name }); } } |
- app.module.ts:
Javascript
import { NgModule } from "@angular/core" ; import { BrowserModule } from "@angular/platform-browser" ; import { FormsModule } from "@angular/forms" ; import { HttpClientModule } from "@angular/common/http" ; import { BrowserAnimationsModule } from "@angular/platform-browser/animations" ; import { RouterModule } from "@angular/router" ; import { AppComponent } from "./app.component" ; import { NodeService } from "./nodeservice" ; import { TreeTableModule } from "primeng/treetable" ; import { ButtonModule } from "primeng/button" ; import { DialogModule } from "primeng/dialog" ; import { MultiSelectModule } from "primeng/multiselect" ; import { InputTextModule } from "primeng/inputtext" ; import { ToastModule } from "primeng/toast" ; import { ContextMenuModule } from "primeng/contextmenu" ; @NgModule({ imports: [ BrowserModule, BrowserAnimationsModule, TreeTableModule, ToastModule, DialogModule, ButtonModule, MultiSelectModule, InputTextModule, ContextMenuModule, HttpClientModule, FormsModule, RouterModule.forRoot([ { path: "" , component: AppComponent } ]) ], declarations: [AppComponent], bootstrap: [AppComponent], providers: [NodeService] }) export class AppModule {} |
Output:
Reference: https://primefaces.org/primeng/treetable/contextmenu
Contact Us