Get values from a dynamic checkbox list

筅森魡賤 提交于 2019-12-23 19:25:03

问题


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?


回答1:


<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);
}


来源:https://stackoverflow.com/questions/42315578/get-values-from-a-dynamic-checkbox-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!