[英]How to delete a row in a table in Angular4 . The delete button is in each row
Component 零件
<tr *ngFor="let item of items; let i = index">
<th>{{ i + 1 }}</th>
<th>{{ item.id }}</th>
<td>{{ item.title }}</td>
<th><button (click)="deleteItem()">Edit</button></th>
</tr>
Item.service Item.service
deleteItem(){
let header = new HttpHeaders();
return this._http.delete(this.api);
I assume i should append the delete button with id for the item. 我假设我应该为该项目添加ID为ID的删除按钮。 But I dont know how to implement that. 但是我不知道如何实现。
Im using the new HttpClient 我正在使用新的HttpClient
You can pass the item
as a parameter to deleteItem
as 您可以将该item
作为参数传递给deleteItem
作为
<tr *ngFor="let item of items; let i = index">
<th>{{ i + 1 }}</th>
<th>{{ item.id }}</th>
<td>{{ item.title }}</td>
<th><button (click)="deleteItem(item)">Edit</button></th>
</tr>
Now, depending on your actual API endpoints, you can do something like 现在,根据您的实际API端点,您可以执行以下操作
deleteItem(item: any){
return this._http.delete(this.api + '/items/' + item.id);
}
You should be able to just add a parameter to the deleteItem()
method of yours. 您应该只需要向您的deleteItem()
方法添加一个参数即可。
<tr *ngFor="let item of items; let i = index">
<th>{{ i + 1 }}</th>
<th>{{ item.id }}</th>
<td>{{ item.title }}</td>
<th><button (click)="deleteItem(item.id)">Edit</button></th>
</tr>
deleteItem(number id){
let header = new HttpHeaders();
return this._http.delete(this.api);
Just pass the item id in delete function like 只需在删除功能中传递商品ID即可
<th><button (click)="deleteItem(item.id)">Edit</button></th>
And get that id in your delete function as a parameter and pass to the api 并在您的delete函数中获取该ID作为参数,然后传递给api
deleteItem(itemId){
let header = new HttpHeaders();
return this._http.delete(this.api);
}
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.