从动态复选框列表中获取值 [英] Get values from a dynamic checkbox list

查看:119
本文介绍了从动态复选框列表中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看我的 html

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

我的 .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));
    });

我想从复选框中检索值并将其保存到tablela中。如何从动态复选框列表中获取值?

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?

推荐答案

<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>

其中 checkedItems 是一个相同的数组大小为 items 您需要准备和初始化。

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

这篇关于从动态复选框列表中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