5

This time i need help about how to delete a row based the row ID in html-table when delete button clicked. The table data source is from a separated Json file.

The table looks like this : Image Link

<div class="container">
        <table border=1 class="table">
          <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Age</th>
            <th>Phone</th>
          </tr>
            <tr *ngFor="let d of data | async" [attr.id]="d.id"> <!--each row id == id-->
                <td>{{ d.id }}</td>
                <td>{{ d.name }}</td>
                <td>{{ d.email }}</td>
                <td>{{ d.age }}</td>
                <td>{{ d.phone }}</td>
                <button id="remove">DELETE ROW</button>
            </tr>
        </table> 
    </div>

Please let me know if more snippets are needed. Thank you.

Wira Xie
  • 819
  • 5
  • 24
  • 46

3 Answers3

5
<div class="container">
        <table border=1 class="table">
          <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Age</th>
            <th>Phone</th>
          </tr>
            <tr *ngFor="let d of data | async" [attr.id]="d.id"> <!--each row id == id-->
                <td>{{ d.id }}</td>
                <td>{{ d.name }}</td>
                <td>{{ d.email }}</td>
                <td>{{ d.age }}</td>
                <td>{{ d.phone }}</td>
                <button id="remove" (click)="deleteRow(d)">DELETE ROW</button>
            </tr>
        </table> 
    </div>

Typescript

deleteRow(d){
    const index = this.data.indexOf(d);
    this.data.splice(index, 1);
}
3

you can add this code in you HTML file

<div class="container">
            <table border=1 class="table">
              <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Age</th>
                <th>Phone</th>
              </tr>
                <tr *ngFor="let d of data | async" [attr.id]="d.id"> <!--each row id == id-->
                    <td>{{ d.id }}</td>
                    <td>{{ d.name }}</td>
                    <td>{{ d.email }}</td>
                    <td>{{ d.age }}</td>
                    <td>{{ d.phone }}</td>
                    <button id="remove" (click)="deleteRow(d.id)">DELETE ROW</button>
                </tr>
            </table> 
        </div>

And add this code in youe component file

deleteRow(id){
        for(let i = 0; i < this.data.length; ++i){
            if (this.data[i].id === id) {
                this.data.splice(i,1);
            }
        }
    }
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
1

I would add a (click) event on the button and pass 'd' as the parameter. Then in the function that the click calls, I would splice the 'deleted' item.

Code to add (click) event :

<button id="remove" (click)="functionToDeleteItem(d)">DELETE ROW</button>

Example to splice array and remove deleted item:

stackoverflow past example - How do I remove an array item in TypeScript?

Also, unless you are using the id="remove" in the button for some other use like css, I would remove it as it is not needed.

John
  • 46
  • 4