在https://angular.io/guide/dynamic-form的帮助下,我正在制作一个动态表单,该表单需要首先显示两个字段。
new TextboxQuestion({
key: 'firstName',
label: 'First name',
value: '',
required: true,
order: 1
}),
new TextboxQuestion({
key: 'lastName',
label: 'Last name',
value: '',
required: true,
order: 2
}),
这两个字段必须在加载时首先显示。
此后,我将有两个按钮,分别为add and remove
。
<button (click)="addNew()"> Add </button>
<button (click)="removeNew()"> Remove </button> <br><br>
通过单击添加,我需要显示接下来的两个字段(以下字段)
new TextboxQuestion({
key: 'emailAddress',
label: 'Email',
type: 'email',
order: 3
}),
new DropdownQuestion({
key: 'brave',
label: 'Bravery Rating',
options: [
{key: 'solid', value: 'Solid'},
{key: 'great', value: 'Great'},
{key: 'good', value: 'Good'},
{key: 'unproven', value: 'Unproven'}
],
order: 4
})
订单1和2处于初始状态,点击添加后,需要显示下两个订单3和4。
请帮助我实现单击“添加”按钮时添加子字段的结果。
一次显示所有内容的工作堆栈闪电战,https://stackblitz.com/edit/angular-x4a5b6
答案 0 :(得分:1)
您唯一需要做的就是使用slice和变量“ page”“分页”您的问题
也就是说:
//add a variable "page" in your dinamic-form.component.ts
page:number=0;
和
//in your dinamic-form.component.html, change the *ngFor like
<form (ngSubmit)="onSubmit()" [formGroup]="form">
<div *ngFor="let question of questions.slice(page*2,(page+1)*2)" class="form-row">
<app-question [question]="question" [form]="form"></app-question>
</div>
....
</form>
然后,您的按钮添加可以像
<button (click)="page=page+1"> Add </button>
答案 1 :(得分:1)
使用formArray实现动态表单。
嗯,事情更复杂了。我做了一个stackblik,请参阅demo
我将尝试说明如何扩展https://angular.io/guide/dynamic-form以允许使用表单数组。
我们首先需要做的是创建一种新型的问题,一个questionArray
import { QuestionBase } from './question-base';
export class ArrayQuestion extends QuestionBase<string> {
controlType = 'array';
type: any;
constructor(options: {} = {}) {
super(options);
}
}
我们必须更改问题库以添加新的属性“儿童”
export class QuestionBase<T> {
value: T;
...
children:any[];
constructor(options: {
value?: T,
...
children?:any
} = {}) {
this.value = options.value;
...
this.children=options.children || null;
}
}
添加更改问题控制服务以允许管理formArrays
toFormGroup(questions: QuestionBase<any>[]) {
let group: any = {};
questions.forEach(question => {
//If the control type is "array" we create a FormArray
if (question.controlType=="array") {
group[question.key]=new FormArray([]);
}
else {
group[question.key] = question.required ? new FormControl(question.value || '', Validators.required)
: new FormControl(question.value || '');
}
});
return new FormGroup(group);
}
好吧,我们将dynamic-form.component转换为显示FormArray
<div *ngFor="let question of questions" class="form-row">
<ng-container *ngIf="question.children">
<div [formArrayName]="question.key">
<div *ngFor="let item of form.get(question.key).controls; let i=index" [formGroupName]="i">
<div *ngFor="let item of question.children">
<app-question [question]="item" [form]="form.get(question.key).at(i)"></app-question>
</div>
</div>
</div>
</ng-container>
<ng-container *ngIf="!question.children">
<app-question [question]="question" [form]="form"></app-question>
</ng-container>
</div>
就这样。那么,如何增加和减少formArrays?我们有两个按钮
<button (click)="addControls('myArray')"> Add </button>
<button (click)="removeControls('myArray')"> Remove </button> <br><br>
和两个函数addControls和removeControls
addControls(control: string) {
let question: any = this.questions.find(q => q.key == control);
let children = question ? question.children : null;
if (children)
(this.form.get(control) as FormArray).push(this.qcs.toFormGroup(children))
}
removeControls(control: string){
let array=this.form.get(control) as FormArray;
array.removeAt(array.length-1);
}
更新,我忘记添加问题的示例了:
let questions: QuestionBase<any>[] = [
new TextboxQuestion({
key: 'firstName',
label: 'First name',
value: '',
required: true,
order: 1
}),
new TextboxQuestion({
key: 'lastName',
label: 'Last name',
value: '',
required: true,
order: 2
}),
new ArrayQuestion({
key: 'myArray',
value: '',
order: 3,
children: [
new TextboxQuestion({
key: 'emailAddress',
label: 'Email',
type: 'email',
order: 3
}),
new DropdownQuestion({
key: 'brave',
label: 'Bravery Rating',
options: [
{ key: 'solid', value: 'Solid' },
{ key: 'great', value: 'Great' },
{ key: 'good', value: 'Good' },
{ key: 'unproven', value: 'Unproven' }
],
order: 4
})
]
})
];