javascript - 如何动态地将 id 添加到表行中,以便可以唯一标识它们
问题描述
我在 manage.component.html 中有一个表结构,其中填充了来自服务器的数据。我有一个“编辑”按钮,可用于编辑列中的单元格。
要求:我想唯一标识每个单元格,可以进一步用于编辑并将值返回值发送回服务器以进行更新。
我试图将“counter”附加到“id”的“id”上,这样可以用来唯一标识每个行单元格,但是“counter”没有设置在“id”上
管理.component.html
<tbody>
<tr *ngFor="let work of workflows$">
<td>{{work.req_id}}</td>
<td><input id="btnn{{count$}}" type="text" value="{{work.client_ip}}" maxlength="4" placeholder="name" onload="addID()"/></td>
<td>{{work.client_type}}</td>
<td>{{work.project}}</td>
<td><button (click)="edit()" type="button" class="btn btn-primary">Edit</button></td>
</tr>
</tbody>
管理.component.ts
export class ManageComponent implements OnInit {
count$ = 0;
edit(event){
document.getElementById('btnn').focus();
}
addID(){
this.count$ = 5;
}
}
当我单击“编辑”按钮时,焦点应该在行的元素上,编辑后,当我单击“发送”按钮时,值的更改应该可以在 .ts 文件上访问,以便可以将值发送回服务器。
解决方案
有多种方法可以做到这一点。但是由于您想通过 id 或 class 获取它,请查看以下代码
<tbody>
<tr *ngFor="let work of workflows$; let i = index">
<td>{{work.req_id}}</td>
<td><input id="btnn-{{i}}" type="text" value="{{work.client_ip}}" maxlength="4" placeholder="name" onload="addID(i)"/></td>
<td>{{work.client_type}}</td>
<td>{{work.project}}</td>
<td>
<!-- Pass the index i as a parameter in edit() method -->
<button (click)="edit(i)" type="button" class="btn btn-primary">Edit</button>
</td>
</tr>
</tbody>
在你的打字稿中
edit(_index){
document.getElementById('btnn-'+_index).focus(); //get the element by id
}
希望有帮助
推荐阅读
- sass - 如何自定义 Bootstrap 5 Mixin
- java - 如何验证返回的状态代码是否成功,即使用 Rest Assured 的任何 2xx HTTP 状态代码?
- infinispan - HotRod 客户端不断向关闭服务器发送请求
- java - 使用 cucumber-spring 合并属性文件
- laravel - laravel websocket 不会在生产中连接
- icalendar - 使用 Caldav 进行同步
- pandas - 如何按数据框中的数据计算频率?
- node.js - 摩卡测试失败而没有给出正确的错误
- amazon-web-services - 基于 JSON 结构更新数据框中的值
- reactjs - 由于有时会在我的 UI 中加载数据,因此我对初始结果进行了硬编码以避免出现空白屏幕,我们是否有适当的解决方案?