Open In App

Angular PrimeNG Table Row Editing

Last Updated : 10 Nov, 2022
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 are going to learn Angular PrimeNG Table Row Editing.

Row Editing helps to edit each and every individual row and then modify the contents of the row and also save them. This method uses manual editing and saving.

Syntax: Table Row Editing is done as follows:

<p-table [value]="tutorials" dataKey="title" editMode="row">
  <ng-template pTemplate="body" let-tutorial 
    let-editing="editing" let-ri="rowIndex">
    <tr [pEditableRow]="tutorial">
      <td>
        <p-cellEditor>
          <ng-template pTemplate="input">
            <input pInputText type="text" 
              [(ngModel)]="tutorial.title" />
          </ng-template>
          <ng-template pTemplate="output">
            {{ tutorial.title }}
          </ng-template>
        </p-cellEditor>
      </td>
    </tr>
  </ng-template>
</p-table>

 

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: After successful installation, the following project structure will be displayed:

 

Example 1: In the following example, we have a Table with a row editing feature in Angular PrimeNG.

  • app.component.html:

HTML




<h1 style="color:green;
           text-align:center;">
  GeeksforGeeks
</h1>
<h3>Angular PrimeNG Table Row Editing</h3>
<h5>Basic</h5>
  
<p-table #dt [value]="tutorials" 
             dataKey="index"
             editMode="row" 
             responsiveLayout="scroll">
    <ng-template pTemplate="caption">
        <div class="table-header">
            List of Tutorials
            <span class="p-input-icon-left">
                <i class="pi pi-search"></i>
                <input pInputText type="text" 
                       (input)=
    "dt.filterGlobal($event.target.value, 'contains')" 
                       placeholder="Global Search" />
            </span>
        </div>
    </ng-template>
    <ng-template pTemplate="header">
        <tr>
            <th style="width: 3rem"></th>
            <th pSortableColumn="title">
                Title <p-sortIcon field="title"></p-sortIcon>
            </th>
            <th pSortableColumn="category">
                Category <p-sortIcon field="category"></p-sortIcon>
            </th>
            <th pSortableColumn="rating">
                Rating <p-sortIcon field="rating"></p-sortIcon>
            </th>
            <th style="width:8rem"></th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" 
                 let-tutorial let-editing="editing"
                 let-ri="rowIndex">
        <tr [pEditableRow]="tutorial">
            <td>
                <p-tableCheckbox [value]="tutorial" 
                                 [index]="ri">
                </p-tableCheckbox>
            </td>
            <td>
                <p-cellEditor>
                    <ng-template pTemplate="input">
                        <input pInputText type="text" 
                               [(ngModel)]="tutorial.title" />
                    </ng-template>
                    <ng-template pTemplate="output">
                        {{ tutorial.title }}
                    </ng-template>
                </p-cellEditor>
            </td>
            <td>
                <p-cellEditor>
                    <ng-template pTemplate="input">
                        <input pInputText type="text" 
                               [(ngModel)]="tutorial.category" />
                    </ng-template>
                    <ng-template pTemplate="output">
                        {{ tutorial.category }}
                    </ng-template>
                </p-cellEditor>
            </td>
            <td>
                <p-cellEditor>
                    <ng-template pTemplate="input">
                        <input pInputText type="text" 
                               [(ngModel)]="tutorial.rating" />
                    </ng-template>
                    <ng-template pTemplate="output">
                        {{ tutorial.rating }}
                    </ng-template>
                </p-cellEditor>
            </td>
  
            <td style="text-align:center">
                <button *ngIf="!editing"
                          pButton pRipple type="button" 
                          pInitEditableRow icon="pi pi-pencil" 
                          (click)="onRowEditInit(tutorial, ri)" 
                          class="p-button-rounded p-button-text">
                </button>
                <button *ngIf="editing" 
                          pButton pRipple type="button" 
                          pSaveEditableRow icon="pi pi-check" 
                          (click)="onRowEditSave(tutorial, ri)" 
                          class="p-button-rounded 
                                 p-button-text 
                                 p-button-success mr-2">
                </button>
            </td>
        </tr>
    </ng-template>
</p-table>


  • app.component.ts

Javascript




