这一次我需要关于如何删除行的帮助,当删除按钮单击时,在html-table中删除行ID。表数据源来自一个单独的Json文件。
表如下所示: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>
如果需要更多的代码片段,请让我知道。谢谢。
发布于 2017-10-31 16:35:44
您可以将此代码添加到HTML文件中
<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>并将此代码添加到组件文件中
deleteRow(id){
for(let i = 0; i < this.data.length; ++i){
if (this.data[i].id === id) {
this.data.splice(i,1);
}
}
}发布于 2019-03-23 00:29:00
<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>打字脚本
deleteRow(d){
const index = this.data.indexOf(d);
this.data.splice(index, 1);
}发布于 2017-10-31 16:28:42
我会在按钮上添加一个(单击)事件,并将'd‘作为参数传递。然后在单击调用的函数中,我将拼接“已删除”项。
添加(单击)事件的代码:
<button id="remove" (click)="functionToDeleteItem(d)">DELETE ROW</button>拼接数组和删除已删除项的示例:
stackoverflow past example - How do I remove an array item in TypeScript?
此外,除非您正在使用按钮中的id=“删除”用于其他用途,如css,我会删除它,因为它是不需要的。
https://stackoverflow.com/questions/47030574
复制相似问题