angular - 如何使用 angular2 使用表单一次编辑多行
问题描述
想使用 angular2 形式一次编辑多行
编辑 btn 不能一次编辑多行,同时尝试打开编辑第一行编辑关闭和选定行编辑打开,但要求所有行都应该是可编辑的
解决方案
您可以在实体模型上添加 FormGroup 字段。
例如;
export class Product{
name:string
manufacturer:string
editForm:FormGroup
isEditing:boolean
}
在你的桌子上;
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Manufacturer</th>
<th scope="col">#</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let product of products">
<ng-template [ngIf]="product.isEditing">
<form [formGroup]="product.editForm">
<td>
<input type="text" formControlName="name" />
</td>
<td>
<input type="text" formControlName="manufacturer" />
</td>
<td>
<button type="submit" (click)="saveChanges()">Save</button>
<button type="button" (click)="product.isEditing = false">Cancel</button>
</td>
</form>
</ng-template>
<ng-template [ngIf]="!product.isEditing">
<td>{{product.name}} </td>
<td>{{product.Manufacturer}}</td>
<td><button (click)="product.isEditing = true">Edit</button></td>
</ng-template>
</tr>
</tbody>
保存更改时
saveChanges(product:Product){
//Maybe api call
product.isEditing = false
}
推荐阅读
- javascript - JavaScript Google Cloud Function:将 Stripe 值写入 Firebase
- python - 打印 joblib 中函数的输出
- javascript - 查找所有可能的 n 个参数的组合(正负)总和?
- machine-learning - 为什么小数据集的方差很大?
- node.js - 在系统重启时启动 nginx 和服务器
- ios - 在我们完成执行 DispatchWorkItem 并尝试使用 DispatchQueue.main.async 更新 UI 之后,UI 视图项是否可能不再有效
- sql - 来自两个不同表的生效日期,匹配某个日期的记录
- java - 如何使用 WireMock 存根多个参数 Springboot Restful POST Endpoint
- android - 当设备关闭 1 小时或更长时间时,Alarmmanager 不会触发广播接收器
- node.js - 在生产中反应连接到没有端点的 API