lundi 16 avril 2018

Angular 5 array from checkboxes

I have an array that comes from server response, I would like to add a check box to each element, so as many elements are selected and submit button clicked, selected elements should be deleted. To do so I need to get an array of selected elements in my component.ts, where I use group form, is there any way to implement this?

component

this.delAddressForm = this.fb.group({
   //these elements shuold contain a list of selected checkboxes
   'elements' : [null, Validators.required], 
   'action' : ['remove'],
});

del(formData : any){
    console.log(formData) //returns -> {action:"remove", elements:true}, should be {action:"remove", elements:[element1, element2, element3, ...]}
}

template

<form [formGroup]="delAddressForm" (ngSubmit)="del(delAddressForm.value)">
   <div *ngFor='let delivery of user.delivery;'>
     <div class="form-check">
       <input type="checkbox" class="form-check-input" id="elements" formControlName="elements">
       <label class="form-check-label" for="elements"></label>
     </div><br>
   </div>

   <button type="submit" class="btn btn-outline-danger pull-right" [disabled]="!delAddressForm.valid"><i class="fa fa-fw fa-times"></i></button>
</form>

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire