角动态表
我正在尝试以角度构建形式,以便用户可以在表中输入数字。行的数量和列的数量可能不同(Nrows,ncolumns),但用户不可编辑。 预期的结果应该是这样的:
对于我正在使用formarray的行,NGFOR正在工作。 但是我无法在列上迭代。 也许我的方法反正不好?
// transport-form.component.ts
export class TransportFormComponent implements OnInit {
title = 'formarray';
kostenForm!: FormGroup;
rows!: FormArray;
nRows = 3;
nColumns = 3;
constructor(private formBuilder: FormBuilder) {}
ngOnInit() {
this.kostenForm = new FormGroup({
rows: new FormArray([]),
});
for (let i = 0; i < this.nRows; i++) {
this.addItem();
}
}
createItem(): FormGroup {
let row = {};
for (let j = 0; j < this.nColumns; j++) {
row[`v${j}`] = '';
}
return this.formBuilder.group(row);
}
addItem(): void {
this.rows = this.kostenForm.get('rows') as FormArray;
this.rows.push(this.createItem());
}
}
// transport-form.component.html
<div [formGroup]="kostenForm">
<div formArrayName="rows"
*ngFor="let row of kostenForm.get('rows')['controls']; let i = index;">
<div [formGroupName]="i">
<!-- <ng-container *ngFor="let control of row.controls; let j = index">
<input formControlName="v{j}">???
</ng-container> Does not work -->
<!-- Workaround to demonstrate expected output: -->
<input formControlName="v0">
<input formControlName="v1">
<input formControlName="v2">
</div>
</div>
</div>
{{ kostenForm.value | json }}
I am trying to build a Form in Angular so that a user can enter numbers in a table. The number of rows and columns can be different (nRows, nColumns) but not editable by the user.
The expected result should be something like this:
For the rows I am using a FormArray and ngFor is working.
But I can not iterate over the Columns.
Maybe my approach is not good anyway?
// transport-form.component.ts
export class TransportFormComponent implements OnInit {
title = 'formarray';
kostenForm!: FormGroup;
rows!: FormArray;
nRows = 3;
nColumns = 3;
constructor(private formBuilder: FormBuilder) {}
ngOnInit() {
this.kostenForm = new FormGroup({
rows: new FormArray([]),
});
for (let i = 0; i < this.nRows; i++) {
this.addItem();
}
}
createItem(): FormGroup {
let row = {};
for (let j = 0; j < this.nColumns; j++) {
row[`v${j}`] = '';
}
return this.formBuilder.group(row);
}
addItem(): void {
this.rows = this.kostenForm.get('rows') as FormArray;
this.rows.push(this.createItem());
}
}
// transport-form.component.html
<div [formGroup]="kostenForm">
<div formArrayName="rows"
*ngFor="let row of kostenForm.get('rows')['controls']; let i = index;">
<div [formGroupName]="i">
<!-- <ng-container *ngFor="let control of row.controls; let j = index">
<input formControlName="v{j}">???
</ng-container> Does not work -->
<!-- Workaround to demonstrate expected output: -->
<input formControlName="v0">
<input formControlName="v1">
<input formControlName="v2">
</div>
</div>
</div>
{{ kostenForm.value | json }}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于声誉较低,我无法发表评论,所以我添加了答案。
长话短说,如果您有各种各样的数组,则可以创建formarrays的形式。
您可以在本文的接受答案中找到所有解释:
Angular-Formarray:从二维JavaScript数组中创建表格
在帖子中他使用2x2数据阵列,但我认为您可以将其调整为所需的。
I can't comment due to my low reputation, so I add an answer.
Long story short, if you have an Array of Array, you can create a FormArrays of FormArrays.
You can find all the explanations in the accepted answer of this post:
Angular-FormArray: Creating a table form from 2-dimensional Javascript array
In the post he use a 2x2 data array, but I think you can adapt it to your needed.