viana
viana

Reputation: 515

Get values from a dynamic checkbox list

See my html:

<ion-item ion-item *ngFor="let item of items">
    <ion-label>{{ item.name }}</ion-label>
    <ion-checkbox color="secondary"></ion-checkbox>
</ion-item>

My .TS:

this.database.executeSql("SELECT * FROM check_item", []).then((data) => {
        this.items = [];
        if (data.rows.length > 0) {
            for (var i = 0; i < data.rows.length; i++) {
                this.items.push({ id: data.rows.item(i).id, name: data.rows.item(i).name });
            }
        }
    }, (error) => {
        console.log("ERROR: " + JSON.stringify(error));
    });

I would like to retrieve the values from the checkbox and save it to a tablela. How can I get values from a dynamic checkbox list?

Upvotes: 3

Views: 3741

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657148

<ion-item ion-item *ngFor="let item of items;let i=index">
    <ion-label>{{ item.name }}</ion-label>
    <ion-checkbox [(ngModel)]="checkedItems[i]" color="secondary"></ion-checkbox>
</ion-item>

where checkedItems is an array of the same size as items that you need to prepare and initialize.

checkedItems:boolean[];

constructor() { // or whereever `items` is initialized
  this.items = ...
  this.checkedItems = new Array(this.items.length);
}

Upvotes: 4

Related Questions