【问题标题】:How to delete a particular row from table by clicking button Angular 2如何通过单击按钮 Angular 2 从表中删除特定行
【发布时间】:2018-01-27 04:45:36
【问题描述】:

我正在尝试通过单击 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>

这是我的组件:

public loadData() {       

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

        }

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

这是我的删除方法:

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

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

【问题讨论】:

  • 你的意思是从上到下一层一层?
  • 你能发布你的删除方法吗?
  • public Delete(response) { this.myCartService.updateExamOrder(response) .subscribe(response => this.getDataOnSuccessForDelete(response), response => this.getDataOnErrorForDelete(response)); }这是我的删除方法

标签: javascript angular html-table


【解决方案1】:

您可以在 *ngFor 中添加index

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

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

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

最后:

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

【讨论】:

  • 是的,我知道了,谢谢
  • 出了什么问题?
【解决方案2】:

你可以这样做:

&lt;button class="button_transparent" (click)="delete(cart)"&gt; delete &lt;/button&gt;

然后

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

在这里,您正在创建一个没有要删除的元素的新列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多