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.

1
  • Show us what did you try instead asking us to do it for you. Commented Oct 31, 2017 at 8:10

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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);
            }
        }
    }

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.