简体   繁体   English

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

[英]How to delete a particular row from table by clicking button Angular 2

I am trying to delete a particular row by clicking button from that row which is in for loop,but its deleting all the rows of that table. 我正在尝试通过单击for循环中该行的按钮来删除特定行,但是会删除该表的所有行。

Here is my Html code: 这是我的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>

Here is my Component: 这是我的组件:

public loadData() {       

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

        }

    getDataOnSuccess(response) {       
        this.modalData = response.Items;
        }

This is my delete method: 这是我的删除方法:

public Delete(response) {
     this.myCartService.updateExamOrder(response)
     .subscribe(response => this.getDataOnSuccessForDelete(response),
     response => this.getDataOnErrorForDelete(response));
} 

Please help me to do, How to delete only one row from table? 请帮我做,如何从表中仅删除一行?

You can add index in *ngFor : 您可以在* ngFor中添加index

<tr *ngFor="let cart of modalData;let i = index">

Then, pass the index in the method: 然后,在方法中传递索引:

<button class="button_transparent" (click)="delete(i)"> delete </button>

And finally: 最后:

delete(i){
  this.modalData.splice(i,1);
}

you can do this: 你可以这样做:

<button class="button_transparent" (click)="delete(cart)"> delete </button>

then 然后

delete(item){
    this. modalData = this. modalData.filter(item => item.id !== id);
    this.modalData.push();}

here you are creating a new list without the element that you want to delete. 在这里,您正在创建一个新列表,其中没有要删除的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM