如何在 angular 5 中使用 splice
how to use splice in angular 5
我创建了一个 table 来显示从数据库中获取的数据。获取数据的 TS 部分类似于:
this._appService.getVisitData().subscribe((data) => {
this.checkinTemp = data;
// Calling the DT trigger to manually render the table
this.dtTrigger.next();
});
生成了table,我添加了编辑删除功能,我的查看代码:
<table datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" class="row-border hover">
<thead>
<tr>
<th>ID</th>
<th>Visitor Name</th>
<th>Department</th>
<th>Purpose</th>
<th>Schedule Date Time</th>
<th>Entry Source</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let data of checkinTemp">
<td>{{data.id}}</td>
<td>
<a (click)="viewCheckinData(data)"> {{data.f_name}} {{data.l_name}} </a>
<span>
<a (click)="editCheckinData(data)">Edit</a>
<a (click)="confirmDelete(data)">Delete</a>
</span>
</td>
<td>
<span *ngIf="data.department == null || data.department == ''">N/A</span>
<span *ngIf="data.department !== null ">{{data.department}}</span>
</td>
<td>
<span *ngIf="data.purpose == null || data.purpose == ''">N/A</span>
<span *ngIf="data.purpose !== null ">{{data.purpose}}</span>
</td>
<td>{{data.pre_check_in | date:'short'}}</td>
<td>{{data.creator_id}}</td>
</tr>
</tbody>
</table>
<p-confirmDialog header="Confirmation" icon="fa fa-question-circle" width="425"></p-confirmDialog>
现在,当用户按下删除键时,confirmDelete(data)
会被调用。 TS文件中的函数:
confirmDelete(item) {
this.confirmationService.confirm({
message: 'Are you sure you want to delete this Visitor?',
header: 'Confirmation',
accept: () => {
this._appService.deleteCheckin(item.id).subscribe(res => {
// Splice Or something so the page doesnt reload but the data gets removed from the view.
this.flashMsg.flashMsg('success', 'Deleted', 'Visitor has been deleted.');
},
err => {
this.flashMsg.flashMsg('error', 'Error', 'Visitor has not been deleted.');
});
},
reject: () => {
},
});
}
到目前为止,当我删除数据时,数据被删除并弹出确认消息,但数据仍然显示在视图中,直到页面重新加载。有没有办法在不加载页面的情况下从 table 中删除数据?我查找了 splice 但没有在代码中使用它。
从服务器成功删除项目后,您可以使用 splice(...)
从数组中删除它,如下所示:
const index = this.checkinTemp.indexOf(item);
this.checkinTemp.splice(index, 1);
我创建了一个 table 来显示从数据库中获取的数据。获取数据的 TS 部分类似于:
this._appService.getVisitData().subscribe((data) => {
this.checkinTemp = data;
// Calling the DT trigger to manually render the table
this.dtTrigger.next();
});
生成了table,我添加了编辑删除功能,我的查看代码:
<table datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" class="row-border hover">
<thead>
<tr>
<th>ID</th>
<th>Visitor Name</th>
<th>Department</th>
<th>Purpose</th>
<th>Schedule Date Time</th>
<th>Entry Source</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let data of checkinTemp">
<td>{{data.id}}</td>
<td>
<a (click)="viewCheckinData(data)"> {{data.f_name}} {{data.l_name}} </a>
<span>
<a (click)="editCheckinData(data)">Edit</a>
<a (click)="confirmDelete(data)">Delete</a>
</span>
</td>
<td>
<span *ngIf="data.department == null || data.department == ''">N/A</span>
<span *ngIf="data.department !== null ">{{data.department}}</span>
</td>
<td>
<span *ngIf="data.purpose == null || data.purpose == ''">N/A</span>
<span *ngIf="data.purpose !== null ">{{data.purpose}}</span>
</td>
<td>{{data.pre_check_in | date:'short'}}</td>
<td>{{data.creator_id}}</td>
</tr>
</tbody>
</table>
<p-confirmDialog header="Confirmation" icon="fa fa-question-circle" width="425"></p-confirmDialog>
现在,当用户按下删除键时,confirmDelete(data)
会被调用。 TS文件中的函数:
confirmDelete(item) {
this.confirmationService.confirm({
message: 'Are you sure you want to delete this Visitor?',
header: 'Confirmation',
accept: () => {
this._appService.deleteCheckin(item.id).subscribe(res => {
// Splice Or something so the page doesnt reload but the data gets removed from the view.
this.flashMsg.flashMsg('success', 'Deleted', 'Visitor has been deleted.');
},
err => {
this.flashMsg.flashMsg('error', 'Error', 'Visitor has not been deleted.');
});
},
reject: () => {
},
});
}
到目前为止,当我删除数据时,数据被删除并弹出确认消息,但数据仍然显示在视图中,直到页面重新加载。有没有办法在不加载页面的情况下从 table 中删除数据?我查找了 splice 但没有在代码中使用它。
从服务器成功删除项目后,您可以使用 splice(...)
从数组中删除它,如下所示:
const index = this.checkinTemp.indexOf(item);
this.checkinTemp.splice(index, 1);