如何通过单击按钮 Angular 2 从表中删除特定行

sne*_*ank 2 javascript html-table angular

我试图通过单击 for 循环中该行中的按钮来删除特定行,但它会删除该表的所有行。

这是我的 HTML 代码:

<table id="mytable" class="table table-bordred table-striped">
    <tbody id="del">
        <tr *ngFor="let cart of modalData">
            <td>
                <div style="display:inline-flex;">
                    <div style="margin-left:10px;">
                        <p class="font_weight" style="font-size:13px;">{{cart.ExamName}}</p>
                        <p>{{cart.Categoryname}}|{{cart.CompanyName}}</p>
                    </div>
                </div>
            </td>
            <td>
                <div style="margin-top: 32px;">
                    <p class="font_weight" style="font-size:13px;"></p> <span class="font_weight" style="font-size: 13px;"> {{cart.Amount| currency :'USD':'true'}} </span> </div>
            </td>
            <td>
                <div style="margin-top: 19px;">
                    <button class="button_transparent" (click)="Delete(cart)"> delete </button>
                </div>
            </td>
        </tr>
        <tr> </tr>
    </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

这是我的组件:

public loadData() {       

                let transaction = new TransactionalInformation();
                this.myCartService.GetExamOrderForCart()
                    .subscribe(response => this.getDataOnSuccess(response),
                    response => this.getDataOnError(response));          

        }

    getDataOnSuccess(response) {       
        this.modalData = response.Items;
        }
Run Code Online (Sandbox Code Playgroud)

这是我的删除方法:

public Delete(response) {
     this.myCartService.updateExamOrder(response)
     .subscribe(response => this.getDataOnSuccessForDelete(response),
     response => this.getDataOnErrorForDelete(response));
} 
Run Code Online (Sandbox Code Playgroud)

请帮我做,如何从表中仅删除一行?

Ser*_*ero 6

您可以添加index*ngFor :

<tr *ngFor="let cart of modalData;let i = index">
Run Code Online (Sandbox Code Playgroud)

然后,在方法中传递索引:

<button class="button_transparent" (click)="delete(i)"> delete </button>
Run Code Online (Sandbox Code Playgroud)

最后:

delete(i){
  this.modalData.splice(i,1);
}
Run Code Online (Sandbox Code Playgroud)