user2480218
user2480218

Reputation: 61

Angular Checkbox List not working

I want to create a checkbox list as shown here. I want to display a checkbox and name and on submit i want to get all selected value. I am not able to display the name and set selected value on checkbox.

[] Jim 
[x] Mondo
[] Ann

I have created this code:

  <div *ngIf="formSubmitted && teamForm.pristine" class = "submitted"> Form submitted successfully. </div>
<div class="team">
  <form [formGroup]="teamForm" (ngSubmit)="onFormSubmit()">
    <div class="all-emp">
      <b> Employees in Team :{{empFormArray.controls.length}}</b><br><br>
      <div formArrayName="empFormArray"> 
        <div *ngFor = "let emp of empFormArray.controls; let idx = index" [formGroupName]="idx" class="employee">
          <p> <b>Employee : {{idx + 1}}</b> </p>
            <p>Emp Id : <input type="checkbox" [value]="emp.isSelected"  formControlName="name">{{emp.name}}</p>
        </div>
      </div>

    </div> <br/>
 <button [disabled]="teamForm.invalid">SUBMIT</button>

This my component class. Please check my employee method:

export class CheckboxlistComponent implements OnInit {

  teamForm:FormGroup;
  formSubmitted = false;

  constructor(
       private formBuilder:FormBuilder) { 
  }

  ngOnInit() {
      this.createTeamForm();
      this.addEmployee('00', true);
      this.addEmployee('99', false);
      this.addEmployee('77', false);
  }

createTeamForm(){
      this.teamForm = this.formBuilder.group({
      empFormArray: this.formBuilder.array([]) 
      }); 
  }
  get empFormArray(): FormArray{
      return this.teamForm.get('empFormArray') as FormArray;
  }

  addEmployee(name, selected){

    let obj = {
       name: name,
      isSelected: selected
    }

    let fg = this.formBuilder.group({
        name: [obj]
      });

      this.empFormArray.push(fg);     
  }

  onFormSubmit() {
      let data = JSON.stringify(this.teamForm.value);
      console.log(data);

    this.formSubmitted = true;
      // this.teamForm.reset();   
  }   
}

Upvotes: 2

Views: 246

Answers (2)

AVJT82
AVJT82

Reputation: 73367

You are pushing an Object name containing you name and isSelected to your formArray:

let obj = {
  name: name,
  isSelected: selected
}

let fg = this.formBuilder.group({
  name: [obj] // here!
});

this.empFormArray.push(fg);     

What you'd want is to push just the obj as is to the formArray, so:

let fg = this.formBuilder.group({
  name: name,
  isSelected: selected
});

Also you'd want to modify your template and set the formcontrol in the checkbox as isSelected instead of name and also remove [value]. Also notice we need to use controls in template to reach your employee name. So change:

<input type="checkbox" [value]="emp.isSelected" formControlName="name">{{emp.name}}

to:

<input type="checkbox" formControlName="isSelected">{{emp.controls.name.value}}

That should sort out your issues :)

StackBlitz

Upvotes: 2

Nirav Gandhi
Nirav Gandhi

Reputation: 2005

I recommend using https://vitalets.github.io/checklist-model/. This makes working with checklists super easy.

Upvotes: 0

Related Questions