jeudi 27 juin 2019

Checkbox value is not changing in reactive forms angular

i m generating form from json file with help of reactive forms.Generation of form works properly but the problem i m facing is - when i click on checkbox for first time in mozila its value becomes "on". but when i uncheck it nothing is happening its value remains "On" which should be "off or false". In chrome on click nothing is happening.

app.components.ts

import { Component } from '@angular/core';
import { QuestionControlService } from './question-control.service';
import GxData from './gx.json';
@Component({
  selector: 'app-root',
  template: `
    <div>
      <app-dynamic-form [questions]="questions"></app-dynamic-form>
    </div>
  `,
  providers: []
})
export class AppComponent{

  questions: any[];
  constructor() {
    this.questions = GxData;
  }

} 

dynamic-form.components.ts

import { Component, Input, OnInit, SimpleChanges } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { QuestionControlService } from '../question-control.service';
import SbbData from '../sbb.json';
import GxData from '../gx.json';

@Component({
  selector: 'app-dynamic-form',
  templateUrl: './dynamic-form.component.html',
  providers: [QuestionControlService]
})
export class DynamicFormComponent implements OnInit {

  @Input() questions: any[] = [];
  form: FormGroup;
  payLoad = '';

  constructor(private qcs: QuestionControlService) { }

  ngOnInit() {
    this.form = this.qcs.toFormGroup(this.questions);
  }
  callGx() {
    this.questions = GxData;
    this.form = this.qcs.toFormGroup(this.questions);
  }
  callSbb() {
    this.questions = SbbData;
    this.form = this.qcs.toFormGroup(this.questions);
  }

  onSubmit() {

    this.payLoad = JSON.stringify(this.form.value);
    console.log(JSON.parse(this.payLoad));
  }

}

dynamic-form.component.html

<div class="search_box">
  <form (ngSubmit)="onSubmit()" [formGroup]="form">
      <button type="button" (click)="callSbb()">SBB</button> 
      <button type="button" (click)="callGx()">GX</button> 
    <div *ngFor="let question of questions" class="form-row">
      <app-question [question]="question" [form]="form"></app-question>
    </div>

    <div class="form-row">
      <button type="submit" [disabled]="!form.valid">Save</button>
    </div>
  </form>

  <div *ngIf="payLoad" class="form-row">
    <strong>Saved the following values</strong><br>
  </div>
</div>
 

question-control.service.ts

import { Injectable } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';

@Injectable()
export class QuestionControlService {
  constructor() { }

  toFormGroup(questions: any[]) {
    let group: any = {};

    questions.forEach(question => {
      group[question.key] = question.required ? new FormControl(question.value || '', Validators.required)
        : new FormControl(question.value || '');
    });
    return new FormGroup(group);
  }
}

dynamic-form-question.component.ts

import { Component, Input } from '@angular/core';
import { FormGroup } from '@angular/forms';

// import { QuestionBase } from '../question-base';

@Component({
  selector: 'app-question',
  templateUrl: './dynamic-form-question.component.html'
})
export class DynamicFormQuestionComponent {
  @Input() question: any;
  @Input() form: FormGroup;
  get isValid() { return this.form.controls[this.question.key].valid; }
}

dynamic-form-question.component.html

<div [formGroup]="form">


  <div [ngSwitch]="question.controlType" class="checkbox_wrapper">

    <input *ngSwitchCase="'textbox'" [formControlName]="question.key" [id]="question.key" [type]="question.type" name="">
    <label [attr.for]="question.key"></label>

    <select [id]="question.key" *ngSwitchCase="'dropdown'" [formControlName]="question.key" name = "" >
      <option *ngFor="let opt of question.options" [attr.value]="opt.key" [selected]="opt.select"></option>
    </select>
  </div>
  <div class="errorMessage" *ngIf="!isValid"> is required</div>
</div> 

both json-files look like

[
    {
        "key": "Context",
        "label": "Context",
        "required": false,
        "order": 1,
        "controlType": "textbox",
        "name": "Context",
        "type": "checkbox"
    },
    {
        "key": "contextopt",
        "label": "",
        "required": false,
        "order": 2,
        "controlType": "dropdown",
        "name": "contextopt",
        "options": [
            {
                "key": "All Context",
                "value": "All Context",
                "select": true
            },
            {
                "key": "aaa",
                "value": "aaa"
            },
            {
                "key": "bb",
                "value": "bb"
            },
            {
                "key": "Other",
                "value": "Other"
            }
        ]
    },
    {
        "key": "Movement",
        "label": "Movement",
        "required": false,
        "order": 3,
        "controlType": "textbox",
        "name": "Movement",
        "type": "checkbox"
    }
]




Aucun commentaire:

Enregistrer un commentaire