Open In App

Angular PrimeNG Table ContextMenu

Improve
Improve
Like Article
Like
Save
Share
Report

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 Table ContextMenu.

The Table Component is used to display tabular data. PrimeNG provides a lot of customization in properties, methods, templates, and styling to make the component look and function as required. The Table component can also be made dynamic so that the data can be loaded efficiently.

Creating Angular application & module installation:

Step 1: Create an Angular application using the following command.

ng new appname

Step 2: After creating your project folder i.e. appname, move to it using the following command.

cd appname

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: It will look like the following:

 

  • Steps to run the application: To run the above file run the below command:
ng serve --save

Example 1: This is the basic example that shows how to use the Angular PrimeNG Table ContextMenu. In this example, we have ContextMenu to delete a particular row using its id.

  • app.component.html:

HTML




<h1 style="color: green; 
           text-align: center;">
  GeeksforGeeks
</h1>
<h3>Angular PrimeNG Table ContextMenu</h3>
  
<p-table #dt styleClass="p-datatable-striped"
             [value]="tutorials"
             [columns]="cols"
             [(contextMenuSelection)]="selectedProduct"
             [contextMenu]="cm">
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns">
                {{ col.header }}
            </th>
        </tr>
    </ng-template>
  
    <ng-template pTemplate="body" let-rowData 
                  let-columns="columns">
        <tr [pContextMenuRow]="rowData">
            <td *ngFor="let col of columns">
                {{ rowData[col.field] }}
            </td>
        </tr>
    </ng-template>
</p-table>
  
<p-contextMenu #cm [model]="gfg"></p-contextMenu>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
import { MessageService, MenuItem } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    providers: [MessageService]
})
  
export class AppComponent {
    cols: any[];
    tutorials: Tutorial[];
    selectedProduct: Tutorial;
    gfg: MenuItem[];
    selectedColumns: any[];
    constructor(private messageService: MessageService) { }
  
    ngOnInit() {
        this.tutorials = [
            {
                id: 1,
                title: "Queue",
                category: "Data Structure",
                rating: 8
            },
            {
                id: 2,
                title: "Circularly LinkedList",
                category: "Data Structure",
                rating: 1
            },
            {
                id: 3,
                title: "Doubly LinkedList",
                category: "Data Structure",
                rating: 3
            },
            {
                id: 4,
                title: "Singly LinkedList",
                category: "Data Structure",
                rating: 5
            },
            {
                id: 5,
                title: "Doubly Ended Queue",
                category: "Data Structure",
                rating: 10
            },
            {
                id: 6,
                title: "Binary Search Tree",
                category: "Data Structure",
                rating: 2
            },
            {
                id: 7,
                title: "Red Black Tree",
                category: "Data Structure",
                rating: 9
            },
            {
                id: 8,
                title: "Breadth First Search",
                category: "Graph",
                rating: 6
            },
            {
                id: 9,
                title: "Floyd's Cycle",
                category: "Algorithm",
                rating: 7
            },
            {
                id: 10,
                title: "Travelling Salesman Problem",
                category: "Algorithm",
                rating: 4
            },
            {
                id: 11,
                title: "Bellman Ford",
                category: "Graph",
                rating: 8
            },
            {
                id: 12,
                title: "KMP Algorithm",
                category: "String",
                rating: 10
            }
        ];
  
        this.cols = [
            { field: "id", header: "Id" },
            { field: "title", header: "Title" },
            { field: "category", header: "Category" },
            { field: "rating", header: "Rating" }
        ];
  
        this.gfg = [
            {
                label: "Remove row",
                command: () => this.deleteProduct(this.selectedProduct)
            }
        ];
    }
  
    deleteProduct(product: Tutorial) {
        this.tutorials =
            this.tutorials.filter(
                (p) => p.id !== product.id
            );
        this.selectedProduct = null;
    }
}
  
export interface Tutorial {
    id?: number;
    title?: string;
    category?: string;
    rating?: number;
}


  • 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 { AppComponent } from './app.component';
