有没有一种方法可以从一个对象数组中创建动态表格组来编辑Angular 11中动态表的表行?
我想创建一个动态数据表,该表将从后端服务器接收数据。数据将作为对象数组。是否有一种方法可以从数据中动态创建/绑定表单组,该组将允许对表的每一行进行内联编辑并在编辑模式时更新更改。除非我必须这样做,否则我不想每次都为表格数据进行硬码数据。我也无法为该项目使用角材料。这是表行的组件。该表充当数据表,但我似乎无法弄清楚如何使其可编辑。
import {
Component,
Input,
Output,
EventEmitter,
ViewChild,
ChangeDetectionStrategy,
} from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
import { EditButtonGroupComponent } from '../../edit-button-group/edit-button-group.component';
@Component({
selector: '[dataTableRow]',
templateUrl: './data-table-row-form.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class DataTableRowFormComponent {
@ViewChild('editButtonGroup', { static: true })
editButtonGroup: EditButtonGroupComponent;
@Input() data: any[];
@Input() formModel: any;
@Input() useEditButton: boolean;
@Input() updating: boolean;
@Input() deleting: boolean;
@Input() index: number;
checkMark(value: any) {
if (value === true) {
return 'bi bi-check-lg';
} else {
return '';
}
}
booleanCheck(val: any) {
if (typeof val === 'boolean') {
return true;
} else {
return false;
}
}
returnZero() {
return 0;
}
enableEditMode() {
this.editModeEnabled = true;
this.copyModel();
}
disableEditMode() {
this.editModeEnabled = false;
this.copyModel();
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.0/angular.min.js"></script>
<td *ngFor="let y of data[index] | keyvalue : returnZero">
<i [ngClass]="checkMark(y.value)" *ngIf="booleanCheck(y.value) else default"></i>
<ng-template #default>
<ng-container *ngIf="!editModeEnabled else form">{{y.value}}</ng-container>
<ng-template #form>
<input type="text" class="form-control" [(ngModel)]="formModel" />
</ng-template>
</ng-template>
</td>
<td *ngIf="useEditButton">
<edit-button-group [editing]="isEditing && updating" [deleting]="isDeleting && deleting" (onDelete)="delete()"
(onEdit)="update()" (onEditModeEnabled)="enableEditMode()" (onEditModeDisabled)="disableEditMode()"
(onDelete)="onEmit()">
</edit-button-group>
</td>
1。
列表项目
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一样
要从formarray创建一个表,就像我们看到
您使用getter获取FormGroup 。这样,您将neen'th将formarray封闭在一个formGroup内部,
但我们希望用innamy创建列,因此第一个是知道表的“列”
。.html的“ .html”变成大约
,我们有两个函数,编辑可以取消
stackblitz
To create a table from a FormArray it's just some like
Where we has
See that you use a getter to get the formGroup. In this way you neen'd enclosed the formArray inside a FormGroup
But we want create the columns dinamically, so the first is know the "columns" of your table
The .html becomes like
And we have two functions, edit can cancel
the stackblitz
是的,这绝对是可能的。您需要创建一个表单阵列。
最终,您需要将数据推入表单。
Yes, this is definitely possible. You'll need to create a form array.
Ultimately, you need to push your data into the form.