gpt4 book ai didi

angular - 如何在 Angular 5 中使用拼接

转载 作者:太空狗 更新时间:2023-10-29 18:04:03 25 4
gpt4 key购买 nike

我创建了一个表来显示从数据库中获取的数据。获取数据的 TS 部分类似于:

this._appService.getVisitData().subscribe((data) => {
this.checkinTemp = data;
// Calling the DT trigger to manually render the table
this.dtTrigger.next();
});

表格生成,我添加了编辑和删除功能,我的 View 代码:

<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: () => {
},
});
}

到目前为止,当我删除数据时,数据被删除并弹出确认消息,但数据仍显示在 View 中,直到页面重新加载。有没有办法在不加载页面的情况下从表中删除数据?我查找了 splice 但没有在代码中使用它。

最佳答案

从服务器成功删除项目后,您可以使用 splice(...) 将其从数组中删除,如下所示:

const index = this.checkinTemp.indexOf(item);
this.checkinTemp.splice(index, 1);

关于angular - 如何在 Angular 5 中使用拼接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49933001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com