import { TableModule } from 'primeng/table';
import { ContextMenuModule } 
    from 'primeng/contextmenu';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TableModule,
        ContextMenuModule,
        HttpClientModule,
        FormsModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
  
export class AppModule { }


Output:

 

Example 2: This is another example that shows how to use the Angular PrimeNG Table ContextMenu. In this example, we have ContextMenu to view a particular row and show the category in the toast message.

  • app.component.html:

HTML




<h1 style="color: green; 
           text-align: center;">
  GeeksforGeeks
</h1>
<h3>Angular PrimeNG Table ContextMenu</h3>
  
<p-table #dt styleClass="p-datatable-striped"
             [value]="tutorials"
             [columns]="cols"
             [(contextMenuSelection)]="selectedProduct"
             [contextMenu]="cm">
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns">
                {{ col.header }}
            </th>
        </tr>
    </ng-template>
  
    <ng-template pTemplate="body" let-rowData
                   let-columns="columns">
        <tr [pContextMenuRow]="rowData">
            <td *ngFor="let col of columns">
                {{ rowData[col.field] }}
            </td>
        </tr>
    </ng-template>
</p-table>
<p-toast></p-toast>
<p-contextMenu #cm [model]="gfg"></p-contextMenu>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
import { MessageService, MenuItem } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    providers: [MessageService]
})
  
export class AppComponent {
    cols: any[];
    tutorials: Tutorial[];
    selectedProduct: Tutorial;
    gfg: MenuItem[];
    selectedColumns: any[];
    constructor(private messageService: MessageService) {}
  
    ngOnInit() {
        this.tutorials = [
            {
                id: 1,
                title: "Queue",
                category: "Data Structure",
                rating: 8
            },
            {
                id: 2,
                title: "Circularly LinkedList",
                category: "Data Structure",
                rating: 1
            },
            {
                id: 3,
                title: "Doubly LinkedList",
                category: "Data Structure",
                rating: 3
            },
            {
                id: 4,
                title: "Singly LinkedList",
                category: "Data Structure",
                rating: 5
            },
            {
                id: 5,
                title: "Doubly Ended Queue",
                category: "Data Structure",
                rating: 10
            },
            {
                id: 6,
                title: "Binary Search Tree",
                category: "Data Structure",
                rating: 2
            },
            {
                id: 7,
                title: "Red Black Tree",
                category: "Data Structure",
                rating: 9
            },
            {
                id: 8,
                title: "Breadth First Search",
                category: "Graph",
                rating: 6
            },
            {
                id: 9,
                title: "Floyd's Cycle",
                category: "Algorithm",
                rating: 7
            },
            {
                id: 10,
                title: "Travelling Salesman Problem",
                category: "Algorithm",
                rating: 4
            },
            {
                id: 11,
                title: "Bellman Ford",
                category: "Graph",
                rating: 8
            },
            {
                id: 12,
                title: "KMP Algorithm",
                category: "String",
                rating: 10
            }
        ];
  
        this.cols = [
            { field: "id", header: "Id" },
            { field: "title", header: "Title" },
            { field: "category", header: "Category" },
            { field: "rating", header: "Rating" }
        ];
  
        this.gfg = [
            {
                label: "See this Row",
                command: () => this.viewProduct(this.selectedProduct)
            },
        ];
    }
  
    viewProduct(product: Tutorial) {
        this.messageService.add({
            severity: "success",
            summary: "Tutorial Selected is: ",
            detail: product.title
        });
    }
}
  
export interface Tutorial {
    id?: number;
    title?: string;
    category?: string;
    rating?: number;
}


  • 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 { TableModule } from "primeng/table";
import { ToastModule } from "primeng/toast";
import { ContextMenuModule } 
    from "primeng/contextmenu";
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TableModule,
        ContextMenuModule,
        ToastModule,
        HttpClientModule,
        FormsModule,
        RouterModule.forRoot([
            { path: "", component: AppComponent }
        ])
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
  
})
  
export class AppModule { }


Output:

 

Reference: https://primefaces.org/primeng/table/contextmenu



Last Updated : 12 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads