1

I have tried angular 2 to list categories list with checkbox and user can select any category. I tried with below code. The issue is if I selected any one checkbox all the checkbox are selected and I cannot get the form values on save function. Food.category is dynamic. How can I fix this?

<form #f="ngForm" novalidate>
    <div class="checkbox checkbox-primary" *ngFor="let categoryname of Food.category;">
        <label for="categoryname">          
            <input type = "checkbox" name="categoryname[]" [(ngModel)]="Catcheckbox" (click)="onClick(categoryname)" value="{{categoryname}}"/>
            {{categoryname}}
        </label>
    </div>  
    <button type="submit" (click)="save(f.value, f.valid)" class="btn btn-default">Submit</button>
</form> 

export class ExploreListComponent implements OnInit {
    Catcheckbox:true ;

    onClick(categoryname) {
        alert(categoryname + ' was clicked!');
    }

    save(isValid: boolean, f: User) {
        if (!isValid) return;
        console.log(f);
    }
}
Dale K
  • 25,246
  • 15
  • 42
  • 71
Vel
  • 9,027
  • 6
  • 34
  • 66

3 Answers3

1

Change your code as below , and you will get all selected checkboxes values;

Template :

<form #f="ngForm" novalidate>
    <div class="checkbox checkbox-primary" *ngFor="let categoryname of Food.category;let i=index;">
        <label for="categoryname">          
            <input type="checkbox" name="categoryname[{{i}}]"  [value]="categoryname" 
(change)="categories[$event.target.getAttribute('value')]=$event.target.checked"/>
        {{categoryname}}
        </label>
    </div>  
        <button type="submit" (click)="save(f.valid)" class="btn btn-default">Submit</button>
</form> 

Component :

export class ExploreListComponent implements OnInit {

        categories = {};

        save(isValid: boolean) {
            if (!isValid) return;
            console.log(this.categories);
        }
    }
Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122
0
<div class="checkbox checkbox-primary" *ngFor="let categoryname of Food.category;">
            <label for="categoryname">          
             <input type = "checkbox" *ngIf="selectAll" [checked]="selectedAll" name="categoryname[]" [(ngModel)]="Catcheckbox" (click)="onClick(categoryname)" value="{{categoryname}}"/>


               <input type = "checkbox" *ngIf="selectAll" [checked]="!selectedAll" name="categoryname[]" [(ngModel)]="Catcheckbox" (click)="onClick(categoryname)" value="{{categoryname}}"/>
            {{categoryname}}
            </label>
        </div>  
         <button type="submit" (click)="save(f.value, f.valid)" class="btn btn-default">Submit</button>
    </form> 

on click of the select all button

this.selectAll=true

So all the elements are selected and you have the entire array passed to *ngFor

Aravind
  • 40,391
  • 16
  • 91
  • 110
0

I would recommend using ReactiveFormsModule. In this case you are assigning the array to all input name attributes. Try to assign only current index like name="{{i}}" which is set in *ngFor directive

<form #f="ngForm" novalidate>
    <div class="checkbox checkbox-primary" *ngFor="let categoryname of Food.category; let i = index">
        <label for="categoryname">          
         <input type = "checkbox" name="{{i}}" [(ngModel)]="Catcheckbox" (click)="onClick(categoryname)" value="{{categoryname}}"/>
        {{categoryname}}
        </label>
    </div>  
     <button type="submit" (click)="save(f.value, f.valid)" class="btn btn-default">Submit</button>
</form>