html - 我想获取单击用户的 ID 并在我在引导模式上单击是时将其删除
问题描述
这是 html 文件,我想获取特定学生的 id 点击并使用弹出模式删除学生
<div style="margin-top: 50px;">
<table datatable="ng" class="row-border hover" [dtOptions]="dtOptions" [dtTrigger]="dtTrigger">
<thead>
<tr>
<th>S/N</th>
<th>Full Name</th>
<th>Class</th>
<th>Sex</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let student of students; let i = index">
<td>{{i+1}}</td>
<td><a [routerLink]="['/view-student', student.id]" >{{student.lastName + ' ' + student.firstName}}</a></td>
<td>{{student.grade.name}}</td>
<td>{{student.sex.name}}</td>
<td><a [routerLink]="['/edit-student', student.id]" class="btn btn-primary" >Edit</a></td>
<td><a data-toggle="modal" data-target="#myModal" class="btn btn-danger">Delete</a></td>
</tr>
</tbody>
</table>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Warning</h4>
</div>
<div class="modal-body">
Are you sure you want to delete..
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
<button type="button" (click)="" data-dismiss="modal" class="btn btn-primary">Yes</button>
</div>
</div>
</div>
</div>
这是我在组件中用于删除我点击的学生的删除功能。提前谢谢你的帮助
delete(id){
this.studentService.delete(id).subscribe(() => {
this.students.splice(id, 1);
this.loadAllStudents();
});}
解决方案
只需在组件属性中保存当前学生 ID。
html(表格):
...
<td><a (click)="setId(student.id)" data-toggle="modal" data-target="#myModal" class="btn btn-danger">Delete</a></td>
...
ts:
public id = ''
setId(id) {
this.id = id
}
html(模态):
...
<button type="button" (click)="delete(id)" data-dismiss="modal" class="btn btn-primary">Yes</button>
...
推荐阅读
- c# - C#初始化嵌套类和外部类产生空引用
- asp.net-core-3.1 - 返回 JSON 401 响应对象而不是 IIS 401 错误页面
- pandas - pandas iterrows 进入字典并创建一个新的数据框不起作用
- sed - 如何在替换表达式中使用 '^' 字符
- r - 如何通过上 50% 的百分位数过滤我的数据以获得单独的因变量?
- sql - 需要由两周前日期的数据组成的列进行比较
- android - 我如何知道我的 Android 应用程序是否在前台,包括在显示插页式广告时?
- angular9 - 如何在角度 9 中为多个 if else 语句编写测试用例
- r - 将逗号分隔的数据集作为单独的列导入时出现问题
- mysql - cPanel 部署:我的 app.js 暴露给大家看