import { Component, OnInit, ViewChild } from '@angular/core';
import { Table } from 'primeng/table';
import { PrimeNGConfig } from 'primeng/api';
import { MessageService } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styles: [
        `
  :host ::ng-deep .p-cell-editing {
      padding-top: 0 !important;
      padding-bottom: 0 !important;
  }
`,
    ],
})
export class AppComponent {
    tutorials: Tutorial[];
    @ViewChild('dt') table: Table;
    clonedTutorials: { [s: string]: Tutorial } = {};
    constructor(private primengConfig: PrimeNGConfig) { }
  
    ngOnInit() {
        this.tutorials = [
            {
                title: 'Queue',
                category: 'Data Structure',
                rating: 8,
            },
            {
                title: 'Circularly LinkedList',
                category: 'Data Structure',
                rating: 1,
            },
            {
                title: 'Doubly LinkedList',
                category: 'Data Structure',
                rating: 3,
            },
            {
                title: 'Singly LinkedList',
                category: 'Data Structure',
                rating: 5,
            },
            {
                title: 'Doubly Ended Queue',
                category: 'Data Structure',
                rating: 10,
            },
            {
                title: 'Binary Search Tree',
                category: 'Data Structure',
                rating: 2,
            },
            {
                title: 'Red Black Tree',
                category: 'Data Structure',
                rating: 9,
            },
            {
                title: 'Breadth First Search',
                category: 'Graph',
                rating: 6,
            },
            {
                title: "Floyd's Cycle",
                category: 'Algorithm',
                rating: 7,
            },
            {
                title: 'Travelling Salesman Problem',
                category: 'Algorithm',
                rating: 4,
            },
            {
                title: 'Bellman Ford',
                category: 'Graph',
                rating: 8,
            },
            {
                title: 'KMP Algorithm',
                category: 'String',
                rating: 10,
            },
        ];
  
        this.primengConfig.ripple = true;
    }
    onRowEditInit(tutorial: Tutorial, index: number) {
        this.clonedTutorials[index] = { ...this.tutorials[index] };
    }
  
    onRowEditSave(tutorial: Tutorial, index: number) {
        if (tutorial.rating > 0) {
            delete this.clonedTutorials[index];
        } else {
        }
    }
}
export interface Tutorial {
    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 { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TableModule } from 'primeng/table';
import { MultiSelectModule } from 'primeng/multiselect';
import { ButtonModule } from 'primeng/button';
import { InputTextModule } from 'primeng/inputtext';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TableModule,
        MultiSelectModule,
        ButtonModule,
        InputTextModule,
        FormsModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
export class AppModule { }


Output:

 

Example 2: In the following example, we have the option of canceling the editing of cells in Angular PrimeNG.

  • app.component.html:

HTML




<h1 style="color:green;
           text-align:center;">
    GeeksforGeeks
</h1>
<h3>Angular PrimeNG Table Row Editing</h3>
<h5>Basic</h5>
  
<p-table #dt [value]="tutorials" 
             dataKey="index" 
             editMode="row" 
             responsiveLayout="scroll">
    <ng-template pTemplate="caption">
        <div class="table-header">
            List of Tutorials
            <span class="p-input-icon-left">
                <i class="pi pi-search"></i>
                <input pInputText type="text" 
                       (input)=
    "dt.filterGlobal($event.target.value, 'contains')" 
                        placeholder="Global Search" />
            </span>
        </div>
    </ng-template>
    <ng-template pTemplate="header">
        <tr>
            <th style="width: 3rem"></th>
            <th pSortableColumn="title">
                Title <p-sortIcon field="title"></p-sortIcon>
            </th>
            <th pSortableColumn="category">
                Category <p-sortIcon field="category"></p-sortIcon>
            </th>
            <th pSortableColumn="rating">
                Rating <p-sortIcon field="rating"></p-sortIcon>
            </th>
            <th style="width:8rem"></th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" 
                 let-tutorial let-editing="editing" 
                 let-ri="rowIndex">
        <tr [pEditableRow]="tutorial">
            <td>
                <p-tableCheckbox [value]="tutorial" 
                                 [index]="ri">
                </p-tableCheckbox>
            </td>
            <td>
                <p-cellEditor>
                    <ng-template pTemplate="input">
                        <input pInputText type="text" 
                               [(ngModel)]="tutorial.title" />
                    </ng-template>
                    <ng-template pTemplate="output">
                        {{ tutorial.title }}
                    </ng-template>
                </p-cellEditor>
            </td>
            <td>
                <p-cellEditor>
                    <ng-template pTemplate="input">
                        <input pInputText type="text" 
                               [(ngModel)]="tutorial.category" />
                    </ng-template>
                    <ng-template pTemplate="output">
                        {{ tutorial.category }}
                    </ng-template>
                </p-cellEditor>
            </td>
            <td>
                <p-cellEditor>
                    <ng-template pTemplate="input">
                        <input pInputText type="text" 
                               [(ngModel)]="tutorial.rating" />
                    </ng-template>
                    <ng-template pTemplate="output">
                        {{ tutorial.rating }}
                    </ng-template>
                </p-cellEditor>
            </td>
  
