Ionic - Get values from multiple checkboxes in ngFor loop


#1

Hi Guys!

I know this question is pretty stupid but i’m stuck on this for 2/3 days now and i’m still a newbie in ionic 2 :confused:

I have a database that stores player names and a page with checkboxes that lets me select player names.

I would like to put every selected player into an array.

Here is the code:

<ion-item *ngFor="let player of players">
    <ion-label>{{player.name}}</ion-label>
    <ion-checkbox color="dark"></ion-checkbox>
</ion-item>

Is there any way i could achieve that?


#2

I’m having the same dumb problem. Anybody?


#3

My solution for similar problem is for each item to keep a flag that keeps item state (i.e. checked or unchecked). In your case code will look something like this:

<ion-item *ngFor="let player of players">
    <ion-label>{{player.name}}</ion-label>
    <ion-checkbox color="dark" [(ngModel)]="player.checked"></ion-checkbox>
</ion-item>

This way you know what players has been selected and do what ever you need with this information.

Hope it helps.


#4

Hi,
I’d like to parse all checkbox selected values and store them in an array


#5

Players.map(player=>{this.myarray.push…