,

Angular 15 Inline Table Editing: A Beginner’s Tutorial (Part 1)

Posted by






Inline Table Editing Angular 15

Inline Table Editing in Angular 15

Welcome to the first part of our Angular tutorial series on inline table editing. In this tutorial, we will learn how to implement inline editing in a table using Angular 15. Inline editing allows the user to edit data directly in the table without the need for a separate form or popup.

Prerequisites

Before we begin, make sure you have a basic understanding of HTML, CSS, and JavaScript. It is also recommended to have some knowledge of Angular and its concepts. If you are new to Angular, you can check out our Angular tutorials for beginners to get started.

Setting up the project

First, let’s create a new Angular project. Open your terminal or command prompt and run the following commands:

    ng new inline-table-editing-angular-15
    cd inline-table-editing-angular-15
    ng serve
  

This will create a new Angular project and start a development server. Now, open your project in your favorite code editor and let’s get started with the implementation.

Implementing inline table editing

For the purpose of this tutorial, we will create a simple table with some sample data and allow the user to edit the data inline. We will use Angular 15’s data binding and event handling to achieve this.

First, open the app.component.html file in your project and add the following code to create a basic table:

    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Email</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>
        <tr *ngFor="let user of users">
          <td>
            <span *ngIf="!user.editing">{{ user.name }}</span>
            <input *ngIf="user.editing" [(ngModel)]="user.name">
          </td>
          <td>
            <span *ngIf="!user.editing">{{ user.email }}</span>
            <input *ngIf="user.editing" [(ngModel)]="user.email">
          </td>
          <td>
            <button *ngIf="!user.editing" (click)="toggleEditing(user)">Edit</button>
            <button *ngIf="user.editing" (click)="saveChanges(user)">Save</button>
          </td>
        </tr>
      </tbody>
    </table>
  

Next, open the app.component.ts file and add the following code to define the logic for editing the table data:

    import { Component } from '@angular/core';

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      users = [
        { name: 'John Doe', email: 'john@example.com', editing: false },
        { name: 'Jane Doe', email: 'jane@example.com', editing: false }
      ];

      toggleEditing(user) {
        user.editing = !user.editing;
      }

      saveChanges(user) {
        user.editing = false;
      }
    }
  

With this code, we have created a simple table with inline editing functionality. When the user clicks the “Edit” button, the table cells become editable, and when they click the “Save” button, the changes are saved and the cells become non-editable again.

Conclusion

Congratulations! You have successfully implemented inline table editing in Angular 15. In the next part of this tutorial series, we will explore more advanced features and customizations for inline table editing. Stay tuned!


0 0 votes
Article Rating
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Abhishek Kumar
7 months ago

record is not submit why ??

Jo Paji
7 months ago

this is some really good!!! tut continues this practical IT series bro

Gott hilft
7 months ago

thank you very much. That is great!!

Adam
7 months ago

Keep IT up Chetan. nice work

codeFirst Academy
7 months ago

nice scenario for fresher.

Charles J
7 months ago

nice ! keep Posting…!#angular

amber
7 months ago

simply great for small forms. thanks for sharing

Petar's World
7 months ago

Great video! What comes in my mind, can we replace json stringify and parse with structuredClone function to copy the obj?

Sanket
7 months ago

Nice sirji..so much helpful

kiran
7 months ago

Nice n very practical scenarios