            <td style="text-align:center">
                <button *ngIf="!editing" 
                         pButton pRipple type="button" 
                         pInitEditableRow icon="pi pi-pencil" 
                         (click)="onRowEditInit(tutorial, ri)" 
                         class="p-button-rounded 
                                p-button-text">
                </button>
                <button *ngIf="editing" 
                         pButton pRipple type="button" 
                         pSaveEditableRow icon="pi pi-check" 
                         (click)="onRowEditSave(tutorial, ri)" 
                         class="p-button-rounded 
                                p-button-text 
                                p-button-success mr-2">
                </button>
                <button *ngIf="editing" 
                         pButton pRipple type="button" 
                         pCancelEditableRow icon="pi pi-times" 
                         (click)="onRowEditCancel(tutorial, ri)" 
                         class="p-button-rounded 
                                p-button-text 
                                p-button-danger">
                </button>
            </td>
        </tr>
    </ng-template>
</p-table>


  • app.component.ts:

Javascript




import { Component, OnInit, ViewChild } from '@angular/core';
import { Table } from 'primeng/table';
import { PrimeNGConfig } from 'primeng/api';
import { MessageService } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styles: [
        `
  :host ::ng-deep .p-cell-editing {
      padding-top: 0 !important;
      padding-bottom: 0 !important;
  }
`,
    ],
})
export class AppComponent {
    tutorials: Tutorial[];
    @ViewChild('dt') table: Table;
    clonedTutorials: { [s: string]: Tutorial } = {};
    constructor(private primengConfig: PrimeNGConfig) { }
  
    ngOnInit() {
        this.tutorials = [
            {
                title: 'Queue',
                category: 'Data Structure',
                rating: 8,
            },
            {
                title: 'Circularly LinkedList',
                category: 'Data Structure',
                rating: 1,
            },
            {
                title: 'Doubly LinkedList',
                category: 'Data Structure',
                rating: 3,
            },
            {
                title: 'Singly LinkedList',
                category: 'Data Structure',
                rating: 5,
            },
            {
                title: 'Doubly Ended Queue',
                category: 'Data Structure',
                rating: 10,
            },
            {
                title: 'Binary Search Tree',
                category: 'Data Structure',
                rating: 2,
            },
            {
                title: 'Red Black Tree',
                category: 'Data Structure',
                rating: 9,
            },
            {
                title: 'Breadth First Search',
                category: 'Graph',
                rating: 6,
            },
            {
                title: "Floyd's Cycle",
                category: 'Algorithm',
                rating: 7,
            },
            {
                title: 'Travelling Salesman Problem',
                category: 'Algorithm',
                rating: 4,
            },
            {
                title: 'Bellman Ford',
                category: 'Graph',
                rating: 8,
            },
            {
                title: 'KMP Algorithm',
                category: 'String',
                rating: 10,
            },
        ];
  
        this.primengConfig.ripple = true;
    }
    onRowEditInit(tutorial: Tutorial, index: number) {
        this.clonedTutorials[index] = { ...this.tutorials[index] };
    }
  
    onRowEditSave(tutorial: Tutorial, index: number) {
        if (tutorial.rating > 0) {
            delete this.clonedTutorials[index];
        } else {
        }
    }
  
    onRowEditCancel(tutorial: Tutorial, index: number) {
        this.tutorials[index] = this.clonedTutorials[index];
        delete this.clonedTutorials[index];
    }
}
export interface Tutorial {
    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 { BrowserAnimationsModule } 
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TableModule } from 'primeng/table';
import { MultiSelectModule } from 'primeng/multiselect';
import { ButtonModule } from 'primeng/button';
import { InputTextModule } from 'primeng/inputtext';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        TableModule,
        MultiSelectModule,
        ButtonModule,
        InputTextModule,
        FormsModule,
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
export class AppModule { }


Output:

 

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads